Detecting hardware

Discussion about everything. New games, 3d math, development tips...
Post Reply
Anteater

Detecting hardware

Post by Anteater »

Hi. Does anyone know how to

A) Check the CPU speed
B) Detect RAM
C) Detect video card memory

All without using DirectX (I use MinGW)?
Guest

Post by Guest »

There is probably some functions in the mingw standard header files that allows this, google it.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

This should get you A and B. Should work on Linux and Windows both. Havent' fully tested them though.

Code: Select all

bool getProcessorSpeedMHz(irr::u32* MHz)
{
#if defined(_IRR_WINDOWS_)
	LONG Error;
	
	HKEY Key;
	Error = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
			"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",
			0, KEY_READ, &Key);
   if(Error != ERROR_SUCCESS)
      return false;

	DWORD Speed = 0;
	DWORD Size = sizeof(Speed);
   Error = RegQueryValueEx(Key, "~MHz", NULL, NULL, (LPBYTE)&Speed, &Size);

	RegCloseKey(Key);

	if (Error != ERROR_SUCCESS)
		return false;
	else if (MHz)
		*MHz = Speed;
	return true;
#elif defined(LINUX)
	// could probably read from "/proc/cpuinfo" or "/proc/cpufreq" also
	struct clockinfo CpuClock;
	size_t Size = sizeof(clockinfo);

	if (!sysctlbyname("kern.clockrate", 2, &CpuClock, &Size, NULL, 0))
		return false;
	else if (MHz)
		*MHz = CpuClock.hz;
	return true;
#else
	return false;
#endif
}

bool getSystemMemory(irr::u32* Total, irr::u32* Avail)
{
#if defined(_IRR_WINDOWS_)
	MEMORYSTATUS MemoryStatus;
	MemoryStatus.dwLength = sizeof(MEMORYSTATUS);

	// cannot fail
	GlobalMemoryStatus(&MemoryStatus);

	if (Total)
		*Total = (irr::u32)MemoryStatus.dwTotalPhys;
	if (Avail)
		*Avail = (irr::u32)MemoryStatus.dwAvailPhys;
	return true;
#elif defined(LINUX)
	// could probably read from "/proc/meminfo"
	struct sysinfo SystemStatus;
	if (!sysinfo(&SystemStatus))
		return false;

	if (Total)
		*Total = SystemStatus.totalram;
	if (Avail)
		*Avail = SystemStatus.freeram;
	return true;
#else
	return false;
#endif
}
Anteater

Post by Anteater »

Thanks a ton. Oh, by the way, do you mind me using this code in a (possibly) commercial project?
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Do with it as you wish, it has been posted in a public forum.
mm765
Posts: 172
Joined: Fri May 28, 2004 10:12 am

Post by mm765 »

thanks vitek, one small corerction:

sysinfo returns 0 if ok, so
if (!sysinfo(&SystemStatus))

must be
if (sysinfo(&SystemStatus) != 0)
Post Reply