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

  1. /*
  2.  *  LocalReAlloc
  3.  *
  4.  *  This program demonstrates the use of LocalReAlloc.    It will allocate
  5.  *  a block of memory using LocalAlloc, copy a string to the block of
  6.  *  memory, enlarge the block, and then display the string using the
  7.  *  MessageBox function.
  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.   HANDLE    hNewBlock;      /*  A handle to the new memory block    */
  20.   WORD        wFlags;       /*  Return Value from function  */
  21.   char NEAR *    pBuffer;      /*  Pointer to locked buffer    */
  22.   char *    strcpy();      /*  Tell compiler that it returns
  23.                    *  a pointer to a char
  24.                    */
  25.  
  26.   hMemBlock = LocalAlloc( LMEM_ZEROINIT | LMEM_MOVEABLE ,
  27.              (WORD) 70 );
  28.        /*  Allocate 70 bytes of moveable memory and initialize
  29.     *  all of the bytes to 0
  30.     */
  31.  
  32.   pBuffer = LocalLock ( hMemBlock );
  33.   if ( pBuffer != NULL )
  34.     {
  35.     strcpy ( pBuffer , "Both LocalReAlloc and LocalLock were Successfull" );
  36.     LocalUnlock( hMemBlock );
  37.        /*  Place string into pBuffer and unlock the memory.
  38.     *  When increasing the size of the block, we don't have to worry
  39.     *  about the contents.    They will still be intact, but there will
  40.     *  be more memory available
  41.     */
  42.     }
  43.  
  44.   hNewBlock = LocalReAlloc( hMemBlock , 80 ,
  45.                 LMEM_MOVEABLE | LMEM_ZEROINIT );
  46.        /*  Resized the block to 80 bytes.  It makes the new block moveable
  47.     *  and also initializes any new bytes to zero
  48.     */
  49.  
  50.   if ( hNewBlock != NULL )        /*    Check to see if successful  */
  51.     {
  52.     pBuffer = LocalLock ( hNewBlock );
  53.        /*  Lock it and return the pointer  */
  54.  
  55.     if ( pBuffer != NULL )
  56.       MessageBox( NULL , (LPSTR) pBuffer , (LPSTR) "OK" , MB_OK );
  57.      /*  Display the message that all is OK  */
  58.     else
  59.       MessageBox( NULL ,
  60.          (LPSTR)"LocalLock was not Successfull, but LocalReAlloc was",
  61.          (LPSTR)"OK" , MB_OK );
  62.      /*  Tell user that things went half right  */
  63.     }
  64.   else
  65.     MessageBox( NULL , (LPSTR)"LocalReAlloc was not Successful" ,
  66.         (LPSTR) "OK" , MB_OK );
  67.     /*  Tell user that it didn't work at all  */
  68.  
  69.   return 0;
  70.   }
  71.