home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / dialog / procinst.c < prev    next >
C/C++ Source or Header  |  1988-08-10  |  3KB  |  125 lines

  1. /*
  2.  *
  3.  *  FreeProcInstance
  4.  *  
  5.  *  This program demonstrates the use of the function FreeProcInstance.  
  6.  *  This function frees the function specified bye the parameter from the
  7.  *  data segment bound to it by the MakeProcInstance function.
  8.  *  
  9.  *  Other references: DlgOpen.c, DlgSave.c, Print.c
  10.  */
  11.  
  12. #include <windows.h>
  13. #include "procinst.h"
  14.  
  15. typedef struct  {
  16.   int    nDummy;
  17. } SETUPDATA;
  18.  
  19. static SETUPDATA strSetUpData;
  20. static HANDLE hInst;
  21. static char    szFileName[] = "procinst";
  22. static char    szFuncName[] = "FreeProcInstance";
  23.  
  24. long    FAR PASCAL WndProc (HANDLE, unsigned, WORD, LONG);
  25. BOOL FAR PASCAL TestFarFunc ();
  26.  
  27. int    PASCAL WinMain(hInstance, hPrevInstance, lpszCmdLine, cmdShow)
  28. HANDLE hInstance, hPrevInstance;
  29. LPSTR  lpszCmdLine;
  30. int    cmdShow;
  31. {
  32.   HWND hWnd;
  33.   MSG  msg;
  34.   if (!hPrevInstance)
  35.   {
  36.     WNDCLASS rClass;
  37.  
  38.     rClass.style         = CS_HREDRAW | CS_VREDRAW;
  39.     rClass.lpfnWndProc   = WndProc;
  40.     rClass.cbClsExtra    = 0;
  41.     rClass.cbWndExtra    = 0;
  42.     rClass.hInstance     = hInstance;
  43.     rClass.hCursor       = LoadCursor (NULL, IDC_ARROW);
  44.     rClass.hIcon         = LoadIcon (hInstance, IDI_APPLICATION);
  45.     rClass.hbrBackground = GetStockObject (WHITE_BRUSH);
  46.     rClass.lpszMenuName  = (LPSTR) NULL;
  47.     rClass.lpszClassName = (LPSTR) szFileName;
  48.  
  49.     RegisterClass ((LPWNDCLASS) & rClass);
  50.   }
  51.   else
  52.     GetInstanceData (hPrevInstance, (PSTR) & strSetUpData,
  53.         sizeof (SETUPDATA));
  54.  
  55.   hInst = hInstance;
  56.  
  57.   hWnd = CreateWindow ((LPSTR) szFileName, (LPSTR) szFuncName,
  58.       WS_OVERLAPPEDWINDOW,
  59.       CW_USEDEFAULT, 0,
  60.       CW_USEDEFAULT, 0,
  61.       NULL, NULL, hInstance, NULL);
  62.  
  63.   ShowWindow (hWnd, cmdShow);
  64.   UpdateWindow (hWnd);
  65.  
  66.   while (GetMessage((LPMSG) & msg, NULL, 0, 0))
  67.   {
  68.     TranslateMessage((LPMSG) & msg);
  69.     DispatchMessage((LPMSG) & msg);
  70.   }
  71.   exit(msg.wParam);
  72. }
  73.  
  74.  
  75. BOOL FAR PASCAL TestFarFunc ()
  76. {
  77.   return TRUE;
  78. }
  79.  
  80. long FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
  81. HWND     hWnd ;
  82. unsigned iMessage ;
  83. WORD     wParam ;
  84. LONG     lParam ;
  85. {
  86.  static HANDLE hInstance;
  87.  FARPROC  lpProc;
  88.  HMENU hMenu;
  89.  switch(iMessage)
  90.  {
  91.   case WM_CREATE:
  92.   {
  93.    hInstance = GetWindowWord(hWnd, GWW_HINSTANCE);
  94.    hMenu = LoadMenu(hInstance, "ResMenu");
  95.    SetMenu(hWnd, hMenu);
  96.    DrawMenuBar(hWnd);
  97.    break;
  98.   }
  99.   case WM_COMMAND:
  100.   {
  101.    switch(wParam)
  102.    {
  103.     case IDM_EXECUTE:
  104.     {
  105.     lpProc = MakeProcInstance ((FARPROC) TestFarFunc, hInst);
  106.     MessageBox (NULL, (LPSTR)"Freeing procedure instance", (LPSTR)szFuncName, MB_OK);
  107.     FreeProcInstance (lpProc);
  108.     MessageBox (NULL, (LPSTR)"Instance procedure freed", (LPSTR)szFuncName, MB_OK);
  109.     }
  110.    }
  111.    break;
  112.   }
  113.   case WM_DESTROY:
  114.   {
  115.    PostQuitMessage(0);
  116.    break;
  117.   }
  118.   default:
  119.   {
  120.    return DefWindowProc (hWnd, iMessage, wParam, lParam) ;
  121.   }
  122.  }
  123.  return (0L); 
  124. }
  125.