#define SALFDELETE(pValue) /
if (pValue) /
delete[] pValue; /
pValue = NULL;
struct stuMemInfo
{
void *pAllocAddr;
wchar_t *pPageInitProtect;
DWORD dwReginSize;
DWORD dwState;
wchar_t *pStateInfo;
wchar_t *pPageProect;
wchar_t *pPageType;
};
void PageProtectToString(DWORD dwProtect, wchar_t *pContent)
{
if (!pContent)
{
return;
}
switch (dwProtect)
{
case PAGE_EXECUTE:
{
wsprintfW(pContent, L"E---");
break;
}
case PAGE_EXECUTE_READ:
{
wsprintfW(pContent, L"ER--");
break;
}
case PAGE_EXECUTE_READWRITE:
{
wsprintfW(pContent, L"ERW-");
break;
}
case PAGE_EXECUTE_WRITECOPY:
{
wsprintfW(pContent, L"ERWC");
break;
}
case PAGE_NOACCESS:
{
wsprintfW(pContent, L"----");
break;
}
case PAGE_READONLY:
{
wsprintfW(pContent, L"-R--");
break;
}
case PAGE_READWRITE:
{
wsprintfW(pContent, L"-RW-");
break;
}
case PAGE_WRITECOPY:
{
wsprintfW(pContent, L"---C");
break;
}
case PAGE_GUARD:
{
wsprintfW(pContent, L"page guard");
break;
}
case PAGE_NOCACHE:
{
wsprintfW(pContent, L"page no catch");
break;
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
SYSTEM_INFO mySysInfo;
memset(&mySysInfo, 0, sizeof(SYSTEM_INFO));
GetSystemInfo(&mySysInfo);
DWORD pageSize = mySysInfo.dwAllocationGranularity;
BYTE *pBasicAddr = NULL;
MEMORY_BASIC_INFORMATION memBasicInfo;
setlocale(LC_CTYPE, "");
wchar_t *pMemInfo = new wchar_t[512];
memset(pMemInfo, 0, sizeof(512));
stuMemInfo memInfo;
memset(&memInfo, 0, sizeof(stuMemInfo));
memInfo.pStateInfo = new wchar_t[50];
memInfo.pPageInitProtect = new wchar_t[20];
memInfo.pPageProect = new wchar_t[50];
memInfo.pPageType = new wchar_t[50];
do
{
memset(&memBasicInfo, 0, sizeof(memBasicInfo));
if ( VirtualQuery(pBasicAddr, &memBasicInfo, sizeof(MEMORY_BASIC_INFORMATION)) == NULL)
{
break;
}
if (memBasicInfo.State == MEM_FREE)
{
wprintf(L"the addr %p is MEM_FREE /r/n", pBasicAddr);
pBasicAddr += memBasicInfo.RegionSize;
continue;
}
memInfo.dwState = memBasicInfo.State;
switch (memBasicInfo.State)
{
case MEM_COMMIT:
{
wsprintfW(memInfo.pStateInfo, L"memory state is MEM_COMMIT");
break;
}
case MEM_RESERVE:
{
wsprintfW(memInfo.pStateInfo, L"memory state is MEM_RESERVE");
break;
}
}
PageProtectToString(memBasicInfo.AllocationProtect, memInfo.pPageInitProtect);
memInfo.dwReginSize = (DWORD) memBasicInfo.RegionSize;
if (memBasicInfo.State != MEM_RESERVE)
{
PageProtectToString(memBasicInfo.Protect, memInfo.pPageProect);
}
else
{
wcscpy(memInfo.pPageProect, L"MEM_RESERVE");
}
switch (memBasicInfo.Type)
{
case MEM_IMAGE:
{
wsprintfW(memInfo.pPageType, L"MEM_IMAGE");
break;
}
case MEM_MAPPED:
{
wsprintfW(memInfo.pPageType, L"MEM_MAPPED");
break;
}
case MEM_PRIVATE:
{
wsprintfW(memInfo.pPageType, L"MEM_PRIVATE");
break;
}
}
memInfo.pAllocAddr = memBasicInfo.AllocationBase;
wprintf(L"the BaseAddress is %p, AllocationBase is %p, PageState is %s,/
InitProtect is %s, current Protect is %s, type is %s, /
page number is %d /r/n",
pBasicAddr, memInfo.pAllocAddr, memInfo.pStateInfo,
memInfo.pPageInitProtect, memInfo.pPageProect,
memInfo.pPageType, memInfo.dwReginSize);
pBasicAddr += memInfo.dwReginSize;
} while (1);
SALFDELETE(memInfo.pPageType);
SALFDELETE(memInfo.pPageProect);
SALFDELETE(memInfo.pPageInitProtect);
SALFDELETE(memInfo.pStateInfo);
getchar();
return 0;
}