Annotation of q_a/samples/simpldll/loadtest.c, revision 1.1.1.3

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.