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

  1. /*
  2.  *  LocalSize
  3.  *
  4.  *  This program demonstrates the use of the function LocalSize.  It
  5.  *  allocates a block of memory, using LocalAlloc, Locks it, copies a string
  6.  *  into it, displays it in a message box along with the size of the
  7.  *  memory block.  (Which is returned by LocalSize)
  8.  */
  9.  
  10. #include <windows.h>
  11.  
  12. int PASCAL WinMain ( hInstance , hPrevInstance, lpszCmdLine, cmdShow )
  13.  
  14. HANDLE    hInstance , hPrevInstance;
  15. LPSTR    lpszCmdLine;
  16. int    cmdShow;
  17.   {
  18.   HANDLE    hMemBlock;      /*  The memory block    */
  19.   WORD        wFlags;       /*  Return Value from function  */
  20.   char NEAR *    pBuffer;      /*  Pointer to locked buffer    */
  21.   int           bytes;            /*  Number of bytes allocated  */
  22.  
  23.   hMemBlock = LocalAlloc( LMEM_ZEROINIT | LMEM_MOVEABLE ,
  24.              (WORD) 80 );
  25.  
  26.        /*  Allocate 80 bytes of moveable memory and initialize
  27.     *  all of the bytes to 0
  28.     */
  29.  
  30.   bytes = LocalSize( hMemBlock );
  31.                   /*  Find out the size of hMemBlock  */
  32.  
  33.   pBuffer = LocalLock ( hMemBlock );
  34.        /*  Lock it and return the pointer  */
  35.  
  36.   if ( pBuffer != NULL )
  37.     {
  38.     sprintf( (char *) pBuffer , "LocalAlloc allocated %d bytes" , bytes );
  39.     MessageBox( NULL , (LPSTR) pBuffer , (LPSTR) "OK" , MB_OK );
  40.        /*  Display the message that all is OK  */
  41.     }
  42.   else
  43.     MessageBox( NULL , (LPSTR)"LocalLock was not Successfull" ,
  44.         (LPSTR)"OK" , MB_OK );
  45.  
  46.   return 0;
  47.   }
  48.