|
|
1.1.1.2 ! root 1: /******************************************************************************\ 1.1 root 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: * 1.1.1.2 ! root 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: * AboutDlgProc- processes about dialog messages 1.1 root 12: * 1.1.1.2 ! root 13: * ! 14: * Microsoft Developer Support ! 15: * Copyright (c) 1992 Microsoft Corporation ! 16: * ! 17: \******************************************************************************/ 1.1 root 18: 19: #include <windows.h> 20: #include "loadtest.h" 21: #include "the_dll.h" 22: 23: 24: 1.1.1.2 ! root 25: /******************************************************************************\ 1.1 root 26: * 27: * FUNCTION: WinMain (standard WinMain INPUTS/RETURNS) 28: * 1.1.1.2 ! root 29: * GLOBAL VARS: ghwndMain - handle of main app window 1.1 root 30: * 31: * LOCAL VARS: msg - msg to get/dispatch 32: * 1.1.1.2 ! root 33: \******************************************************************************/ 1.1 root 34: 1.1.1.2 ! root 35: int WINAPI WinMain (HANDLE hInstance,HANDLE hPrevInstance, ! 36: LPSTR lpCmdLine, int nCmdShow) 1.1 root 37: { MSG msg; 38: 39: if (!hPrevInstance) 40: { 41: WNDCLASS wc; 42: 43: wc.style = NULL; 44: wc.lpfnWndProc = (WNDPROC)MainWndProc; 45: wc.cbClsExtra = 0; 46: wc.cbWndExtra = 0; 47: wc.hInstance = hInstance; 48: wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); 49: wc.hCursor = LoadCursor (NULL, IDC_ARROW); 50: wc.hbrBackground = GetStockObject (WHITE_BRUSH); 1.1.1.2 ! root 51: wc.lpszMenuName = (LPCTSTR) "Menu"; ! 52: wc.lpszClassName = (LPCTSTR) "LOADTEST"; 1.1 root 53: 54: if (!RegisterClass (&wc)) 1.1.1.2 ! root 55: { ! 56: MessageBox (NULL, (LPCTSTR) "WinMain(): RegisterClass() failed", ! 57: (LPCTSTR) "Err! - LOADTEST", MB_OK | MB_ICONEXCLAMATION); 1.1 root 58: return(FALSE); 59: } 60: } 61: 1.1.1.2 ! root 62: if (!(ghwndMain = CreateWindow ("LOADTEST", "LOADTEST Sample Application", ! 63: WS_OVERLAPPEDWINDOW, ! 64: CW_USEDEFAULT, CW_USEDEFAULT, ! 65: CW_USEDEFAULT, CW_USEDEFAULT, ! 66: NULL, NULL, hInstance, NULL))) 1.1 root 67: return (NULL); 68: 1.1.1.2 ! root 69: ShowWindow (ghwndMain, nCmdShow); 1.1 root 70: 1.1.1.2 ! root 71: while (GetMessage (&msg, NULL, NULL, NULL)) ! 72: { ! 73: TranslateMessage (&msg); ! 74: DispatchMessage (&msg); 1.1 root 75: } 1.1.1.2 ! root 76: 1.1 root 77: return (msg.wParam); 78: } 79: 80: 81: 1.1.1.2 ! root 82: /******************************************************************************\ 1.1 root 83: * 84: * FUNCTION: MainWndProc (standard window procedure INPUTS/RETURNS) 85: * 86: * GLOBAL VARS: pfnDLLFunction1 - pointer to DLLFunction1 87: * pfnDLLFunction2 - pointer to DLLFunction2 88: * pfnDLLFunction3 - pointer to DLLFunction3 89: * pfnDLLFunction4 - pointer to DLLFunction4 90: * pfnDLLDialogBox - pointer to DLLDialogBox 91: * 92: * LOCAL VARS: hLib - handle of THE_DLL.DLL 93: * hSubMenu - handle of our "Options" submenu 94: * 95: * COMMENTS: Menuitems IDM_DLLFUNCTION1 through IDM_DLLDIALOGBOX are 96: * all processed by calling their respective functions in 97: * "THE_DLL.DLL". The "CreateThread" menuitem causes a thread 98: * to be created- the thread then calls into "THE_DLL.DLL". 99: * 1.1.1.2 ! root 100: \******************************************************************************/ 1.1 root 101: 1.1.1.2 ! root 102: LRESULT CALLBACK MainWndProc (HWND hwnd, UINT message, WPARAM wParam, ! 103: LPARAM lParam) 1.1 root 104: { static HANDLE hLib = NULL; 105: static HMENU hSubMenu; 106: 107: switch (message) 1.1.1.2 ! root 108: { ! 109: case WM_CREATE: ! 110: { ! 111: HMENU hMenu = GetMenu (hwnd); 1.1 root 112: 113: hSubMenu = GetSubMenu (hMenu, 0); 114: FixMenu (IDM_FREELIBRARY, hSubMenu); 115: break; 116: } 1.1.1.2 ! root 117: 1.1 root 118: case WM_COMMAND: 1.1.1.2 ! root 119: 1.1 root 120: switch (LOWORD(wParam)) 1.1.1.2 ! root 121: { ! 122: case IDM_LOADLIBRARY: ! 123: 1.1 root 124: if (!hLib) 1.1.1.2 ! root 125: { ! 126: if (!(hLib = LoadLibrary ("THE_DLL.DLL"))) ! 127: 1.1 root 128: MessageBox (hwnd, 1.1.1.2 ! root 129: (LPCTSTR) "MainWndProc(): LoadLibrary () failed", ! 130: (LPCTSTR) "Err! - LOADTEST", ! 131: MB_OK | MB_ICONEXCLAMATION); 1.1 root 132: else 1.1.1.2 ! root 133: { ! 134: gpfnDLLFunction1 = (PFNDLL) GetProcAddress (hLib, ! 135: "DLLFunction1"); ! 136: gpfnDLLFunction2 = (PFNDLL) GetProcAddress (hLib, ! 137: "DLLFunction2"); ! 138: gpfnDLLFunction3 = (PFNDLL) GetProcAddress (hLib, ! 139: "DLLFunction3"); ! 140: gpfnDLLFunction4 = (PFNDLL) GetProcAddress (hLib, ! 141: "DLLFunction4"); ! 142: gpfnDLLDialogBox = (PFNDLL) GetProcAddress (hLib, ! 143: "DLLDialogBox"); 1.1 root 144: FixMenu (IDM_LOADLIBRARY, hSubMenu); 145: } 146: } 147: break; 1.1.1.2 ! root 148: 1.1 root 149: case IDM_FREELIBRARY: 1.1.1.2 ! root 150: 1.1 root 151: if (hLib) 1.1.1.2 ! root 152: { ! 153: FreeLibrary (hLib); ! 154: gpfnDLLFunction1 = NULL; ! 155: gpfnDLLFunction2 = NULL; ! 156: gpfnDLLFunction3 = NULL; ! 157: gpfnDLLFunction4 = NULL; ! 158: gpfnDLLDialogBox = NULL; 1.1 root 159: FixMenu (IDM_FREELIBRARY, hSubMenu); 160: hLib = NULL; 161: } 162: break; 1.1.1.2 ! root 163: 1.1 root 164: case IDM_CREATETHREAD: 1.1.1.2 ! root 165: { ! 166: DWORD tid; /* thread identifier */ ! 167: ! 168: if (gpfnDLLFunction1) 1.1 root 169: 170: CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE) 171: ThreadProc, NULL, NULL, &tid); 172: 173: break; 174: } 1.1.1.2 ! root 175: 1.1 root 176: case IDM_DLLFUNCTION1: 1.1.1.2 ! root 177: ! 178: if (gpfnDLLFunction1) ! 179: ! 180: (gpfnDLLFunction1) (); ! 181: 1.1 root 182: break; 1.1.1.2 ! root 183: 1.1 root 184: case IDM_DLLFUNCTION2: 1.1.1.2 ! root 185: ! 186: if (gpfnDLLFunction2) ! 187: ! 188: (gpfnDLLFunction2) ((int) 0); ! 189: 1.1 root 190: break; 1.1.1.2 ! root 191: 1.1 root 192: case IDM_DLLFUNCTION3: 1.1.1.2 ! root 193: ! 194: if (gpfnDLLFunction3) ! 195: ! 196: (gpfnDLLFunction3) ((HANDLE) NULL); ! 197: 1.1 root 198: break; 1.1.1.2 ! root 199: 1.1 root 200: case IDM_DLLFUNCTION4: 1.1.1.2 ! root 201: ! 202: if (gpfnDLLFunction4) ! 203: ! 204: (gpfnDLLFunction4) ((HWND) NULL); ! 205: 1.1 root 206: break; 1.1.1.2 ! root 207: 1.1 root 208: case IDM_DLLDIALOGBOX: 1.1.1.2 ! root 209: ! 210: if (gpfnDLLDialogBox) ! 211: ! 212: (gpfnDLLDialogBox) (hwnd); ! 213: ! 214: break; ! 215: ! 216: case IDM_ABOUT: ! 217: ! 218: DialogBox (GetModuleHandle (NULL), (LPCTSTR)"About", hwnd, ! 219: (DLGPROC) AboutDlgProc); 1.1 root 220: break; 1.1.1.2 ! root 221: 1.1 root 222: default: 1.1.1.2 ! root 223: 1.1 root 224: break; 225: } 226: break; 1.1.1.2 ! root 227: 1.1 root 228: case WM_DESTROY: 1.1.1.2 ! root 229: 1.1 root 230: PostQuitMessage(NULL); 231: break; 1.1.1.2 ! root 232: 1.1 root 233: default: 1.1.1.2 ! root 234: 1.1 root 235: return (DefWindowProc(hwnd, message, wParam, lParam)); 236: } 237: return (NULL); 238: } 239: 240: 241: 1.1.1.2 ! root 242: /******************************************************************************\ 1.1 root 243: * 244: * FUNCTION: Thread 245: * 1.1.1.2 ! root 246: * GLOBAL VARS: ghwndMain - handle of main app window 1.1 root 247: * pfnDLLFunction1 - pointer to DLLFunction1 248: * 249: * COMMENTS: Makes a call into "THE_DLL.DLL", and then waits for the 250: * main window to call TerminateThread(). 251: * 1.1.1.2 ! root 252: \******************************************************************************/ 1.1 root 253: 254: void ThreadProc () 255: { 1.1.1.2 ! root 256: MessageBox (ghwndMain, (LPCTSTR) "calling DLLFunction1", ! 257: (LPCTSTR) "ThreadProc()", MB_OK); ! 258: (gpfnDLLFunction1) (); 1.1 root 259: } 260: 261: 262: 1.1.1.2 ! root 263: /******************************************************************************\ 1.1 root 264: * 265: * FUNCTION: FixMenu 266: * 267: * INPUTS: choice - IDM_LOADLIBRARY or IBM_FREELIBRARY 268: * hSubMenu - handle of submenu, the items of which we'll 269: * enable/disable 270: * 271: * LOCAL VARS: i - loop variable 272: * 273: * COMMENTS: Enables/disables menuitems depending on whether THE_DLL 274: * is loaded/unloaded. 275: * 1.1.1.2 ! root 276: \******************************************************************************/ 1.1 root 277: 278: void FixMenu (UINT choice, HMENU hSubMenu) 1.1.1.2 ! root 279: { ! 280: UINT i; 1.1 root 281: 282: if (choice == IDM_LOADLIBRARY) 283: { 284: EnableMenuItem (hSubMenu, IDM_LOADLIBRARY, MF_DISABLED | MF_GRAYED | 285: MF_BYCOMMAND); 1.1.1.2 ! root 286: 1.1 root 287: for (i = IDM_FREELIBRARY; i <= IDM_DLLDIALOGBOX; i++) 1.1.1.2 ! root 288: 1.1 root 289: EnableMenuItem (hSubMenu, i, MF_ENABLED | MF_BYCOMMAND); 290: } 291: else /* choice == IDM_FREELIBRARY */ 292: { 293: EnableMenuItem (hSubMenu, IDM_LOADLIBRARY, MF_ENABLED | MF_BYCOMMAND); 1.1.1.2 ! root 294: 1.1 root 295: for (i = IDM_FREELIBRARY; i <= IDM_DLLDIALOGBOX; i++) 1.1.1.2 ! root 296: 1.1 root 297: EnableMenuItem (hSubMenu, i, MF_DISABLED | MF_GRAYED |MF_BYCOMMAND); 298: 299: } 300: } 1.1.1.2 ! root 301: ! 302: ! 303: ! 304: /******************************************************************************\ ! 305: * ! 306: * FUNCTION: AboutDlgProc (standard dialog procedure INPUTS/RETURNS) ! 307: * ! 308: * COMMENTS: Displays "about" message ! 309: * ! 310: \******************************************************************************/ ! 311: ! 312: LRESULT CALLBACK AboutDlgProc (HWND hwnd, UINT message, WPARAM wParam, ! 313: LPARAM lParam) ! 314: { switch (message) ! 315: { ! 316: case WM_COMMAND: ! 317: ! 318: if (LOWORD(wParam) == IDOK) ! 319: { ! 320: EndDialog(hwnd, TRUE); ! 321: return (TRUE); ! 322: } ! 323: return (TRUE); ! 324: } ! 325: return (FALSE); ! 326: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.