Win查询注册表获取CPU与内存参数

#include "stdafx.h"
void
	dump_machine_info(
	HANDLE fh
	)
{
	CHAR Str[MAX_PATH*3];
	CHAR MHzStr[64];
	CHAR CpuId[MAX_PATH];
	CHAR VendorId[MAX_PATH];
	TCHAR RegPath[MAX_PATH];
	ULONG i;
	HKEY hKey;
	ULONG MHz;
	DWORD t;
	ULONG l;
	MEMORYSTATUS          ms  ;

	for(i=0; i<128; i++) {
		wsprintf(RegPath, _T("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\%u"), i);
		if(RegOpenKey(HKEY_LOCAL_MACHINE, RegPath, &hKey) != ERROR_SUCCESS) {
			break;
		}
		// MHz
		l = sizeof(ULONG);
		if(RegQueryValueEx(hKey, _T("~MHz"), NULL, &t, (PUCHAR)&MHz, &l) != ERROR_SUCCESS) {
			sprintf(MHzStr, "?");
		} else {
			sprintf(MHzStr, "%u", MHz);
		}
		// CPU Type
		l = MAX_PATH-1;
		if(RegQueryValueEx(hKey,_T( "Identifier"), NULL, &t, (PUCHAR)CpuId, &l) != ERROR_SUCCESS) {
			sprintf(CpuId, "Unknown type");
		} else {
			CpuId[MAX_PATH-1] = 0;
		}
		// CPU Vendor
		l = MAX_PATH-1;
		if(RegQueryValueEx(hKey, _T("VendorIdentifier"), NULL, &t, (PUCHAR)VendorId, &l) != ERROR_SUCCESS) {
			sprintf(CpuId, "Unknown vendor");
		} else {
			VendorId[MAX_PATH-1] = 0;
		}
		l = sprintf(Str, "CPU-%u: %s, %s, %s MHz\n", i, CpuId, VendorId, MHzStr);
	//	TeeOutput(fh, Str, l);
		printf(Str);
		RegCloseKey(hKey);
	}
	ms.dwLength = sizeof (ms) ;
	GlobalMemoryStatus  (&ms) ;
	l = sprintf(Str, "Memory: %u Mb physical, %u Mb virtual\n", (ms.dwTotalPhys)/(1024*1024)+1, (ms.dwTotalVirtual)/(1024*1024)+1);
	//TeeOutput(fh, Str, l);
	printf(Str);
} // end dump_machine_info()

int wmain()
{
	dump_machine_info(NULL);
	return 0;
}

 

CPU-0: x, G, 2527 MHz
CPU-1: x, G, 2527 MHz
CPU-2: x, G, 2527 MHz
CPU-3: x, G, 2527 MHz
Memory: 1909 Mb physical, 2048 Mb virtual

posted @ 2014-02-23 12:03  godjob  Views(1343)  Comments(0Edit  收藏  举报