通过VirtualQuery查询当前进程虚拟内存使用情况
2017-07-31 09:33
1026 查看
函数原型:
SIZE_T WINAPI VirtualQuery(
_In_opt_ LPCVOID lpAddress,
_Out_ PMEMORY_BASIC_INFORMATION lpBuffer,
_In_ SIZE_T dwLength
);
源代码:
#include<stdio.h>
#include<Windows.h>
/**
*
数据类型总结:
BSTR:OLECHAR * 类型的Unicode字符串,时一个COM字符串,带长度前缀。
LPSTR: char*,指向以"\0"结尾的8位(单字节)ANSI字符数组指针
LPWSTR: wchar_t *,指向以 "\0"结尾的16位(双字节)Unicode字符数组指针
LPCSTR: const char *
LPCWSTR: const wchar_t*
LPTWSTR: LPSTR,LPWSTR二选一,取决于 宏是否定义 ASNSI,Unicode
相互转换的方法:
LPWSTR->LPSTR: W2T()
LPTSRT->LPWSTR : T2W()
LPCWSTR->LPCSTR:W2CT()
LPCSTR->LPCWSTR:T2CW()
ANSI->UNICODE:A2W()
UNICODE->ANSI:W2A()
------------------------------
输出宽字符(Unicode)的正确方式:
wchar_t buf[100]=L"123";
printf("%ls",buf)
**/
int main(int argc, char * argv[])
{
LPCVOID pAddress = 0x00;
MEMORY_BASIC_INFORMATION memInfo;
char Type[10];
char Protect[10];
wchar_t dlpath[1024];
while (VirtualQuery(pAddress, &memInfo, sizeof(memInfo)) != 0)
{
ZeroMemory(Type, 10);
ZeroMemory(Protect, 10);
ZeroMemory(dlpath, 1024);
printf("基地址:0x%p |", memInfo.BaseAddress);
if (memInfo.State == MEM_FREE)
{
sprintf(Type, "FREE");
}
else if (memInfo.Type == MEM_IMAGE)
{
sprintf(Type, "Image");
}
else if (memInfo.Type == MEM_MAPPED)
{
sprintf(Type, "mapped");
}
else if (memInfo.Type==MEM_PRIVATE)
{
sprintf(Type, "private");
}
printf(" %s |", Type);
printf(" %d |", memInfo.RegionSize);
if (memInfo.AllocationProtect == PAGE_READONLY)
{
sprintf(Protect, "_R_");
}else if (memInfo.AllocationProtect == PAGE_READWRITE)
{
sprintf(Protect, "_RW_");
}else if (memInfo.AllocationProtect == PAGE_EXECUTE)
{
sprintf(Protect, "_E_");
}
else if (memInfo.AllocationProtect == PAGE_EXECUTE_READWRITE)
{
sprintf(Protect, "_ERW_");
}else if (memInfo.AllocationProtect == PAGE_EXECUTE_READ)
{
sprintf(Protect, "_ER_");
}else if (memInfo.AllocationProtect == PAGE_EXECUTE_WRITECOPY)
{
sprintf(Protect, "_ERC_");
}
printf(" %s |", Protect);
if (memInfo.AllocationProtect==PAGE_EXECUTE_WRITECOPY)
{
DWORD r=GetModuleFileName((HMODULE)memInfo.AllocationBase, dlpath, sizeof(dlpath));
}
printf(" %ls ", dlpath);
printf("\n");//换行
pAddress = (PVOID)((PBYTE)pAddress + memInfo.RegionSize);//指针运算 先转PBYTE是因为 RegionSize是Byte
}
while (1);
}
注意:1.这里是根据 RegionSize每次查询下一个区域的 位置,直到VirtualQuery 返回值为0.
2.还需要注意的是 LPWSTR是宽字符指针(Unicode),也就是wchar_t 指针,需要 %ls 格式化输出。。
SIZE_T WINAPI VirtualQuery(
_In_opt_ LPCVOID lpAddress,
_Out_ PMEMORY_BASIC_INFORMATION lpBuffer,
_In_ SIZE_T dwLength
);
源代码:
#include<stdio.h>
#include<Windows.h>
/**
*
数据类型总结:
BSTR:OLECHAR * 类型的Unicode字符串,时一个COM字符串,带长度前缀。
LPSTR: char*,指向以"\0"结尾的8位(单字节)ANSI字符数组指针
LPWSTR: wchar_t *,指向以 "\0"结尾的16位(双字节)Unicode字符数组指针
LPCSTR: const char *
LPCWSTR: const wchar_t*
LPTWSTR: LPSTR,LPWSTR二选一,取决于 宏是否定义 ASNSI,Unicode
相互转换的方法:
LPWSTR->LPSTR: W2T()
LPTSRT->LPWSTR : T2W()
LPCWSTR->LPCSTR:W2CT()
LPCSTR->LPCWSTR:T2CW()
ANSI->UNICODE:A2W()
UNICODE->ANSI:W2A()
------------------------------
输出宽字符(Unicode)的正确方式:
wchar_t buf[100]=L"123";
printf("%ls",buf)
**/
int main(int argc, char * argv[])
{
LPCVOID pAddress = 0x00;
MEMORY_BASIC_INFORMATION memInfo;
char Type[10];
char Protect[10];
wchar_t dlpath[1024];
while (VirtualQuery(pAddress, &memInfo, sizeof(memInfo)) != 0)
{
ZeroMemory(Type, 10);
ZeroMemory(Protect, 10);
ZeroMemory(dlpath, 1024);
printf("基地址:0x%p |", memInfo.BaseAddress);
if (memInfo.State == MEM_FREE)
{
sprintf(Type, "FREE");
}
else if (memInfo.Type == MEM_IMAGE)
{
sprintf(Type, "Image");
}
else if (memInfo.Type == MEM_MAPPED)
{
sprintf(Type, "mapped");
}
else if (memInfo.Type==MEM_PRIVATE)
{
sprintf(Type, "private");
}
printf(" %s |", Type);
printf(" %d |", memInfo.RegionSize);
if (memInfo.AllocationProtect == PAGE_READONLY)
{
sprintf(Protect, "_R_");
}else if (memInfo.AllocationProtect == PAGE_READWRITE)
{
sprintf(Protect, "_RW_");
}else if (memInfo.AllocationProtect == PAGE_EXECUTE)
{
sprintf(Protect, "_E_");
}
else if (memInfo.AllocationProtect == PAGE_EXECUTE_READWRITE)
{
sprintf(Protect, "_ERW_");
}else if (memInfo.AllocationProtect == PAGE_EXECUTE_READ)
{
sprintf(Protect, "_ER_");
}else if (memInfo.AllocationProtect == PAGE_EXECUTE_WRITECOPY)
{
sprintf(Protect, "_ERC_");
}
printf(" %s |", Protect);
if (memInfo.AllocationProtect==PAGE_EXECUTE_WRITECOPY)
{
DWORD r=GetModuleFileName((HMODULE)memInfo.AllocationBase, dlpath, sizeof(dlpath));
}
printf(" %ls ", dlpath);
printf("\n");//换行
pAddress = (PVOID)((PBYTE)pAddress + memInfo.RegionSize);//指针运算 先转PBYTE是因为 RegionSize是Byte
}
while (1);
}
注意:1.这里是根据 RegionSize每次查询下一个区域的 位置,直到VirtualQuery 返回值为0.
2.还需要注意的是 LPWSTR是宽字符指针(Unicode),也就是wchar_t 指针,需要 %ls 格式化输出。。
相关文章推荐
- windows查询端口使用情况以及根据PID查询进程
- 查看系统当前进程使用情况
- obj-c编程10:Foundation库中类的使用(4)[文件管理,查询当前进程信息]
- linux下通过go语言获得系统进程cpu使用情况的方法
- 通过plsql查询当前连接的数据库的配置情况,如查询oracle的安装地址,和oracle的tns配置
- 计算当前Exchange 2010 数据库的用户使用情况,通过PS命令行!
- Android使用adb查看当前设备运行进程CPU内存等资源的使用情况
- Android使用adb查看当前设备运行进程CPU内存等资源的使用情况
- 用VMQuery查询虚拟地址空间(虚拟内存)的使用情况
- Linux 统计当前所有进程使用swap分区的情况
- obj-c编程10:Foundation库中类的使用(4)[文件管理,查询当前进程信息]
- 禁用虚拟内存后进程的内存使用情况
- 通过进程号杀死进程或查询进程以及使用案例
- obj-c编程10:Foundation库中类的使用(4)[文件管理,查询当前进程信息]
- SQL大圣之路笔记——SQL 通过创建存储过程,查看当前进程情况(是否造成表锁死等)
- 如何通过 SQL Server 链接服务器和分布式查询使用 Excel
- 使用netstat命令查看当前端口占用情况
- 通过AIX性能工具,查询最耗资源的oracle进程
- 通过AIX性能工具,查询最耗资源的oracle进程
- oracle格式化查询(start with ,connect by prior )使用情况