AllThingsGeek

Just another WordPress.com weblog

Accessing SpeedFan data from C++ — July 30, 2008

Accessing SpeedFan data from C++

Cursed out loud last night. As it turns out the rest of the world is only using Delphi and C# to access SpeedFan’s shared memory. The documentation was vague and the users were clueless. Digging around long enough, I found some info on the shared memory used by SpeedFan.

Apparently it publishes a shared memory area named “SFSharedMemory_ALM”, which looks like this:

TSharedMem=packed record
version:word;
flags :word;
MemSize:integer;
handle :THandle;
NumTemps:word;
NumFans :word;
NumVolts:word;
temps:array[0..31] of integer;
fans :array[0..31] of integer;
volts:array[0..31] of integer;
end;

Okay, so if we port that to C++ we end up with the following:

// pragma pack is included here because the struct is a pascal Packed Record,
// meaning that fields aren’t aligned on a 4-byte boundary. 4 bytes fit 2
// 2-byte records.

#pragma pack(1)

// This is the struct we’re using to access the shared memory.
struct SFMemory {
WORD version;
WORD flags;
int MemSize;
int handle;
WORD NumTemps;
WORD NumFans;
WORD NumVolts;
signed int temps[32];
signed int fans[32];
signed int volts[32];
};

Brilliant. Now all we have to do is open the shared memory mapped file:

// Open file
HANDLE file = (HANDLE)CreateFile(filename,GENERIC_READ | GENERIC_WRITE, 0,
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
// Create file mapping
HANDLE filemap = (HANDLE)CreateFileMapping(file,NULL,PAGE_READWRITE,0,nSize,mapname);
// Get pointer
SFMemory* sfmemory = (SFMemory*)MapViewOfFile(filemap, FILE_MAP_READ, 0, 0, nSize);

We can now access sfmemory to retrieve the information we’re after. To retrieve the temperature from the first temperature sensor we just do sfmemory->temps[0]. The value returned should be divided by 100 to return the actual temperature. As for what the labels are for the sensors, I know of no good and reliable way to assign that. But now we can at least read the values.

A possible aproach would be to show a listbox with a label going “CPU Temp Sensor”, listing all the available sensors and allowing the user to select one.

Continue reading

Design a site like this with WordPress.com
Get started