--- q_a/samples/simpldll/loadtest.c 2018/08/09 18:29:19 1.1 +++ q_a/samples/simpldll/loadtest.c 2018/08/09 18:29:28 1.1.1.2 @@ -1,15 +1,20 @@ -/************************************************************************\ +/******************************************************************************\ * * PROGRAM: LOADTEST.C * * PURPOSE: A simple demonstration of LoadLibrary()-ing a DLL at * runtime and obtaining function addresses within the DLL. * -* FUNTIONS: WinMain() - initialization, create window, msg loop -* MainWndProc() - processes main window msgs -* ThreadProc() - makes a single call into "THE_DLL.DLL" +* FUNTIONS: WinMain - initialization, create window, msg loop +* MainWndProc - processes main window msgs +* ThreadProc - makes a single call into "THE_DLL.DLL" +* AboutDlgProc- processes about dialog messages * -\************************************************************************/ +* +* Microsoft Developer Support +* Copyright (c) 1992 Microsoft Corporation +* +\******************************************************************************/ #include #include "loadtest.h" @@ -17,18 +22,18 @@ -/************************************************************************\ +/******************************************************************************\ * * FUNCTION: WinMain (standard WinMain INPUTS/RETURNS) * -* GLOBAL VARS: hwndMain - handle of main app window +* GLOBAL VARS: ghwndMain - handle of main app window * * LOCAL VARS: msg - msg to get/dispatch * -\************************************************************************/ +\******************************************************************************/ -int APIENTRY WinMain (HANDLE hInstance,HANDLE hPrevInstance, - LPSTR lpCmdLine, int nCmdShow) +int WINAPI WinMain (HANDLE hInstance,HANDLE hPrevInstance, + LPSTR lpCmdLine, int nCmdShow) { MSG msg; if (!hPrevInstance) @@ -43,36 +48,38 @@ int APIENTRY WinMain (HANDLE hInstance,H wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor (NULL, IDC_ARROW); wc.hbrBackground = GetStockObject (WHITE_BRUSH); - wc.lpszMenuName = (LPSTR) "Menu"; - wc.lpszClassName = (LPSTR) "LOADTEST"; + wc.lpszMenuName = (LPCTSTR) "Menu"; + wc.lpszClassName = (LPCTSTR) "LOADTEST"; if (!RegisterClass (&wc)) - { MessageBox (NULL, "WinMain(): RegisterClass() failed", - "Err! - LOADTEST", MB_OK | MB_ICONHAND); + { + MessageBox (NULL, (LPCTSTR) "WinMain(): RegisterClass() failed", + (LPCTSTR) "Err! - LOADTEST", MB_OK | MB_ICONEXCLAMATION); return(FALSE); } } - if (!(hwndMain = CreateWindow ("LOADTEST", "LOADTEST Sample Application", - WS_OVERLAPPEDWINDOW, - CW_USEDEFAULT, CW_USEDEFAULT, - CW_USEDEFAULT, CW_USEDEFAULT, - NULL, NULL, hInstance, NULL))) + if (!(ghwndMain = CreateWindow ("LOADTEST", "LOADTEST Sample Application", + WS_OVERLAPPEDWINDOW, + CW_USEDEFAULT, CW_USEDEFAULT, + CW_USEDEFAULT, CW_USEDEFAULT, + NULL, NULL, hInstance, NULL))) return (NULL); - ShowWindow(hwndMain, nCmdShow); + ShowWindow (ghwndMain, nCmdShow); - while (GetMessage(&msg, NULL, NULL, NULL)) - { TranslateMessage(&msg); - DispatchMessage(&msg); + while (GetMessage (&msg, NULL, NULL, NULL)) + { + TranslateMessage (&msg); + DispatchMessage (&msg); } + return (msg.wParam); - UNREFERENCED_PARAMETER(lpCmdLine); } -/************************************************************************\ +/******************************************************************************\ * * FUNCTION: MainWndProc (standard window procedure INPUTS/RETURNS) * @@ -90,93 +97,141 @@ int APIENTRY WinMain (HANDLE hInstance,H * "THE_DLL.DLL". The "CreateThread" menuitem causes a thread * to be created- the thread then calls into "THE_DLL.DLL". * -\************************************************************************/ +\******************************************************************************/ -LONG APIENTRY MainWndProc (HWND hwnd, UINT message, UINT wParam, - LONG lParam) +LRESULT CALLBACK MainWndProc (HWND hwnd, UINT message, WPARAM wParam, + LPARAM lParam) { static HANDLE hLib = NULL; static HMENU hSubMenu; switch (message) - { case WM_CREATE: - { HMENU hMenu = GetMenu (hwnd); + { + case WM_CREATE: + { + HMENU hMenu = GetMenu (hwnd); hSubMenu = GetSubMenu (hMenu, 0); FixMenu (IDM_FREELIBRARY, hSubMenu); break; } + case WM_COMMAND: + switch (LOWORD(wParam)) - { case IDM_LOADLIBRARY: + { + case IDM_LOADLIBRARY: + if (!hLib) - { if (!(hLib = LoadLibrary ("THE_DLL.DLL"))) + { + if (!(hLib = LoadLibrary ("THE_DLL.DLL"))) + MessageBox (hwnd, - "MainWndProc(): LoadLibrary () failed", - "Err! - LOADTEST", MB_OK | MB_ICONHAND); + (LPCTSTR) "MainWndProc(): LoadLibrary () failed", + (LPCTSTR) "Err! - LOADTEST", + MB_OK | MB_ICONEXCLAMATION); else - { pfnDLLFunction1 = (PFNDLL) GetProcAddress (hLib, - "DLLFunction1"); - pfnDLLFunction2 = (PFNDLL) GetProcAddress (hLib, - "DLLFunction2"); - pfnDLLFunction3 = (PFNDLL) GetProcAddress (hLib, - "DLLFunction3"); - pfnDLLFunction4 = (PFNDLL) GetProcAddress (hLib, - "DLLFunction4"); - pfnDLLDialogBox = (PFNDLL) GetProcAddress (hLib, - "DLLDialogBox"); + { + gpfnDLLFunction1 = (PFNDLL) GetProcAddress (hLib, + "DLLFunction1"); + gpfnDLLFunction2 = (PFNDLL) GetProcAddress (hLib, + "DLLFunction2"); + gpfnDLLFunction3 = (PFNDLL) GetProcAddress (hLib, + "DLLFunction3"); + gpfnDLLFunction4 = (PFNDLL) GetProcAddress (hLib, + "DLLFunction4"); + gpfnDLLDialogBox = (PFNDLL) GetProcAddress (hLib, + "DLLDialogBox"); FixMenu (IDM_LOADLIBRARY, hSubMenu); } } break; + case IDM_FREELIBRARY: + if (hLib) - { FreeLibrary (hLib); - pfnDLLFunction1 = NULL; - pfnDLLFunction2 = NULL; - pfnDLLFunction3 = NULL; - pfnDLLFunction4 = NULL; - pfnDLLDialogBox = NULL; + { + FreeLibrary (hLib); + gpfnDLLFunction1 = NULL; + gpfnDLLFunction2 = NULL; + gpfnDLLFunction3 = NULL; + gpfnDLLFunction4 = NULL; + gpfnDLLDialogBox = NULL; FixMenu (IDM_FREELIBRARY, hSubMenu); hLib = NULL; } break; + case IDM_CREATETHREAD: - { DWORD tid; /* thread identifier */ + { + DWORD tid; /* thread identifier */ + + if (gpfnDLLFunction1) - if (pfnDLLFunction1) CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE) ThreadProc, NULL, NULL, &tid); break; } + case IDM_DLLFUNCTION1: - if (pfnDLLFunction1) - (pfnDLLFunction1) (); + + if (gpfnDLLFunction1) + + (gpfnDLLFunction1) (); + break; + case IDM_DLLFUNCTION2: - if (pfnDLLFunction2) - (pfnDLLFunction2) ((int) 0); + + if (gpfnDLLFunction2) + + (gpfnDLLFunction2) ((int) 0); + break; + case IDM_DLLFUNCTION3: - if (pfnDLLFunction3) - (pfnDLLFunction3) ((HANDLE) NULL); + + if (gpfnDLLFunction3) + + (gpfnDLLFunction3) ((HANDLE) NULL); + break; + case IDM_DLLFUNCTION4: - if (pfnDLLFunction4) - (pfnDLLFunction4) ((HWND) NULL); + + if (gpfnDLLFunction4) + + (gpfnDLLFunction4) ((HWND) NULL); + break; + case IDM_DLLDIALOGBOX: - if (pfnDLLDialogBox) - (pfnDLLDialogBox) (hwnd); + + if (gpfnDLLDialogBox) + + (gpfnDLLDialogBox) (hwnd); + + break; + + case IDM_ABOUT: + + DialogBox (GetModuleHandle (NULL), (LPCTSTR)"About", hwnd, + (DLGPROC) AboutDlgProc); break; + default: + break; } break; + case WM_DESTROY: + PostQuitMessage(NULL); break; + default: + return (DefWindowProc(hwnd, message, wParam, lParam)); } return (NULL); @@ -184,27 +239,28 @@ LONG APIENTRY MainWndProc (HWND hwnd, UI -/************************************************************************\ +/******************************************************************************\ * * FUNCTION: Thread * -* GLOBAL VARS: hwndMain - handle of main app window +* GLOBAL VARS: ghwndMain - handle of main app window * pfnDLLFunction1 - pointer to DLLFunction1 * * COMMENTS: Makes a call into "THE_DLL.DLL", and then waits for the * main window to call TerminateThread(). * -\************************************************************************/ +\******************************************************************************/ void ThreadProc () { - MessageBox (hwndMain, "calling DLLFunction1", "ThreadProc()", MB_OK); - (pfnDLLFunction1) (); + MessageBox (ghwndMain, (LPCTSTR) "calling DLLFunction1", + (LPCTSTR) "ThreadProc()", MB_OK); + (gpfnDLLFunction1) (); } -/************************************************************************\ +/******************************************************************************\ * * FUNCTION: FixMenu * @@ -217,23 +273,54 @@ void ThreadProc () * COMMENTS: Enables/disables menuitems depending on whether THE_DLL * is loaded/unloaded. * -\************************************************************************/ +\******************************************************************************/ void FixMenu (UINT choice, HMENU hSubMenu) -{ UINT i; +{ + UINT i; if (choice == IDM_LOADLIBRARY) { EnableMenuItem (hSubMenu, IDM_LOADLIBRARY, MF_DISABLED | MF_GRAYED | MF_BYCOMMAND); + for (i = IDM_FREELIBRARY; i <= IDM_DLLDIALOGBOX; i++) + EnableMenuItem (hSubMenu, i, MF_ENABLED | MF_BYCOMMAND); } else /* choice == IDM_FREELIBRARY */ { EnableMenuItem (hSubMenu, IDM_LOADLIBRARY, MF_ENABLED | MF_BYCOMMAND); + for (i = IDM_FREELIBRARY; i <= IDM_DLLDIALOGBOX; i++) + EnableMenuItem (hSubMenu, i, MF_DISABLED | MF_GRAYED |MF_BYCOMMAND); } } + + + +/******************************************************************************\ +* +* FUNCTION: AboutDlgProc (standard dialog procedure INPUTS/RETURNS) +* +* COMMENTS: Displays "about" message +* +\******************************************************************************/ + +LRESULT CALLBACK AboutDlgProc (HWND hwnd, UINT message, WPARAM wParam, + LPARAM lParam) +{ switch (message) + { + case WM_COMMAND: + + if (LOWORD(wParam) == IDOK) + { + EndDialog(hwnd, TRUE); + return (TRUE); + } + return (TRUE); + } + return (FALSE); +}