|
|
1.1 root 1: /************************************************************************\
2: *
3: * PROGRAM: LOADTEST.C
4: *
5: * PURPOSE: A simple demonstration of LoadLibrary()-ing a DLL at
6: * runtime and obtaining function addresses within the DLL.
7: *
8: * FUNTIONS: WinMain() - initialization, create window, msg loop
9: * MainWndProc() - processes main window msgs
10: * ThreadProc() - makes a single call into "THE_DLL.DLL"
11: *
12: \************************************************************************/
13:
14: #include <windows.h>
15: #include "loadtest.h"
16: #include "the_dll.h"
17:
18:
19:
20: /************************************************************************\
21: *
22: * FUNCTION: WinMain (standard WinMain INPUTS/RETURNS)
23: *
24: * GLOBAL VARS: hwndMain - handle of main app window
25: *
26: * LOCAL VARS: msg - msg to get/dispatch
27: *
28: \************************************************************************/
29:
30: int APIENTRY WinMain (HANDLE hInstance,HANDLE hPrevInstance,
31: LPSTR lpCmdLine, int nCmdShow)
32: { MSG msg;
33:
34: if (!hPrevInstance)
35: {
36: WNDCLASS wc;
37:
38: wc.style = NULL;
39: wc.lpfnWndProc = (WNDPROC)MainWndProc;
40: wc.cbClsExtra = 0;
41: wc.cbWndExtra = 0;
42: wc.hInstance = hInstance;
43: wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
44: wc.hCursor = LoadCursor (NULL, IDC_ARROW);
45: wc.hbrBackground = GetStockObject (WHITE_BRUSH);
46: wc.lpszMenuName = (LPSTR) "Menu";
47: wc.lpszClassName = (LPSTR) "LOADTEST";
48:
49: if (!RegisterClass (&wc))
50: { MessageBox (NULL, "WinMain(): RegisterClass() failed",
51: "Err! - LOADTEST", MB_OK | MB_ICONHAND);
52: return(FALSE);
53: }
54: }
55:
56: if (!(hwndMain = CreateWindow ("LOADTEST", "LOADTEST Sample Application",
57: WS_OVERLAPPEDWINDOW,
58: CW_USEDEFAULT, CW_USEDEFAULT,
59: CW_USEDEFAULT, CW_USEDEFAULT,
60: NULL, NULL, hInstance, NULL)))
61: return (NULL);
62:
63: ShowWindow(hwndMain, nCmdShow);
64:
65: while (GetMessage(&msg, NULL, NULL, NULL))
66: { TranslateMessage(&msg);
67: DispatchMessage(&msg);
68: }
69: return (msg.wParam);
70: UNREFERENCED_PARAMETER(lpCmdLine);
71: }
72:
73:
74:
75: /************************************************************************\
76: *
77: * FUNCTION: MainWndProc (standard window procedure INPUTS/RETURNS)
78: *
79: * GLOBAL VARS: pfnDLLFunction1 - pointer to DLLFunction1
80: * pfnDLLFunction2 - pointer to DLLFunction2
81: * pfnDLLFunction3 - pointer to DLLFunction3
82: * pfnDLLFunction4 - pointer to DLLFunction4
83: * pfnDLLDialogBox - pointer to DLLDialogBox
84: *
85: * LOCAL VARS: hLib - handle of THE_DLL.DLL
86: * hSubMenu - handle of our "Options" submenu
87: *
88: * COMMENTS: Menuitems IDM_DLLFUNCTION1 through IDM_DLLDIALOGBOX are
89: * all processed by calling their respective functions in
90: * "THE_DLL.DLL". The "CreateThread" menuitem causes a thread
91: * to be created- the thread then calls into "THE_DLL.DLL".
92: *
93: \************************************************************************/
94:
95: LONG APIENTRY MainWndProc (HWND hwnd, UINT message, UINT wParam,
96: LONG lParam)
97: { static HANDLE hLib = NULL;
98: static HMENU hSubMenu;
99:
100: switch (message)
101: { case WM_CREATE:
102: { HMENU hMenu = GetMenu (hwnd);
103:
104: hSubMenu = GetSubMenu (hMenu, 0);
105: FixMenu (IDM_FREELIBRARY, hSubMenu);
106: break;
107: }
108: case WM_COMMAND:
109: switch (LOWORD(wParam))
110: { case IDM_LOADLIBRARY:
111: if (!hLib)
112: { if (!(hLib = LoadLibrary ("THE_DLL.DLL")))
113: MessageBox (hwnd,
114: "MainWndProc(): LoadLibrary () failed",
115: "Err! - LOADTEST", MB_OK | MB_ICONHAND);
116: else
117: { pfnDLLFunction1 = (PFNDLL) GetProcAddress (hLib,
118: "DLLFunction1");
119: pfnDLLFunction2 = (PFNDLL) GetProcAddress (hLib,
120: "DLLFunction2");
121: pfnDLLFunction3 = (PFNDLL) GetProcAddress (hLib,
122: "DLLFunction3");
123: pfnDLLFunction4 = (PFNDLL) GetProcAddress (hLib,
124: "DLLFunction4");
125: pfnDLLDialogBox = (PFNDLL) GetProcAddress (hLib,
126: "DLLDialogBox");
127: FixMenu (IDM_LOADLIBRARY, hSubMenu);
128: }
129: }
130: break;
131: case IDM_FREELIBRARY:
132: if (hLib)
133: { FreeLibrary (hLib);
134: pfnDLLFunction1 = NULL;
135: pfnDLLFunction2 = NULL;
136: pfnDLLFunction3 = NULL;
137: pfnDLLFunction4 = NULL;
138: pfnDLLDialogBox = NULL;
139: FixMenu (IDM_FREELIBRARY, hSubMenu);
140: hLib = NULL;
141: }
142: break;
143: case IDM_CREATETHREAD:
144: { DWORD tid; /* thread identifier */
145:
146: if (pfnDLLFunction1)
147: CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE)
148: ThreadProc, NULL, NULL, &tid);
149:
150: break;
151: }
152: case IDM_DLLFUNCTION1:
153: if (pfnDLLFunction1)
154: (pfnDLLFunction1) ();
155: break;
156: case IDM_DLLFUNCTION2:
157: if (pfnDLLFunction2)
158: (pfnDLLFunction2) ((int) 0);
159: break;
160: case IDM_DLLFUNCTION3:
161: if (pfnDLLFunction3)
162: (pfnDLLFunction3) ((HANDLE) NULL);
163: break;
164: case IDM_DLLFUNCTION4:
165: if (pfnDLLFunction4)
166: (pfnDLLFunction4) ((HWND) NULL);
167: break;
168: case IDM_DLLDIALOGBOX:
169: if (pfnDLLDialogBox)
170: (pfnDLLDialogBox) (hwnd);
171: break;
172: default:
173: break;
174: }
175: break;
176: case WM_DESTROY:
177: PostQuitMessage(NULL);
178: break;
179: default:
180: return (DefWindowProc(hwnd, message, wParam, lParam));
181: }
182: return (NULL);
183: }
184:
185:
186:
187: /************************************************************************\
188: *
189: * FUNCTION: Thread
190: *
191: * GLOBAL VARS: hwndMain - handle of main app window
192: * pfnDLLFunction1 - pointer to DLLFunction1
193: *
194: * COMMENTS: Makes a call into "THE_DLL.DLL", and then waits for the
195: * main window to call TerminateThread().
196: *
197: \************************************************************************/
198:
199: void ThreadProc ()
200: {
201: MessageBox (hwndMain, "calling DLLFunction1", "ThreadProc()", MB_OK);
202: (pfnDLLFunction1) ();
203: }
204:
205:
206:
207: /************************************************************************\
208: *
209: * FUNCTION: FixMenu
210: *
211: * INPUTS: choice - IDM_LOADLIBRARY or IBM_FREELIBRARY
212: * hSubMenu - handle of submenu, the items of which we'll
213: * enable/disable
214: *
215: * LOCAL VARS: i - loop variable
216: *
217: * COMMENTS: Enables/disables menuitems depending on whether THE_DLL
218: * is loaded/unloaded.
219: *
220: \************************************************************************/
221:
222: void FixMenu (UINT choice, HMENU hSubMenu)
223: { UINT i;
224:
225: if (choice == IDM_LOADLIBRARY)
226: {
227: EnableMenuItem (hSubMenu, IDM_LOADLIBRARY, MF_DISABLED | MF_GRAYED |
228: MF_BYCOMMAND);
229: for (i = IDM_FREELIBRARY; i <= IDM_DLLDIALOGBOX; i++)
230: EnableMenuItem (hSubMenu, i, MF_ENABLED | MF_BYCOMMAND);
231: }
232: else /* choice == IDM_FREELIBRARY */
233: {
234: EnableMenuItem (hSubMenu, IDM_LOADLIBRARY, MF_ENABLED | MF_BYCOMMAND);
235: for (i = IDM_FREELIBRARY; i <= IDM_DLLDIALOGBOX; i++)
236: EnableMenuItem (hSubMenu, i, MF_DISABLED | MF_GRAYED |MF_BYCOMMAND);
237:
238: }
239: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.