您的位置:首页 - C++
获取CPU的个数Windows程序
void ShowProcessors()
{
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION pbuffer = NULL;
DWORD dwSize = 0;
DWORD procCoreCount;

BOOL bResult = GetLogicalProcessorInformation(pbuffer, &dwSize);
if(GetLastError() != ERROR_INSUFFICIENT_BUFFER)
{
_tprintf(TEXT("Impossible to get processor information\n"));
return ;
}

pbuffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc(dwSize);
bResult = GetLogicalProcessorInformation(pbuffer, &dwSize);

if(!bResult)
{
free(pbuffer);

_tprintf(TEXT("Impossible to get processor information\n"));
return ;
}

procCoreCount = 0;
DWORD lpiCount = dwSize / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);

for (DWORD current = 0; current < lpiCount; current++)
{
if(pbuffer[current].Relationship == RelationProcessorCore)
{
if(pbuffer[current].ProcessorCore.Flags == 1)
{
_tprintf(L"+ one CPU core (HyperThreading)\n");
}
else
_tprintf(L"+ one CPU socker\n");

procCoreCount++;
}

}

_tprintf(TEXT("-> %d active CPU(s)\n"), procCoreCount);
}
本站导航
相关文章
柯乐义 Copyright© keleyi.com