1,涉及API
GetSystemInfo 原型:
void WINAPI GetSystemInfo
(
_Out_ LPSYSTEM_INFO lpSystemInfo
);
SYSTEM_INFO 结构在前文介绍过:
其中包含了处理器的核心数目。
typedef struct _SYSTEM_INFO {
union {
DWORD dwOemId; // 已废弃的成员,保留这个成员是为了向以前版本的Windows NT保持兼容
struct {
WORD wProcessorArchitecture; //指定系统中的中央处理器的体系结构
WORD wReserved;
};
};
DWORD dwPageSize;
LPVOID lpMinimumApplicationAddress;
LPVOID lpMaximumApplicationAddress;
DWORD_PTR dwActiveProcessorMask;
DWORD dwNumberOfProcessors; //处理器的核心数
DWORD dwProcessorType;
DWORD dwAllocationGranularity;
WORD wProcessorLevel;
WORD wProcessorRevision;
} SYSTEM_INFO, *LPSYSTEM_INFO;
2,使用
//获取CPU核心数目
DWORD GetProcessorNumber()
{
SYSTEM_INFO si;
memset(&si,0,sizeof(SYSTEM_INFO));
GetSystemInfo(&si);
return si.dwNumberOfProcessors;
}