window系统内存获取函数【C++】

本文介绍了Windows系统中通过GetPerformanceInfo和GlobalMemoryStatusEx函数获取系统资源信息的方法,并提供了测试代码及运行结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

windows系统资源的获取函数主要有2个:GetPerformanceInfo  和 GlobalMemoryStatusEx 定义如下:

<pre name="code" class="cpp">	typedef struct _PERFORMANCE_INFORMATION {
		DWORD cb;						// 结构体数据大小 sizeof(_PERFORMANCE_INFORMATION)
		SIZE_T CommitTotal;					// 
		SIZE_T CommitLimit;
		SIZE_T CommitPeak;
		SIZE_T PhysicalTotal;					// 物理内存总大小(单位:页)
		SIZE_T PhysicalAvailable;				// 物理内存可用大小(单位:页)
		SIZE_T SystemCache;					// 系统缓存(物理内存总大小+硬盘划分的虚拟内存总大小)(单位:页)
		SIZE_T KernelTotal;					// 
		SIZE_T KernelPaged;
		SIZE_T KernelNonpaged;
		SIZE_T PageSize;					// 页大小(单位:字节)
		DWORD HandleCount;
		DWORD ProcessCount;
		DWORD ThreadCount;
	} PERFORMANCE_INFORMATION, *PPERFORMANCE_INFORMATION, PERFORMACE_INFORMATION, *PPERFORMACE_INFORMATION;

	BOOL
		WINAPI
		GetPerformanceInfo (
		PPERFORMACE_INFORMATION pPerformanceInformation,
		DWORD cb
		);

	typedef struct _MEMORYSTATUSEX {
		DWORD dwLength;						// 结构体数据大小 sizeof(_MEMORYSTATUSEX)
		DWORD dwMemoryLoad;					// 内存使用率
		DWORDLONG ullTotalPhys;					// 物理内存总大小(单位:字节)
		DWORDLONG ullAvailPhys;					// 物理内存可用大小(单位:字节)
		DWORDLONG ullTotalPageFile;				// 总页面大小(物理内存总大小+硬盘划分的虚拟内存总大小)(单位:字节)
		DWORDLONG ullAvailPageFile;				// 当前进程能获取的最大内存数(单位:字节)
		DWORDLONG ullTotalVirtual;				// 当前进程最大内存寻址地址(默认2GB,开启大地址后就是4GB)
		DWORDLONG ullAvailVirtual;				// 当前进程可用最大内存(单位:字节)
		DWORDLONG ullAvailExtendedVirtual;
	} MEMORYSTATUSEX, *LPMEMORYSTATUSEX;

	WINBASEAPI
		BOOL
		WINAPI
		GlobalMemoryStatusEx(
		__out LPMEMORYSTATUSEX lpBuffer
		);



两个接口有些数据是相同的,有些是有相互关系的,这个在MSDN上可以查到。另外GetPerformanceInfo  这个接口需要调用Psapi库,简单的测试代码如下:

</pre><p><pre name="code" class="cpp">#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include "Psapi.h"

#pragma comment(lib, "Psapi.Lib")

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	cout << "GetPerformanceInfo接口测试:" << endl;
	int nMB = 1024 * 1024;
	//ProcessQUERY(5);
	PERFORMANCE_INFORMATION stPerformance = {0};
	int cb = sizeof(stPerformance);
	GetPerformanceInfo(&stPerformance, cb);
	int nPageSize = stPerformance.PageSize;
	INT64 i64PhyTotal = (INT64)stPerformance.PhysicalTotal * nPageSize / nMB;
	INT64 i64PhyAvail = (INT64)stPerformance.PhysicalAvailable * nPageSize / nMB;
	cout << "Commit Total:" << (INT64)stPerformance.CommitTotal * nPageSize / nMB << endl;
	cout << "Commit Limit:" << (INT64)stPerformance.CommitLimit * nPageSize / nMB << "MB\t" << "Commit peak:\t" 
