How do I get Hardware Info in C++/MFC?

Started by
7 comments, last by Yanna 19 years, 9 months ago
Hello, How do I get Hardware Info (CPU, VGA Card, RAM, Ports, etc, etc) in C++/MFC... Any suggestions... (links to some aritcals would really help)..
A programmer's national anthem; "AAAAAHHHHH!!!!!"
Advertisement
Look into the GetSystemInfo and GetSystemMetrics functions. Also the SYSTEM_INFO structure may be useful. They are Win32 (not MFC), but should give you a starting point.
"When you die, if you get a choice between going to regular heaven or pie heaven, choose pie heaven. It might be a trick, but if it's not, mmmmmmm, boy."
How to Ask Questions the Smart Way.
Thanks, I am fetching:

1. processor information from: HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor\0 .

2. RAM Info from: GlobalMemoryStatus()

3. Hard-Disk Info from: GetVolumeInformation()

Any suggestions about VGA, Sound Card and Ports (USB, Serial, Communication)...

Bye,
A programmer's national anthem; "AAAAAHHHHH!!!!!"

Dont know if you want to include the libraries just for graphics info but opengl/directx have functions which supply the information.

i'm sure theres a better way.

/jeff
For vga cards I think EnumDisplayDevices will do the trick. And most devices seems to registered under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum.
For sound card you can use:
waveOutGetNumDevs
waveOutGetDevCaps
from mmsystem

However, if you do decide to use DirectX to query the hardware you can get a lot more information from direct sound and directshow. Also you can obviously use direct3d to get information about the graphics hardware. You can do similar with opengl.
A simple capabilites viewing app is here
I'm just trying it out myself now to get info from the ports, but I cant seem to get it working.
#include <windows.h>#include <Setupapi.h>#include <Stdio.h>//#include <devguid.h> //includes all the guidHDEVINFO DoDeviceEnum( GUID InterfaceClassGuid ){	long Err;    HDEVINFO DeviceInfoSet;    HDEVINFO NewDeviceInfoSet;    DeviceInfoSet = SetupDiCreateDeviceInfoList(NULL, NULL);        if(DeviceInfoSet == INVALID_HANDLE_VALUE) {        Err = GetLastError();        printf( "SetupDiCreateDeviceInfoList failed: %lx.\n", Err );        return 0;    }    NewDeviceInfoSet = SetupDiGetClassDevsEx( &InterfaceClassGuid, //class guid        NULL, //enumerator        NULL, //hwndParent        DIGCF_PRESENT | DIGCF_DEVICEINTERFACE, //flags		DeviceInfoSet,        NULL, //machine name        NULL //reserved    );    if(NewDeviceInfoSet == INVALID_HANDLE_VALUE)     {        Err = GetLastError();        printf( "SetupDiGetClassDevsEx failed: %lx.\n", Err );        return 0;    }    return NewDeviceInfoSet;}//taken from vc6 devguid.hDEFINE_GUID( GUID_DEVCLASS_PORTS,          0x4d36e978L, 0xe325, 0x11ce, 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18 );int main() {	HDEVINFO hdi;	GUID ports = GUID_DEVCLASS_PORTS;	hdi=DoDeviceEnum(ports);	SP_DEVICE_INTERFACE_DATA  sdid;	memset(&sdid,0,sizeof(SP_DEVICE_INTERFACE_DATA));	sdid.cbSize=sizeof(SP_DEVICE_INTERFACE_DATA);	if (!SetupDiEnumDeviceInterfaces(hdi,		NULL,		&ports,		0, //index		&sdid) ) {			printf("error enumerating devices\n");			return 0;		}	printf("%d\n",ports);	printf("%d\n",sdid.InterfaceClassGuid);	printf("%d\n",sdid.Flags);	PSP_INTERFACE_DEVICE_DETAIL_DATA pDevDetail = NULL;	DWORD Size; 	SetupDiGetDeviceInterfaceDetail( 	hdi, &sdid, NULL, 0, &Size, NULL ); 	pDevDetail = (PSP_INTERFACE_DEVICE_DETAIL_DATA)malloc(Size); 	pDevDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA); 	SP_DEVINFO_DATA DevInfoData; 	DevInfoData.cbSize = sizeof(SP_DEVINFO_DATA); 	if(!SetupDiGetDeviceInterfaceDetail( hdi, &sdid, pDevDetail, Size, NULL, &DevInfoData)) { printf("failed to get device detail\n");return 0;} printf("device name:%s\n",pDevDetail->DevicePath);char *buff = NULL;DWORD dwData;DWORD buffersize = 0;//get sizeif (!SetupDiGetDeviceRegistryProperty(hdi,								 &DevInfoData,								 SPDRP_DEVTYPE,								 &dwData, //?								 (BYTE *)buff,								 buffersize,								 &buffersize) 								 ) {									 DWORD error=GetLastError();									 printf("error:%d\n",error);									 printf("failed1\n");									 return 0;								 }buff = new char[buffersize+1];//put dataif (!SetupDiGetDeviceRegistryProperty(hdi,								 &DevInfoData,								 SPDRP_DEVTYPE,								 &dwData, //?								 (BYTE *)buff,								 buffersize,								 &buffersize)								 )  {									 printf("failed2\n");									 return 0;								 }printf("%s\n",buff);SetupDiDestroyDeviceInfoList(hdi); // destroy device information list 	return 0;}

The code is pritty much just cut and pasted from msdn with bits filled in.

I get this output:
1295444344
1295444344
1
device name:\\?\acpi#pnp0501#1#{4d36e978-e325-11ce-bfc1-08002be10318}
error:13
failed1


But I cant see whats wrong.. (ERROR_INVALID_DATA?)

Anyway, I'm assuming I'm on the right track, so you just need to lookup the SetupDiCreateDeviceInfoList & related functions. If anyone manages to get something working please post the code.
Thanks a lot bro (aboeing), I'll not be working on that assignment for a week or so... (Actually have another assignment to make first, It's a game of NIM in MFC in which the computer always wins (or mostly wins) )

Will post the code for this one as soon as I start working on this one again (and most importantly, get anything working ;) )

Bye,
A programmer's national anthem; "AAAAAHHHHH!!!!!"
Does any one know how can I find out the memory size of the VGA card?

This topic is closed to new replies.

Advertisement