Heap_Strings.cpp
Upload User: dq031136
Upload Date: 2022-08-08
Package Size: 802k
Code Size: 7k
Development Platform:

C++ Builder

  1. #include <windows.h>  
  2. #include "Heap_Strings.h" 
  3. #if defined (WIN32)
  4. #define IS_WIN32 TRUE
  5. #else
  6. #define IS_WIN32 FALSE
  7. #endif
  8. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  9. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  10. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  11. HINSTANCE hInst;   // current instance
  12. LPCTSTR lpszAppName  = "MyApp";
  13. LPCTSTR lpszTitle    = "My Application"; 
  14. #define BLOCKSIZE 32
  15. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  16. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  17.                       LPTSTR lpCmdLine, int nCmdShow)
  18. {
  19.    MSG      msg;
  20.    HWND     hWnd; 
  21.    WNDCLASS wc;
  22.    // Register the main application window class.
  23.    //............................................
  24.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  25.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  26.    wc.cbClsExtra    = 0;                      
  27.    wc.cbWndExtra    = 0;                      
  28.    wc.hInstance     = hInstance;              
  29.    wc.hIcon         = LoadIcon( hInstance, lpszAppName ); 
  30.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  31.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  32.    wc.lpszMenuName  = lpszAppName;              
  33.    wc.lpszClassName = lpszAppName;              
  34.    if ( IS_WIN95 )
  35.    {
  36.       if ( !RegisterWin95( &wc ) )
  37.          return( FALSE );
  38.    }
  39.    else if ( !RegisterClass( &wc ) )
  40.       return( FALSE );
  41.    hInst = hInstance; 
  42.    // Create the main application window.
  43.    //....................................
  44.    hWnd = CreateWindow( lpszAppName, 
  45.                         lpszTitle,    
  46.                         WS_OVERLAPPEDWINDOW, 
  47.                         CW_USEDEFAULT, 0, 
  48.                         CW_USEDEFAULT, 0,  
  49.                         NULL,              
  50.                         NULL,              
  51.                         hInstance,         
  52.                         NULL               
  53.                       );
  54.    if ( !hWnd ) 
  55.       return( FALSE );
  56.    ShowWindow( hWnd, nCmdShow ); 
  57.    UpdateWindow( hWnd );         
  58.    while( GetMessage( &msg, NULL, 0, 0) )   
  59.    {
  60.       TranslateMessage( &msg ); 
  61.       DispatchMessage( &msg );  
  62.    }
  63.    return( msg.wParam ); 
  64. }
  65. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  66. {
  67.    WNDCLASSEX wcex;
  68.    wcex.style         = lpwc->style;
  69.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  70.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  71.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  72.    wcex.hInstance     = lpwc->hInstance;
  73.    wcex.hIcon         = lpwc->hIcon;
  74.    wcex.hCursor       = lpwc->hCursor;
  75.    wcex.hbrBackground = lpwc->hbrBackground;
  76.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  77.    wcex.lpszClassName = lpwc->lpszClassName;
  78.    // Added elements for Windows 95.
  79.    //...............................
  80.    wcex.cbSize = sizeof(WNDCLASSEX);
  81.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  82.                             IMAGE_ICON, 16, 16,
  83.                             LR_DEFAULTCOLOR );
  84.    return RegisterClassEx( &wcex );
  85. }
  86. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  87. {
  88. static LPTSTR* lpStrList;
  89. static int     nBlocks;
  90. static int     nStrings;
  91. static HANDLE  hHeap     = NULL;
  92.    switch( uMsg )
  93.    {
  94.       case WM_CREATE  : 
  95.               hHeap     = HeapCreate( 0, 1024, 0 );
  96.               lpStrList = HeapAlloc( hHeap, HEAP_ZERO_MEMORY, 
  97.                                      sizeof( LPTSTR )*BLOCKSIZE );
  98.               nBlocks   = 1;
  99.               nStrings  = 0;
  100.               break;
  101.       case WM_COMMAND :
  102.               switch( LOWORD( wParam ) )
  103.               {
  104.                  case IDM_ALLOCATE :
  105.                         {
  106.                            // If there is no room in the array then allocate
  107.                            // more space to store the new item in.
  108.                            //.....................................
  109.                            if ( nStrings == nBlocks*BLOCKSIZE )
  110.                            {
  111.                               nBlocks++;
  112.                               lpStrList = HeapReAlloc( hHeap, HEAP_ZERO_MEMORY, 
  113.                                                        lpStrList, 
  114.                                                        sizeof( LPTSTR )
  115.                                                        *BLOCKSIZE*nBlocks );
  116.                            }
  117.                            *(lpStrList+nStrings) = HeapAlloc( hHeap,
  118.                                                       HEAP_ZERO_MEMORY, 12 );
  119.                            strcpy( *(lpStrList+nStrings), "Test String" );
  120.                            nStrings++;
  121.                         }
  122.                         break;
  123.                  case IDM_FREE :
  124.                         if ( nStrings > 0 )
  125.                         {
  126.                            nStrings--;
  127.                            HeapFree( hHeap, 0, *(lpStrList+nStrings) );
  128.                            if ( nStrings < (nBlocks-1)*BLOCKSIZE )
  129.                            {
  130.                               nBlocks--;
  131.                               lpStrList = HeapReAlloc( hHeap, 0, 
  132.                                                        lpStrList, 
  133.                                                        sizeof( LPTSTR )
  134.                                                        *BLOCKSIZE*nBlocks );
  135.                            }
  136.                         }
  137.                         break;
  138.                  case IDM_ABOUT :
  139.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  140.                         break;
  141.                  case IDM_EXIT :
  142.                         DestroyWindow( hWnd );
  143.                         break;
  144.               }
  145.               break;
  146.       
  147.       case WM_DESTROY :
  148.               HeapDestroy( hHeap );
  149.               PostQuitMessage(0);
  150.               break;
  151.       default :
  152.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  153.    }
  154.    return( 0L );
  155. }
  156. LRESULT CALLBACK About( HWND hDlg,           
  157.                         UINT message,        
  158.                         WPARAM wParam,       
  159.                         LPARAM lParam)
  160. {
  161.    switch (message) 
  162.    {
  163.        case WM_INITDIALOG: 
  164.                return (TRUE);
  165.        case WM_COMMAND:                              
  166.                if (   LOWORD(wParam) == IDOK         
  167.                    || LOWORD(wParam) == IDCANCEL)    
  168.                {
  169.                        EndDialog(hDlg, TRUE);        
  170.                        return (TRUE);
  171.                }
  172.                break;
  173.    }
  174.    return (FALSE); 
  175. }