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

  1. /*
  2.  * Name: acsresrc.c
  3.  * Function (s) demonstrated in this program: AccessResource (), FindResource (),
  4.  *                                           SizeofResource ().
  5.  * Description  :
  6.  *         FindResource is used first to locate and identify the resource
  7.  *         item.  SizeofResource is used to determine how many bytes need
  8.  *         to be read to obtain the resource item, and finally
  9.  *         AccessResource () is used to get the handle and set the file
  10.  *         pointer to the beginning of the selected resource.  AccessResource ()
  11.  *         opens the file (resource) as a result, so it must be explicitely
  12.  *         closed.  In this case, "It works!" is displayed in a MessageBox ()
  13.  *         upon successful completion, "It failed" displays if unsuccessful.
  14.  * Additional Comments: Note that _lclose () must be used to close the file
  15.  *         (resource).
  16.  */
  17.  
  18. #include <windows.h>
  19. #include "acsresrc.h"
  20.  
  21. long    FAR PASCAL WndProc (HWND, unsigned, WORD, LONG);
  22. int     FAR PASCAL _lclose (int);
  23. int     FAR PASCAL _lread (HANDLE, LPSTR, short);
  24.  
  25. static char    szBuffer[8];
  26. static int    ErrorCheck;
  27. static char    szResName [] = "HotStuff";
  28.  
  29. int    PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  30. HANDLE   hInstance, hPrevInstance;
  31. LPSTR    lpszCmdLine;
  32. int      nCmdShow;
  33.   {
  34.   static char  szAppName [] = "Resources";
  35.   HWND       hWnd;
  36.   WNDCLASS   wndclass;
  37.   MSG        msg;
  38.   HMENU      hMenu;
  39.  
  40.   if (!hPrevInstance)
  41.     {
  42.     wndclass.style         = CS_HREDRAW | CS_VREDRAW;
  43.     wndclass.lpfnWndProc   = WndProc;
  44.     wndclass.cbClsExtra    = 0;
  45.     wndclass.cbWndExtra    = 0;
  46.     wndclass.hInstance     = hInstance;
  47.     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION);
  48.     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW);
  49.     wndclass.hbrBackground = GetStockObject (WHITE_BRUSH);
  50.     wndclass.lpszMenuName  = NULL;
  51.     wndclass.lpszClassName = szAppName;
  52.  
  53.     if (!RegisterClass (&wndclass))
  54.       return FALSE;
  55.     }
  56.  
  57.   hMenu = LoadMenu (hInstance, "ResMenu");
  58.  
  59.   hWnd = CreateWindow (szAppName, "AccessResource",
  60.                       WS_OVERLAPPEDWINDOW,
  61.                       CW_USEDEFAULT, 0,
  62.                       CW_USEDEFAULT, 0,
  63.                       NULL, hMenu, hInstance, NULL);
  64.  
  65.   ShowWindow (hWnd, nCmdShow);
  66.   UpdateWindow (hWnd);
  67.  
  68.   while (GetMessage (&msg, NULL, 0, 0))
  69.     {
  70.     TranslateMessage (&msg);
  71.     DispatchMessage (&msg);
  72.     }
  73.   return (msg.wParam);
  74.   }
  75.  
  76. long    FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
  77. HWND      hWnd;
  78. unsigned  iMessage;
  79. WORD      wParam;
  80. LONG      lParam;
  81.   {
  82.   static HANDLE hInstance, hResource;
  83.   int    hFile;
  84.   char  szStuff[20], szSuccess[20];
  85.   short sRes;
  86.  
  87.   switch (iMessage)
  88.     {
  89.     case WM_CREATE:
  90.       hInstance = GetWindowWord (hWnd, GWW_HINSTANCE);
  91.       break;
  92.  
  93.     case WM_COMMAND:
  94.       switch (wParam)
  95.         {
  96.         case IDM_EXECUTE:
  97. /* FindResource is used first to locate and identify the resource
  98.         item.  SizeofResource is used to determine how many bytes need
  99.         to be read to obtain the resource item, and finally
  100.         AccessResource () is used to get the handle and set the file
  101.         pointer to the beginning of the selected resource.  AccessResource ()
  102.         opens the file (resource) as a result, so it must be explicitely
  103.         closed.
  104.      */
  105.           hResource = FindResource (hInstance, "HotStuff", RT_RCDATA);
  106.           sRes = SizeofResource (hInstance, hResource);
  107.           hFile = AccessResource (hInstance, hResource);
  108.           ErrorCheck = _lread (hFile, (LPSTR)szStuff, sRes);
  109.           ErrorCheck = _lclose (hFile);
  110.           if (hFile < 1)
  111.             {
  112.             ErrorCheck = sprintf (szStuff, "It failed");
  113.             ErrorCheck = sprintf (szSuccess, "Error");
  114.             }
  115.       else
  116.             ErrorCheck = sprintf (szSuccess, "Item read");
  117.           MessageBox (GetFocus (), szStuff, szSuccess, MB_OK);
  118.         }
  119.         break;
  120.  
  121.     case WM_DESTROY:
  122.       PostQuitMessage (0);
  123.       break;
  124.  
  125.     default:
  126.       return DefWindowProc (hWnd, iMessage, wParam, lParam);
  127.     }
  128.   return (0L);
  129.   }
  130.