home *** CD-ROM | disk | FTP | other *** search
/ Troubleshooting Netware Systems / CSTRIAL0196.BIN / attach / msj / v10n04 / memman.exe / PHYS.C < prev    next >
C/C++ Source or Header  |  1995-04-01  |  7KB  |  229 lines

  1. //===========================================================================
  2. // Program: PHYS
  3. // FILE:    PHYS.C
  4. // Author:  Matt Pietrek, 1994
  5. //===========================================================================
  6.  
  7. #include <windows.h>
  8. #include <stdio.h>
  9. #include <conio.h>
  10. #pragma hdrstop
  11.  
  12. void ShowPhysicalPages(void);
  13. void CreateSharedMemoryRegion(void);
  14. void DeleteSharedMemoryRegion(void);
  15. void ModifyCodePage(void);
  16. PSTR GetPageAttributesAsString(DWORD linear);
  17.  
  18. // Thunked functions
  19. DWORD WINAPI GetPhysicalAddrFromLinear( DWORD linear );
  20. DWORD WINAPI GetPageAttributes(DWORD linear);
  21.  
  22. BOOL FirstInstance = TRUE;
  23. PBYTE PMemMapFileRegion;
  24.  
  25. #pragma data_seg("SHAREDAT")        // Declare a variable in a shared
  26. int MySharedSectionVariable = 0;    // Section.  The variable must be
  27. #pragma data_seg()                  // initialized for the linker to put it
  28.                                     // in the specified section
  29.  
  30. int main()
  31. {
  32.     CreateSharedMemoryRegion();
  33.  
  34.     if ( FirstInstance )
  35.         printf("***** FIRST INSTANCE *****\n");
  36.     else
  37.         printf("***** SECONDARY INSTANCE *****\n");
  38.  
  39.     ShowPhysicalPages();
  40.  
  41.     printf("Press any key...\n");
  42.     getch();
  43.  
  44.     if ( FirstInstance )
  45.     {
  46.         printf("\nNow modifying the code page\n");
  47.         ModifyCodePage();
  48.         ShowPhysicalPages();
  49.     }
  50.     
  51.     DeleteSharedMemoryRegion();
  52.  
  53.     return 0;
  54. }
  55.  
  56. void ShowPhysicalPages(void)
  57. {
  58.     DWORD linearAddr;
  59.     MEMORY_BASIC_INFORMATION mbi;
  60.  
  61.     //
  62.     // Get the starting address of the code area.  We'll pass VirtualQuery
  63.     // the address of a routine within the code area.
  64.     //
  65.     VirtualQuery( ShowPhysicalPages, &mbi, sizeof(mbi) );
  66.     linearAddr = (DWORD)mbi.BaseAddress;
  67.     printf( "First code page     - Linear:%08X  Physical:%08X  %s\n",
  68.             linearAddr,
  69.             GetPhysicalAddrFromLinear(linearAddr),
  70.             GetPageAttributesAsString(linearAddr) );
  71.  
  72.     //
  73.     // Get the starting address of the data area.  We'll pass VirtualQuery
  74.     // the address of a global variable within the data area.
  75.     //
  76.     VirtualQuery( &FirstInstance, &mbi, sizeof(mbi) );
  77.     linearAddr = (DWORD)mbi.BaseAddress;
  78.     printf( "First data page     - Linear:%08X  Physical:%08X  %s\n",
  79.             linearAddr,
  80.             GetPhysicalAddrFromLinear(linearAddr),
  81.             GetPageAttributesAsString(linearAddr) );
  82.  
  83.     //
  84.     // Get the address of a data section with the SHARED attribute
  85.     //
  86.     MySharedSectionVariable = 1;    // Touch it to force it present
  87.     linearAddr = (DWORD)&MySharedSectionVariable;
  88.     printf( "Shared section      - Linear:%08X  Physical:%08X  %s\n",
  89.             linearAddr,
  90.             GetPhysicalAddrFromLinear(linearAddr),
  91.             GetPageAttributesAsString(linearAddr) );
  92.  
  93.     //
  94.     // Get the address of a resource within the module
  95.     //
  96.     linearAddr = (DWORD)
  97.             FindResource(GetModuleHandle(0), MAKEINTATOM(1), RT_STRING);
  98.     printf( "Resources           - Linear:%08X  Physical:%08X  %s\n",
  99.             linearAddr,
  100.             GetPhysicalAddrFromLinear(linearAddr),
  101.             GetPageAttributesAsString(linearAddr) );
  102.     
  103.     //
  104.     // Get the starting address of the process heap area.
  105.     //
  106.     linearAddr = (DWORD)GetProcessHeap();
  107.     printf( "Process Heap        - Linear:%08X  Physical:%08X  %s\n",
  108.             linearAddr,
  109.             GetPhysicalAddrFromLinear(linearAddr),
  110.             GetPageAttributesAsString(linearAddr) );
  111.  
  112.     //
  113.     // Get the starting address of the process environment area.
  114.     //
  115.     VirtualQuery( GetEnvironmentStrings(), &mbi, sizeof(mbi) );
  116.     linearAddr = (DWORD)mbi.BaseAddress;
  117.     printf( "Environment area    - Linear:%08X  Physical:%08X  %s\n",
  118.             linearAddr,
  119.             GetPhysicalAddrFromLinear(linearAddr),
  120.             GetPageAttributesAsString(linearAddr) );
  121.  
  122.     //
  123.     // Get the starting address of the stack area.  We'll pass
  124.     // the address of a stack variable to VirtualQuery
  125.     //
  126.     VirtualQuery( &linearAddr, &mbi, sizeof(mbi) );
  127.     linearAddr = (DWORD)mbi.BaseAddress;
  128.     printf( "Current Stack page  - Linear:%08X  Physical:%08X  %s\n",
  129.             linearAddr,
  130.             GetPhysicalAddrFromLinear(linearAddr),
  131.             GetPageAttributesAsString(linearAddr) );
  132.  
  133.     //
  134.     // Show the address of a memory mapped file
  135.     //
  136.     linearAddr = (DWORD)PMemMapFileRegion;
  137.     printf( "Memory Mapped file  - Linear:%08X  Physical:%08X  %s\n",
  138.             linearAddr,
  139.             GetPhysicalAddrFromLinear(linearAddr),
  140.             GetPageAttributesAsString(linearAddr) );
  141.  
  142.     //
  143.     // Show the address of a routine in KERNEL32.DLL
  144.     //
  145.     linearAddr = (DWORD)
  146.         GetProcAddress( GetModuleHandle("KERNEL32.DLL"), "VirtualQuery" );
  147.     printf( "KERNEL32.DLL        - Linear:%08X  Physical:%08X  %s\n",
  148.             linearAddr,
  149.             GetPhysicalAddrFromLinear(linearAddr),
  150.             GetPageAttributesAsString(linearAddr) );
  151. }
  152.  
  153. HANDLE HFileMapping;
  154.  
  155. void CreateSharedMemoryRegion(void)
  156. {
  157.     BYTE myByte;
  158.     
  159.     HFileMapping = CreateFileMapping( (HANDLE)0xFFFFFFFF,   // File handle
  160.                                     0,                      // security
  161.                                     PAGE_READWRITE,         // protection
  162.                                     0, 0x1000,              // size
  163.                                     "MyFileMapping" );
  164.  
  165.     // In the above call, we can pass PAGE_WRITECOPY instead of
  166.     // PAGE_READWRITE, but then the subsequent MapViewOfFile will fail
  167.  
  168.     if ( !HFileMapping )
  169.     {
  170.         printf("Couldn't create file mapping!\n");
  171.         return;
  172.     }
  173.  
  174.     if ( GetLastError() == ERROR_ALREADY_EXISTS )
  175.         FirstInstance = FALSE;
  176.  
  177.     PMemMapFileRegion = MapViewOfFile( HFileMapping,            // hMapObject
  178.                                         FILE_MAP_ALL_ACCESS,    // access
  179.                                         0, 0,                   // offset
  180.                                         0 );                    // size
  181.     if ( !PMemMapFileRegion )
  182.     {
  183.         printf("Couldn't map view of file!\n");
  184.         return;
  185.     }
  186.     
  187.     myByte = *PMemMapFileRegion;    // Touch the memory to force it present
  188. }
  189.  
  190. void DeleteSharedMemoryRegion(void)
  191. {
  192.     if ( PMemMapFileRegion )
  193.         UnmapViewOfFile( PMemMapFileRegion );
  194.     
  195.     if ( HFileMapping )
  196.         CloseHandle( HFileMapping );
  197. }
  198.  
  199. void Dummy(void)    // Exists solely for us to bash
  200. {
  201. }
  202.  
  203. void ModifyCodePage(void)
  204. {
  205.     BYTE srcByte = 0xCC;
  206.     DWORD cbWritten;
  207.     
  208.     if ( !WriteProcessMemory(GetCurrentProcess(), Dummy, &srcByte,
  209.                                 1, &cbWritten) || (cbWritten != 1) )
  210.         printf("Couldn't write to code page!\n");
  211. }
  212.  
  213. PSTR GetPageAttributesAsString(DWORD linear)
  214. {
  215.     DWORD attr;
  216.     static char szRetBuffer[128];
  217.     
  218.     attr = GetPageAttributes(linear);
  219.     if ( (attr & 1) == 0  )
  220.         return "not present";
  221.     
  222.     strcpy( szRetBuffer, attr & 2 ? "Read/Write" : "ReadOnly" );
  223.     strcat( szRetBuffer, " " );
  224.     strcat( szRetBuffer, attr & 4 ? "USER" : "SPRVSR" );
  225.     
  226.     return szRetBuffer;
  227. }
  228.  
  229.