/* Query.cpp : demonstrates the use of Win32 API functions VirtualQuery() and VirtualQueryEx(). Author : Mithun Shanbhag, mrshanbh@syr.edu */ /* Version History =============== Version 1.0 : 07/20/2004 - First Release Planned Modifications ===================== - Make a module to retrive Handles / IDs of running processes. using tl32help api. */ #include #include #include #include #include #include // istringstream using namespace std ; //----< outputs string in Heading format >---- void Title ( const std::string & str ) { for ( int i = 0 ; i < ( int ) str.length() ; i ++ ) printf ( "-" ) ; printf ( "\n" ) ; printf ( str.c_str() ) ; printf ( "\n" ) ; for ( int i = 0 ; i < ( int ) str.length() ; i ++ ) printf ( "-" ) ; printf ( "\n\n" ) ; } //----< dump of virtual address space of specified process >---- void QueryProcVirtAddSpace ( HANDLE hProc, const DWORD & dwPid ) { std::ostringstream os ( "" ) ; os << "Virtual Address Space. " ; if (dwPid == 0) os << "Current Process" ; else os << "Process # " << dwPid ; Title ( os.str() ) ; SYSTEM_INFO SysInfo ; GetSystemInfo ( & SysInfo ) ; LPCVOID lpAddress = reinterpret_cast < LPCVOID > ( 0x00000000 ) ; MEMORY_BASIC_INFORMATION buffer ; SIZE_T size ; std::string tempStr = "" ; cout << setw(12) << left << "BASE" << setw(14) << left << "SIZE(bytes)" << setw(11) << left << "STATE" << setw(9) << left << "TYPE" << setw(12) << left << "PROTECTION" << setw(3) << left << "|" << setw(12) << left << "ALLOC-BASE" << setw(16) << left << "ALLOC-PROTECTION" << "\n\n" ; while (lpAddress < SysInfo.lpMaximumApplicationAddress ) { if ( ( size = VirtualQuery ( lpAddress, & buffer, sizeof ( MEMORY_BASIC_INFORMATION ) ) ) != 0 ) { cout << setw(2) << left << "0x" << setw(10) << left << buffer.BaseAddress ; cout << setw(2) << left << "0x" << setw(12) << left << hex << (unsigned long) buffer.RegionSize ; if ( buffer.State == MEM_FREE ) tempStr = "FREE" ; else if ( buffer.State == MEM_COMMIT ) tempStr = "COMMITTED" ; else if ( buffer.State == MEM_RESERVE ) tempStr = "RESERVED" ; cout << setw(11) << left << tempStr ; if ( buffer.Type == MEM_IMAGE ) tempStr = "IMAGE" ; else if ( buffer.Type == MEM_MAPPED ) tempStr = "MAPPED" ; else if ( buffer.Type == MEM_PRIVATE ) tempStr = "PRIVATE" ; cout << setw(9) << left << tempStr ; if ( buffer.Protect == PAGE_EXECUTE ) tempStr = "E" ; else if ( buffer.Protect == PAGE_EXECUTE_READ ) tempStr = "E,R" ; else if ( buffer.Protect == PAGE_EXECUTE_READWRITE ) tempStr = "E,R,W" ; else if ( buffer.Protect == PAGE_EXECUTE_WRITECOPY ) tempStr = "E,R,W,WC" ; else if ( buffer.Protect == PAGE_NOACCESS ) tempStr = "NO ACCESS" ; else if ( buffer.Protect == PAGE_READONLY ) tempStr = "R" ; else if ( buffer.Protect == PAGE_READWRITE ) tempStr = "R,W" ; else if ( buffer.Protect == PAGE_WRITECOPY ) tempStr = "R,W,WC" ; else if ( buffer.Protect == PAGE_GUARD ) tempStr = "GUARD" ; else if ( buffer.Protect == PAGE_NOCACHE ) tempStr = "NO CACHE" ; else if ( buffer.Protect == PAGE_WRITECOMBINE ) tempStr = "WCOM" ; cout << setw(12) << left << tempStr ; cout << setw(3) << left << "|" ; cout << setw(2) << left << "0x" << setw(10) << left << buffer.AllocationBase ; if ( buffer.AllocationProtect == PAGE_EXECUTE ) tempStr = "E" ; else if ( buffer.AllocationProtect == PAGE_EXECUTE_READ ) tempStr = "E,R" ; else if ( buffer.AllocationProtect == PAGE_EXECUTE_READWRITE ) tempStr = "E,R,W" ; else if ( buffer.AllocationProtect == PAGE_EXECUTE_WRITECOPY ) tempStr = "E,R,W,WC" ; else if ( buffer.AllocationProtect == PAGE_NOACCESS ) tempStr = "NO ACCESS" ; else if ( buffer.AllocationProtect == PAGE_READONLY ) tempStr = "R" ; else if ( buffer.AllocationProtect == PAGE_READWRITE ) tempStr = "R,W" ; else if ( buffer.AllocationProtect == PAGE_WRITECOPY ) tempStr = "R,W,WC" ; else if ( buffer.AllocationProtect == PAGE_GUARD ) tempStr = "GUARD" ; else if ( buffer.AllocationProtect == PAGE_NOCACHE ) tempStr = "NO CACHE" ; else if ( buffer.AllocationProtect == PAGE_WRITECOMBINE ) tempStr = "WCOM" ; cout << setw(12) << left << tempStr ; cout << "\n" ; } else /* if call to virtual query failed */ { cout << "ERROR : UNABLE TO QUERY THE VIRTUAL ADDRESS SPACE" ; } /* increment pointer by current region size */ lpAddress = reinterpret_cast < LPCVOID > ( (unsigned long) lpAddress + (unsigned long) buffer.RegionSize ) ; } cout << "\n\n\n" ; } //----< program entry >---- void main ( int argc, char * argv[] ) { /* a little command-line check */ ///////////////////////////////// if ( argc != 2 ) { cout << "Syntax Error ! \n" << "Usage : vmquery \n" ; return ; } DWORD dwPid = (DWORD) atol ( (const char *) argv[1] ) ; if ( dwPid == 0 ) QueryProcVirtAddSpace ( GetCurrentProcess(), 0 ) ; // for current process else // need to get handle to specified process { HANDLE hProc ; if ( ( hProc = OpenProcess ( PROCESS_QUERY_INFORMATION, FALSE, dwPid ) ) != NULL ) { QueryProcVirtAddSpace ( hProc, dwPid ) ; } CloseHandle ( hProc ) ; // cleanup } }