home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / apps / getpaddr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  6.1 KB  |  208 lines

  1. /*
  2.  
  3. Function(s) demonstrated in this program: GetProcAddress, LoadLibrary, 
  4.    FreeLibrary
  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 a handle to the given library function.
  13.  
  14. Additional Comments:
  15.  
  16. */
  17.  
  18. #define NOMINMAX
  19. #include <windows.h>
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include "GetPAddr.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 GetProcAddress, LoadLibrary, and FreeLibrary\n\
  41. Windows functions.",
  42.  
  43. "Help Message",
  44. "     This program uses the GetProcAddress Windows\n\
  45. function to get a handle to the StartSelection\n\
  46. function in the SELECT library.  This address is\n\
  47. then displayed in a message box.  Use the menu to\n\
  48. invoke this function.",
  49.  
  50. };    
  51.  
  52. /****************************************************************************/
  53.  
  54. void ProcessMessage (HWND, int); 
  55.  
  56. void ProcessMessage (hWnd, MessageNumber) 
  57.      HWND     hWnd;
  58.      int      MessageNumber;
  59. {
  60.      sprintf (szOutputBuffer1, "%s", Messages [MessageNumber]);
  61.      sprintf (szOutputBuffer2, "%s", Messages [MessageNumber + 1]);
  62.      MessageBox (hWnd, szOutputBuffer2, szOutputBuffer1, MB_OK);
  63. }       
  64.  
  65. /****************************************************************************/
  66.  
  67. int PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  68.      HANDLE      hInstance, hPrevInstance ;
  69.      LPSTR       lpszCmdLine ;
  70.      int         nCmdShow ;
  71.      {
  72.      static char szAppName [] = "GetPAddr" ;
  73.      HWND        hWnd ;
  74.      WNDCLASS    wndclass ;
  75.      MSG msg;
  76.      short       xScreen, yScreen ;
  77.  
  78.      if (!hPrevInstance) 
  79.           {
  80.           wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  81.           wndclass.lpfnWndProc   = WndProc ;
  82.           wndclass.cbClsExtra    = 0 ;
  83.           wndclass.cbWndExtra    = 0 ;
  84.           wndclass.hInstance     = hInstance ;
  85.           wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  86.           wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  87.           wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  88.           wndclass.lpszMenuName  = szAppName ;
  89.           wndclass.lpszClassName = szAppName ;
  90.  
  91.           if (!RegisterClass (&wndclass))
  92.                return FALSE ;
  93.           }
  94.  
  95.      xScreen = GetSystemMetrics (SM_CXSCREEN) ;
  96.      yScreen = GetSystemMetrics (SM_CYSCREEN) ;
  97.  
  98.      hWndParent1 = CreateWindow (szAppName,     /* window class name       */
  99.                     "GetProcAddress",           /* window caption          */
  100.                     WS_OVERLAPPEDWINDOW,        /* window style            */
  101.                     CW_USEDEFAULT,              /* initial x position      */
  102.                     0,                          /* initial y position      */
  103.                     CW_USEDEFAULT,              /* initial x size          */
  104.                     0,                          /* initial y size          */
  105.                     NULL,                       /* parent window handle    */
  106.                     NULL,                       /* window menu handle      */
  107.                     hInstance,                  /* program instance handle */
  108.                     NULL) ;                     /* create parameters       */
  109.  
  110.      ShowWindow (hWndParent1, nCmdShow) ;
  111.      UpdateWindow (hWndParent1) ;
  112.  
  113.      hInstMain = hInstance;
  114.  
  115.      while (GetMessage(&msg, NULL, 0, 0))
  116.      {
  117.       TranslateMessage(&msg);
  118.       DispatchMessage(&msg);
  119.      } 
  120.      return (msg.wParam) ;     
  121.      }
  122.  
  123. /****************************************************************************/
  124.  
  125. long FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
  126. HWND     hWnd ;
  127. unsigned iMessage ;
  128. WORD     wParam ;
  129. LONG     lParam ;
  130. {
  131.  HMENU       hMenu;
  132.  HDC         hDC;
  133.  PAINTSTRUCT ps;
  134.  static int  xClient, yClient;
  135.  HANDLE      hLibModule;
  136.  FARPROC     lpAddress;
  137.  
  138.  switch(iMessage)
  139.  {
  140.   case WM_CREATE:
  141.        hMenu = GetSystemMenu (hWnd, FALSE);
  142.  
  143.        ChangeMenu (hMenu, NULL, "&About", IDM_ABOUT, 
  144.                    MF_APPEND | MF_STRING);
  145.        break;
  146.  
  147.   case WM_SYSCOMMAND:
  148.        switch (wParam) {
  149.           case IDM_ABOUT:
  150.                ProcessMessage (hWnd, 0);
  151.                break;
  152.           default:
  153.                return DefWindowProc (hWnd, iMessage, wParam, lParam) ;
  154.        }
  155.        break;
  156.  
  157.   case WM_COMMAND:
  158.        switch (wParam) {
  159.           case IDM_GETPROCADDRESS:
  160.                hLibModule = LoadLibrary( "select" );
  161.              
  162.                MessageBox (NULL, 
  163.                  (LPSTR)"Retrieving address of the StartSelection function.",
  164.                  (LPSTR)"GetProcAddress", MB_OK);
  165.              
  166.                lpAddress = GetProcAddress( hLibModule, "StartSelection" );
  167.              
  168.                if ( lpAddress != NULL ) {
  169.                   sprintf (szOutputBuffer1, 
  170.                            "The Address of StartSelection is %x",
  171.                            lpAddress);
  172.                   MessageBox (NULL, szOutputBuffer1, "GetProcAddress", MB_OK);
  173.                }
  174.                else
  175.                   MessageBox (NULL, 
  176.                               "Error detected while searching for address.", 
  177.                               "GetProcAddress", MB_OK);
  178.              
  179.                FreeLibrary ( hLibModule );
  180.  
  181.                break;
  182.  
  183.           case IDM_HELP:
  184.                ProcessMessage (hWnd, 2);
  185.                break;
  186.        }
  187.        break;
  188.  
  189.   case WM_PAINT:
  190.        BeginPaint(hWnd, (LPPAINTSTRUCT)&ps);
  191.        EndPaint(hWnd, (LPPAINTSTRUCT)&ps);
  192.        break;
  193.  
  194.   case WM_DESTROY:
  195.        PostQuitMessage(0);
  196.        break;
  197.  
  198.   default:
  199.   {
  200.    return DefWindowProc (hWnd, iMessage, wParam, lParam) ;
  201.   }
  202.  }
  203.  return (0L); 
  204. }
  205.  
  206.  
  207.  
  208.