最近在学习 Win32编程,所以顺便将每日所学记录下来,一方面为了巩固学习的知识,另一方面也为同样在学习Win32开发的童鞋们提供一份参考。
本系列博文均根据学习《Windows核心编程》一书总结而来;
运行环境:
操作系统: Windows 10家庭版
编译器:Visual Studio 2019
虚拟内存 系统信息 许多操作系统的值是根据主机设定的,可以使用GetSystemInfo函数检索主机相关的值;
1 VOID GetSystemInfo ( LPSYSTEM_INFO lpSystemInfo) ;
SYSTEM_INFO结构体:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 typedef struct _SYSTEM_INFO { union { DWORD dwOemId; struct { WORD wProcessorArchitecture; WORD wReserved; } DUMMYSTRUCTNAME; } DUMMYUNIONNAME; DWORD dwPageSize; LPVOID lpMinimumApplicationAddress; LPVOID lpMaximumApplicationAddress; DWORD_PTR dwActiveProcessorMask; DWORD dwNumberOfProcessors; DWORD dwProcessorType; DWORD dwAllocationGranularity; WORD wProcessorLevel; WORD wProcessorRevision; } SYSTEM_INFO, *LPSYSTEM_INFO;
虚拟内存状态 WIndows函数GlobalMemoryStatus可用于检索关于当前内存状态的动态信息 ;
1 void GlobalMemoryStatus ( LPMEMORYSTATUS lpBuffer ) ;
_MEMORYSTATUS结构体:
1 2 3 4 5 6 7 8 9 10 typedef struct _MEMORYSTATUS { DWORD dwLength; DWORD dwMemoryLoad; SIZE_T dwTotalPhys; SIZE_T dwAvailPhys; SIZE_T dwTotalPageFile; SIZE_T dwAvailPageFile; SIZE_T dwTotalVirtual; SIZE_T dwAvailVirtual; } MEMORYSTATUS, *LPMEMORYSTATUS;
确定地址空间状态 Windows提供了VirtualQuery函数以查询地址空间中内存地址的某些信息;
1 2 3 DWORD VirtualQuery (LPCVOID lpAddress, PMEMORY_BASIC_INFORMATION lpBuffer, DWORD dwLength ) ;
Windows还提供了另一个函数,它使一个进程能够查询另一个进程的内存信息;
1 2 3 4 DWORD VirtualQueryEx ( HANDLE hProcess, LPCVOID lpAddress, PMEMORY_BASIC_INFORMATION lpBuffer, SIZE_T dwLength
如果我们需要调用这两个函数,我们需要使用一个结构体:
1 2 3 4 5 6 7 8 9 10 11 12 typedef struct _MEMORY_BASIC_INFORMATION { PVOID BaseAddress; PVOID AllocationBase; DWORD AllocationProtect; #if defined (_WIN64) WORD PartitionId; #endif SIZE_T RegionSize; DWORD State; DWORD Protect; DWORD Type; } MEMORY_BASIC_INFORMATION, *PMEMORY_BASIC_INFORMATION;