<span style="white-space:pre">	</span>     << (INT64)stPerformance.CommitPeak * nPageSize / nMB << "MB\t" << endl;
	cout << "物理内存:\t" << "总量:\t" << i64PhyTotal << "MB\t" << "剩余量:\t" << i64PhyAvail << "MB\t" << endl;
	cout << "System Cache:" << (INT64)stPerformance.SystemCache * nPageSize / (1024 * 1024) << "MB\t" << endl;
	cout << "Kernel Total:" << (INT64)stPerformance.KernelTotal * nPageSize / (1024 * 1024) << "MB\t" << "Kernel paged:\t" 
<span style="white-space:pre">	</span>     << (INT64)stPerformance.KernelPaged * nPageSize / (1024 * 1024) << "MB\t" 
	     << "Kernel Nopaged:" << (INT64)stPerformance.KernelNonpaged * nPageSize / nMB << "MB" << endl;
	cout << "Page Size:" << stPerformance.PageSize  << "\t" << endl;
	cout << "Handle count:" << stPerformance.HandleCount  << "\t" << endl;
	cout << "Process count:" << stPerformance.ProcessCount  << "\t" << endl;
	cout << "Thread count:" << stPerformance.ThreadCount  << "\t" << endl;


	cout << "GlobalMemoryStatusEx接口测试:" << endl;
	MEMORYSTATUSEX stMem = {0};
	stMem.dwLength = sizeof(stMem);
	GlobalMemoryStatusEx(&stMem);
	cout << "内存使用率:" << stMem.dwMemoryLoad << '%' << endl;
	cout << "物理内存:\t" << "总量:\t" << stMem.ullTotalPhys / (1024 * 1024) << "MB\t" << "剩余量:\t" 
<span style="white-space:pre">		</span><< stMem.ullAvailPhys / (1024 * 1024) << "MB\t" << endl;
	cout << "页面文件:\t" << "总量:\t" << stMem.ullTotalPageFile / (1024 * 1024) << "MB\t" << "剩余量:\t" 
<span style="white-space:pre">		</span><< stMem.ullAvailPageFile / (1024 * 1024) << "MB\t" << endl;
	cout << "虚拟内存:\t" << "总量:\t" << stMem.ullTotalVirtual / (1024 * 1024) << "MB\t" << "剩余量:\t" 
<span style="white-space:pre">		</span><< stMem.ullAvailVirtual / (1024 * 1024) << "MB\t" << endl;
	return 0;
}



测试结果如下:


WindowsC++ 编程中,要获取共享内存中的内容,可以使用以下步骤: 1. 使用 `CreateFileMapping` 函数创建一个共享内存对象,该函数返回一个句柄。 2. 使用 `MapViewOfFile` 函数将共享内存对象映射到当前进程的地址空间中,该函数返回一个指向共享内存区域的指针。 3. 通过指针读取或写入共享内存中的数据。 4. 使用 `UnmapViewOfFile` 函数取消共享内存区域的映射。 5. 使用 `CloseHandle` 函数关闭共享内存对象句柄。 以下是一个简单的示例代码: ```c++ #include <Windows.h> #include <iostream> int main() { // 创建一个共享内存对象 HANDLE hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 1024, L"MySharedMemory"); if (hMapFile == NULL) { std::cout << "CreateFileMapping failed, error: " << GetLastError() << std::endl; return 1; } // 将共享内存区域映射到当前进程的地址空间中 LPVOID lpMapAddress = MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 1024); if (lpMapAddress == NULL) { std::cout << "MapViewOfFile failed, error: " << GetLastError() << std::endl; CloseHandle(hMapFile); return 1; } // 读取共享内存中的数据 char* pData = (char*)lpMapAddress; std::cout << "Data in shared memory: " << pData << std::endl; // 取消共享内存区域的映射 UnmapViewOfFile(lpMapAddress); // 关闭共享内存对象句柄 CloseHandle(hMapFile); return 0; } ``` 在上面的示例代码中,我们创建了一个大小为 1024 字节的共享内存对象,并将其命名为 "MySharedMemory"。然后,我们将其映射到当前进程的地址空间中,并通过指针读取了共享内存中的数据。最后,我们取消了共享内存区域的映射并关闭了共享内存对象句柄。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值