I want to get my system hardware information with c++ code.
So how to get system information in windows with c++ ?
You can use the GetSystemInfo function to obtain hardware information such as the OEM identifier, processor type, page size, and so on.
#include <windows.h>
#include <stdio.h>
#pragma comment(lib, "user32.lib")
void main()
{
SYSTEM_INFO siSysInfo;
// Copy the hardware information to the SYSTEM_INFO structure.
GetSystemInfo(&siSysInfo);
// Display the contents of the SYSTEM_INFO structure.
printf("Hardware information: \n");
printf(" OEM ID: %u\n", siSysInfo.dwOemId);
printf(" Number of processors: %u\n",
siSysInfo.dwNumberOfProcessors);
printf(" Page size: %u\n", siSysInfo.dwPageSize);
printf(" Processor type: %u\n", siSysInfo.dwProcessorType);
printf(" Minimum application address: %lx\n",
siSysInfo.lpMinimumApplicationAddress);
printf(" Maximum application address: %lx\n",
siSysInfo.lpMaximumApplicationAddress);
printf(" Active processor mask: %u\n",
siSysInfo.dwActiveProcessorMask);
}
References:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms724423(v=vs.85).aspx
https://msdn.microsoft.com/en-us/library/windows/desktop/ms724381(v=vs.85).aspx
http://www.cplusplus.com/forum/windows/124624/
Processor Info
Use void WINAPI GetSystemInfo(..) (MSDN)
This returns a SYSTEM_INFO struct that contains:
typedef struct _SYSTEM_INFO {
union {
DWORD dwOemId;
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;
Enumerate devices
Use the IPortableDeviceManager interface to enumerate devices. MSDN describes This API in details.
Here is a code snippet from that page:
// Create PortableDeviceManager
HRESULT hr = CoCreateInstance(CLSID_PortableDeviceManager,
NULL,
CLSCTX_INPROC_SERVER,
IID_PPV_ARGS(&pPortableDeviceManager));
if (FAILED(hr))
{
printf("! Failed to CoCreateInstance CLSID_PortableDeviceManager, hr = 0x%lx\n",hr);
return;
}
// Get number of devices
if (SUCCEEDED(hr))
{
hr = pPortableDeviceManager->GetDevices(NULL, &cPnPDeviceIDs);
if (FAILED(hr))
{
printf("! Failed to get number of devices on the system, hr = 0x%lx\n",hr);
}
}
// Report the number of devices found. NOTE: we will report 0, if an error
// occured.
printf("\n%d Windows Portable Device(s) found on the system\n\n", cPnPDeviceIDs);
// Enumerate the device information
if (SUCCEEDED(hr) && (cPnPDeviceIDs > 0))
{
pPnpDeviceIDs = new (std::nothrow) PWSTR[cPnPDeviceIDs];
if (pPnpDeviceIDs != NULL)
{
DWORD dwIndex = 0;
hr = pPortableDeviceManager->GetDevices(pPnpDeviceIDs, &cPnPDeviceIDs);
if (SUCCEEDED(hr))
{
// For each device found, display the devices friendly name,
// manufacturer, and description strings.
for (dwIndex = 0; dwIndex < cPnPDeviceIDs; dwIndex++)
{
printf("[%d] ", dwIndex);
DisplayFriendlyName(pPortableDeviceManager, pPnpDeviceIDs[dwIndex]);
printf(" ");
DisplayManufacturer(pPortableDeviceManager, pPnpDeviceIDs[dwIndex]);
printf(" ");
DisplayDescription(pPortableDeviceManager, pPnpDeviceIDs[dwIndex]);
}
}
else
{
printf("! Failed to get the device list from the system, hr = 0x%lx\n",hr);
}
}
}
// Free memory used to store the information
for (dwIndex = 0; dwIndex < cPnPDeviceIDs; dwIndex++)
{
CoTaskMemFree(pPnpDeviceIDs[dwIndex]);
pPnpDeviceIDs[dwIndex] = NULL;
}
// Delete the array of PWSTR pointers
delete [] pPnpDeviceIDs;
pPnpDeviceIDs = NULL;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With