home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Troubleshooting Netware Systems
/
CSTRIAL0196.BIN
/
attach
/
msj
/
v10n04
/
memman.exe
/
PHYS.C
< prev
next >
Wrap
C/C++ Source or Header
|
1995-04-01
|
7KB
|
229 lines
//===========================================================================
// Program: PHYS
// FILE: PHYS.C
// Author: Matt Pietrek, 1994
//===========================================================================
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#pragma hdrstop
void ShowPhysicalPages(void);
void CreateSharedMemoryRegion(void);
void DeleteSharedMemoryRegion(void);
void ModifyCodePage(void);
PSTR GetPageAttributesAsString(DWORD linear);
// Thunked functions
DWORD WINAPI GetPhysicalAddrFromLinear( DWORD linear );
DWORD WINAPI GetPageAttributes(DWORD linear);
BOOL FirstInstance = TRUE;
PBYTE PMemMapFileRegion;
#pragma data_seg("SHAREDAT") // Declare a variable in a shared
int MySharedSectionVariable = 0; // Section. The variable must be
#pragma data_seg() // initialized for the linker to put it
// in the specified section
int main()
{
CreateSharedMemoryRegion();
if ( FirstInstance )
printf("***** FIRST INSTANCE *****\n");
else
printf("***** SECONDARY INSTANCE *****\n");
ShowPhysicalPages();
printf("Press any key...\n");
getch();
if ( FirstInstance )
{
printf("\nNow modifying the code page\n");
ModifyCodePage();
ShowPhysicalPages();
}
DeleteSharedMemoryRegion();
return 0;
}
void ShowPhysicalPages(void)
{
DWORD linearAddr;
MEMORY_BASIC_INFORMATION mbi;
//
// Get the starting address of the code area. We'll pass VirtualQuery
// the address of a routine within the code area.
//
VirtualQuery( ShowPhysicalPages, &mbi, sizeof(mbi) );
linearAddr = (DWORD)mbi.BaseAddress;
printf( "First code page - Linear:%08X Physical:%08X %s\n",
linearAddr,
GetPhysicalAddrFromLinear(linearAddr),
GetPageAttributesAsString(linearAddr) );
//
// Get the starting address of the data area. We'll pass VirtualQuery
// the address of a global variable within the data area.
//
VirtualQuery( &FirstInstance, &mbi, sizeof(mbi) );
linearAddr = (DWORD)mbi.BaseAddress;
printf( "First data page - Linear:%08X Physical:%08X %s\n",
linearAddr,
GetPhysicalAddrFromLinear(linearAddr),
GetPageAttributesAsString(linearAddr) );
//
// Get the address of a data section with the SHARED attribute
//
MySharedSectionVariable = 1; // Touch it to force it present
linearAddr = (DWORD)&MySharedSectionVariable;
printf( "Shared section - Linear:%08X Physical:%08X %s\n",
linearAddr,
GetPhysicalAddrFromLinear(linearAddr),
GetPageAttributesAsString(linearAddr) );
//
// Get the address of a resource within the module
//
linearAddr = (DWORD)
FindResource(GetModuleHandle(0), MAKEINTATOM(1), RT_STRING);
printf( "Resources - Linear:%08X Physical:%08X %s\n",
linearAddr,
GetPhysicalAddrFromLinear(linearAddr),
GetPageAttributesAsString(linearAddr) );
//
// Get the starting address of the process heap area.
//
linearAddr = (DWORD)GetProcessHeap();
printf( "Process Heap - Linear:%08X Physical:%08X %s\n",
linearAddr,
GetPhysicalAddrFromLinear(linearAddr),
GetPageAttributesAsString(linearAddr) );
//
// Get the starting address of the process environment area.
//
VirtualQuery( GetEnvironmentStrings(), &mbi, sizeof(mbi) );
linearAddr = (DWORD)mbi.BaseAddress;
printf( "Environment area - Linear:%08X Physical:%08X %s\n",
linearAddr,
GetPhysicalAddrFromLinear(linearAddr),
GetPageAttributesAsString(linearAddr) );
//
// Get the starting address of the stack area. We'll pass
// the address of a stack variable to VirtualQuery
//
VirtualQuery( &linearAddr, &mbi, sizeof(mbi) );
linearAddr = (DWORD)mbi.BaseAddress;
printf( "Current Stack page - Linear:%08X Physical:%08X %s\n",
linearAddr,
GetPhysicalAddrFromLinear(linearAddr),
GetPageAttributesAsString(linearAddr) );
//
// Show the address of a memory mapped file
//
linearAddr = (DWORD)PMemMapFileRegion;
printf( "Memory Mapped file - Linear:%08X Physical:%08X %s\n",
linearAddr,
GetPhysicalAddrFromLinear(linearAddr),
GetPageAttributesAsString(linearAddr) );
//
// Show the address of a routine in KERNEL32.DLL
//
linearAddr = (DWORD)
GetProcAddress( GetModuleHandle("KERNEL32.DLL"), "VirtualQuery" );
printf( "KERNEL32.DLL - Linear:%08X Physical:%08X %s\n",
linearAddr,
GetPhysicalAddrFromLinear(linearAddr),
GetPageAttributesAsString(linearAddr) );
}
HANDLE HFileMapping;
void CreateSharedMemoryRegion(void)
{
BYTE myByte;
HFileMapping = CreateFileMapping( (HANDLE)0xFFFFFFFF, // File handle
0, // security
PAGE_READWRITE, // protection
0, 0x1000, // size
"MyFileMapping" );
// In the above call, we can pass PAGE_WRITECOPY instead of
// PAGE_READWRITE, but then the subsequent MapViewOfFile will fail
if ( !HFileMapping )
{
printf("Couldn't create file mapping!\n");
return;
}
if ( GetLastError() == ERROR_ALREADY_EXISTS )
FirstInstance = FALSE;
PMemMapFileRegion = MapViewOfFile( HFileMapping, // hMapObject
FILE_MAP_ALL_ACCESS, // access
0, 0, // offset
0 ); // size
if ( !PMemMapFileRegion )
{
printf("Couldn't map view of file!\n");
return;
}
myByte = *PMemMapFileRegion; // Touch the memory to force it present
}
void DeleteSharedMemoryRegion(void)
{
if ( PMemMapFileRegion )
UnmapViewOfFile( PMemMapFileRegion );
if ( HFileMapping )
CloseHandle( HFileMapping );
}
void Dummy(void) // Exists solely for us to bash
{
}
void ModifyCodePage(void)
{
BYTE srcByte = 0xCC;
DWORD cbWritten;
if ( !WriteProcessMemory(GetCurrentProcess(), Dummy, &srcByte,
1, &cbWritten) || (cbWritten != 1) )
printf("Couldn't write to code page!\n");
}
PSTR GetPageAttributesAsString(DWORD linear)
{
DWORD attr;
static char szRetBuffer[128];
attr = GetPageAttributes(linear);
if ( (attr & 1) == 0 )
return "not present";
strcpy( szRetBuffer, attr & 2 ? "Read/Write" : "ReadOnly" );
strcat( szRetBuffer, " " );
strcat( szRetBuffer, attr & 4 ? "USER" : "SPRVSR" );
return szRetBuffer;
}