This article is an update to my previous articles on the same topic. I started this utility to get some basic information about the hardware. After that I have received some emails from some of our fellow programmers, who are frequent visitors of this site, asking to extend this utility to spit out some more information.
In this update I have added the information about CPU vendor (Intel, Cyrix, AMD, etc.) ,CPU speed, and Physical memory status of sustem .
CPU Information:
To get CPU information I could not find any direct and simple API call. Then I thought of our friendly Registry, which contains all the system information. And there it was all the information I wanted. If you look under HKEY_LOCAL_MACHINE/Hardware/Description/System/CentralProcessor/0 key in registry, you will find the answers to your questions. ~MHz subkey gives the CPU speed and VendorIdentifier gives the vendor information.
We will make use of RegQueryValueEx API call to get the information.
LONG RegQueryValueEx(
HKEY hKey,
LPTSTR lpValueName,
LPDWORD lpReserved,
LPDWORD lpType,
LPBYTE lpData,
LPDWORD lpcbData
);
Before making this call, it is necessary to create the handle to registry key which needs to be queried. For this make use of RegCreateKeyEx API call.
(continued)
This is how the code looks like in the included code for this utility.
result = ::RegOpenKeyEx (HKEY_LOCAL_MACHINE,
"Hardware\\Description\\System\\CentralProcessor\\0", 0, KEY_QUERY_VALUE, &hKey);
if (result == ERROR_SUCCESS) {
result = ::RegQueryValueEx (hKey, _T("~MHz"), NULL, NULL,
(LPBYTE)&data, &dataSize);
m_stCPUSpeed.Format ("%d", data);
m_stCPUSpeed += _T (" MHz");
dataSize = sizeof (vendorData);
result = ::RegQueryValueEx (hKey, _T("VendorIdentifier"), NULL, NULL,
(LPBYTE)vendorData, &dataSize);
m_stVendorInfo.Format ("%s", vendorData);
}
RegCloseKey (hKey);
Since I don't have access to any other CPU than Intel. So I couldn't test this code on CPU's from Cyrix, AMD, etc.
Physical Memory Status:
To get the information about the memory status of the system we can take routes. First one, which is not easy for a person who does not know assembly language, involves getting the required information from CMOS data. Second one which simply involves making GlobalMemoryStatus API call. And I chose the second one (Ofcourse I am not assembly language pro).
VOID GlobalMemoryStatus (
LPMEMORYSTATUS lpBuffer
);
The information is returned in MEMORYSTATUS data structure.
typedef struct _MEMORYSTATUS {
DWORD dwLength;
DWORD dwMemoryLoad;
DWORD dwTotalPhys;
DWORD dwAvailPhys;
DWORD dwTotalPageFile;
DWORD dwAvailPageFile;
DWORD dwTotalVirtual;
DWORD dwAvailVirtual;
} MEMORYSTATUS, *LPMEMORYSTATUS;
The information returned by the GlobalMemoryStatus function is volatile. There is no guarantee that two sequential calls to this function will return the same information. This API call has been made in GetMemoryInfo function of the application attached. I gets the information for memory usage, total physical memory instaled, physical memory available, and total virtual memory.
Download source - 27 KB