home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / module / getmod.c next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  6.0 KB  |  205 lines

  1. /*
  2.  
  3. Function(s) demonstrated in this program: GetModuleFileName, GetModuleHandle,
  4.   GetModuleUsage
  5.  
  6. Windows version:  2.03
  7.  
  8. Windows SDK version:  2.00
  9.  
  10. Compiler version:  C 5.10
  11.  
  12. Description:  This function returns the reference count of the module.
  13.  
  14. Additional Comments:
  15.  
  16. */
  17.  
  18. #define NOMINMAX
  19. #include <windows.h>
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include "GetMod.h"
  23.  
  24.  
  25. HWND     hWndParent1;
  26. HANDLE   hInstMain;
  27.  
  28. char     szOutputBuffer1 [70];
  29. char     szOutputBuffer2 [500];
  30.  
  31.  
  32. /****************************************************************************/
  33. /************************    Message Structure      *************************/
  34. /****************************************************************************/
  35.  
  36. struct { char *szMessage; }
  37.        Messages [] = {
  38. "About\0",
  39. "     This is a sample application to demonstrate the\n\
  40. use of the GetModuleFileName, GetModuleHandle, and\n\
  41. GetModuleUsage Windows functions.",
  42.  
  43. "Help Message",
  44. "     This program uses the GetModuleUsage Windows\n\
  45. function to get the reference count of the current\n\
  46. module.  To test this function, first start several\n\
  47. instances of this program.  Then use the menu to\n\
  48. invoke the function.  The count displayed should\n\
  49. directly represent the number of instances you have\n\
  50. started.",
  51.  
  52. };    
  53.  
  54. /****************************************************************************/
  55.  
  56. void ProcessMessage (HWND, int); 
  57.  
  58. void ProcessMessage (hWnd, MessageNumber) 
  59.      HWND     hWnd;
  60.      int      MessageNumber;
  61. {
  62.      sprintf (szOutputBuffer1, "%s", Messages [MessageNumber]);
  63.      sprintf (szOutputBuffer2, "%s", Messages [MessageNumber + 1]);
  64.      MessageBox (hWnd, szOutputBuffer2, szOutputBuffer1, MB_OK);
  65. }       
  66.  
  67. /****************************************************************************/
  68.  
  69. int PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  70.      HANDLE      hInstance, hPrevInstance ;
  71.      LPSTR       lpszCmdLine ;
  72.      int         nCmdShow ;
  73.      {
  74.      static char szAppName [] = "GetMod" ;
  75.      HWND        hWnd ;
  76.      WNDCLASS    wndclass ;
  77.      MSG msg;
  78.      short       xScreen, yScreen ;
  79.  
  80.      if (!hPrevInstance) 
  81.           {
  82.           wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  83.           wndclass.lpfnWndProc   = WndProc ;
  84.           wndclass.cbClsExtra    = 0 ;
  85.           wndclass.cbWndExtra    = 0 ;
  86.           wndclass.hInstance     = hInstance ;
  87.           wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  88.           wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  89.           wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  90.           wndclass.lpszMenuName  = szAppName ;
  91.           wndclass.lpszClassName = szAppName ;
  92.  
  93.           if (!RegisterClass (&wndclass))
  94.                return FALSE ;
  95.           }
  96.  
  97.      xScreen = GetSystemMetrics (SM_CXSCREEN) ;
  98.      yScreen = GetSystemMetrics (SM_CYSCREEN) ;
  99.  
  100.      hWndParent1 = CreateWindow (szAppName,     /* window class name       */
  101.                     "GetModuleUsage",           /* window caption          */
  102.                     WS_OVERLAPPEDWINDOW,        /* window style            */
  103.                     CW_USEDEFAULT,              /* initial x position      */
  104.                     0,                          /* initial y position      */
  105.                     CW_USEDEFAULT,              /* initial x size          */
  106.                     0,                          /* initial y size          */
  107.                     NULL,                       /* parent window handle    */
  108.                     NULL,                       /* window menu handle      */
  109.                     hInstance,                  /* program instance handle */
  110.                     NULL) ;                     /* create parameters       */
  111.  
  112.      ShowWindow (hWndParent1, nCmdShow) ;
  113.      UpdateWindow (hWndParent1) ;
  114.  
  115.      hInstMain = hInstance;
  116.  
  117.      while (GetMessage(&msg, NULL, 0, 0))
  118.      {
  119.       TranslateMessage(&msg);
  120.       DispatchMessage(&msg);
  121.      } 
  122.      return (msg.wParam) ;     
  123.      }
  124.  
  125. /****************************************************************************/
  126.  
  127. long FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
  128. HWND     hWnd ;
  129. unsigned iMessage ;
  130. WORD     wParam ;
  131. LONG     lParam ;
  132. {
  133.  int         nUsage;
  134.  HMENU       hMenu;
  135.  HDC         hDC;
  136.  PAINTSTRUCT ps;
  137.  HANDLE      hModule;
  138.  
  139.  switch(iMessage)
  140.  {
  141.   case WM_CREATE:
  142.        hMenu = GetSystemMenu (hWnd, FALSE);
  143.  
  144.        ChangeMenu (hMenu, NULL, "&About", IDM_ABOUT, 
  145.                    MF_APPEND | MF_STRING);
  146.        break;
  147.  
  148.   case WM_SYSCOMMAND:
  149.        switch (wParam) {
  150.           case IDM_ABOUT:
  151.                ProcessMessage (hWnd, 0);
  152.                break;
  153.           default:
  154.                return DefWindowProc (hWnd, iMessage, wParam, lParam) ;
  155.        }
  156.        break;
  157.  
  158.   case WM_COMMAND:
  159.        switch (wParam) {
  160.           case IDM_GETMODF:
  161.                GetModuleFileName(GetModuleHandle((LPSTR)"GetMod"), 
  162.                                  szOutputBuffer1, 70);
  163.                MessageBox (hWnd, szOutputBuffer1, "GetModuleFileName", MB_OK);
  164.                break;
  165.  
  166.           case IDM_GETMODH:
  167.                hModule = GetModuleHandle ("GetMod");
  168.                sprintf (szOutputBuffer1, "Module handle = %x", hModule);
  169.                MessageBox (hWnd, szOutputBuffer1, "GetModuleHandle", MB_OK);
  170.                break;
  171.  
  172.           case IDM_GETMODU:
  173.                nUsage = GetModuleUsage (hInstMain);
  174.                sprintf (szOutputBuffer1, 
  175.                         "The module usage of the current module = %i",nUsage);
  176.                MessageBox (hWnd, szOutputBuffer1, "GetModuleUsage", MB_OK);
  177.                break;
  178.  
  179.  
  180.           case IDM_HELP:
  181.                ProcessMessage (hWnd, 2);
  182.                break;
  183.        }
  184.        break;
  185.  
  186.   case WM_PAINT:
  187.        BeginPaint(hWnd, (LPPAINTSTRUCT)&ps);
  188.        EndPaint(hWnd, (LPPAINTSTRUCT)&ps);
  189.        break;
  190.  
  191.   case WM_DESTROY:
  192.        PostQuitMessage(0);
  193.        break;
  194.  
  195.   default:
  196.   {
  197.    return DefWindowProc (hWnd, iMessage, wParam, lParam) ;
  198.   }
  199.  }
  200.  return (0L); 
  201. }
  202.  
  203.  
  204.  
  205.