Virtual_Allocate.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 "Virtual_Allocate.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. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  15. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  16.                       LPTSTR lpCmdLine, int nCmdShow)
  17. {
  18.    MSG      msg;
  19.    HWND     hWnd; 
  20.    WNDCLASS wc;
  21.    // Register the main application window class.
  22.    //............................................
  23.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  24.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  25.    wc.cbClsExtra    = 0;                      
  26.    wc.cbWndExtra    = 0;                      
  27.    wc.hInstance     = hInstance;              
  28.    wc.hIcon         = LoadIcon( hInstance, lpszAppName ); 
  29.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  30.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  31.    wc.lpszMenuName  = lpszAppName;              
  32.    wc.lpszClassName = lpszAppName;              
  33.    if ( IS_WIN95 )
  34.    {
  35.       if ( !RegisterWin95( &wc ) )
  36.          return( FALSE );
  37.    }
  38.    else if ( !RegisterClass( &wc ) )
  39.       return( FALSE );
  40.    hInst = hInstance; 
  41.    // Create the main application window.
  42.    //....................................
  43.    hWnd = CreateWindow( lpszAppName, 
  44.                         lpszTitle,    
  45.                         WS_OVERLAPPEDWINDOW, 
  46.                         CW_USEDEFAULT, 0, 
  47.                         CW_USEDEFAULT, 0,  
  48.                         NULL,              
  49.                         NULL,              
  50.                         hInstance,         
  51.                         NULL               
  52.                       );
  53.    if ( !hWnd ) 
  54.       return( FALSE );
  55.    ShowWindow( hWnd, nCmdShow ); 
  56.    UpdateWindow( hWnd );         
  57.    while( GetMessage( &msg, NULL, 0, 0) )   
  58.    {
  59.       TranslateMessage( &msg ); 
  60.       DispatchMessage( &msg );  
  61.    }
  62.    return( msg.wParam ); 
  63. }
  64. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  65. {
  66.    WNDCLASSEX wcex;
  67.    wcex.style         = lpwc->style;
  68.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  69.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  70.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  71.    wcex.hInstance     = lpwc->hInstance;
  72.    wcex.hIcon         = lpwc->hIcon;
  73.    wcex.hCursor       = lpwc->hCursor;
  74.    wcex.hbrBackground = lpwc->hbrBackground;
  75.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  76.    wcex.lpszClassName = lpwc->lpszClassName;
  77.    // Added elements for Windows 95.
  78.    //...............................
  79.    wcex.cbSize = sizeof(WNDCLASSEX);
  80.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  81.                             IMAGE_ICON, 16, 16,
  82.                             LR_DEFAULTCOLOR );
  83.    return RegisterClassEx( &wcex );
  84. }
  85. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  86. {
  87. static LPVOID lpMem = NULL;
  88.    switch( uMsg )
  89.    {
  90.       case WM_CREATE  : 
  91.               // Reserve 1MB of virtual memory.
  92.               //...............................
  93.               lpMem = VirtualAlloc( NULL, 1048576, MEM_RESERVE, 0 );
  94.               break;
  95.       case WM_COMMAND :
  96.               switch( LOWORD( wParam ) )
  97.               {
  98.                  case IDM_TEST :
  99.                         {
  100.                            // Commit a 70KB block of memory from virtual
  101.                            // memory then use it.
  102.                            //...........................................
  103.                            DWORD  dwoldAccess;
  104.                            LPTSTR lpBuf = VirtualAlloc( lpMem, 71680,
  105.                                                   MEM_COMMIT, PAGE_READWRITE );
  106.                            
  107.                            // Set each 1KB block of memory to a number.
  108.                            //..........................................
  109.                            if ( lpBuf )
  110.                            {
  111.                               int i;
  112.                               for( i = 0; i < 70; i++ )
  113.                               {
  114.                                  if ( VirtualLock( (lpBuf+(i*1024)), 1024 ) )
  115.                                  {
  116.                                     memset( (lpBuf+(i*1024)), i, 1024 );
  117.                                     VirtualUnlock( (lpBuf+(i*1024)), 1024 );
  118.                                  } 
  119.                               } 
  120.                            }
  121.                            // Protect the memory with Read-Only access.
  122.                            //..........................................
  123.                            VirtualProtect( lpBuf, 71680, PAGE_READONLY,
  124.                                            &dwoldAccess );
  125.                            __try // to read from the memory.
  126.                            {
  127.                               char szMsg[20];
  128.                               wsprintf( szMsg, "Memory Value = %d",
  129.                                         *(lpBuf+2048) );
  130.                               MessageBox( hWnd, szMsg, "Read", 
  131.                                           MB_OK | MB_ICONINFORMATION );
  132.                            }
  133.                            __except( 1 )
  134.                            {
  135.                               MessageBox( hWnd, "Error accessing memory",
  136.                                          "Read", 
  137.                                           MB_OK | MB_ICONEXCLAMATION );
  138.                            }
  139.                            __try  // to write to the memory.
  140.                            {
  141.                               *lpBuf = 10;
  142.                            }
  143.                            __except( 1 )
  144.                            {
  145.                               MessageBox( hWnd, "Error accessing memory", 
  146.                                          "Write", 
  147.                                           MB_OK | MB_ICONEXCLAMATION );
  148.                            }
  149.                         }
  150.                         break;
  151.                  case IDM_ABOUT :
  152.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  153.                         break;
  154.                  case IDM_EXIT :
  155.                         DestroyWindow( hWnd );
  156.                         break;
  157.               }
  158.               break;
  159.       
  160.       case WM_DESTROY :
  161.               // Free virtual memory.
  162.               //.....................
  163.               VirtualFree( lpMem, 0, MEM_RELEASE );  
  164.               PostQuitMessage(0);
  165.               break;
  166.       default :
  167.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  168.    }
  169.    return( 0L );
  170. }
  171. LRESULT CALLBACK About( HWND hDlg,           
  172.                         UINT message,        
  173.                         WPARAM wParam,       
  174.                         LPARAM lParam)
  175. {
  176.    switch (message) 
  177.    {
  178.        case WM_INITDIALOG: 
  179.                return (TRUE);
  180.        case WM_COMMAND:                              
  181.                if (   LOWORD(wParam) == IDOK         
  182.                    || LOWORD(wParam) == IDCANCEL)    
  183.                {
  184.                        EndDialog(hDlg, TRUE);        
  185.                        return (TRUE);
  186.                }
  187.                break;
  188.    }
  189.    return (FALSE); 
  190. }