Getting a sensor value from a piece of hardware requires direct access to that hardware via memory-mapped model-specific registers (MSRs), which not only are different in different CPUs but can only be done in a kernel (driver) and not in user-space application. It took me several weeks and several headaches researching it and trying different things before i got a working solution. I must say this topic is a complete nightmare. You can use a library like this and it'll be doing this under the hood, or you can write equivalent code of your own. In both cases these are, by definition, how the hardware exposes information about its temperature sensors. AMD will have equivalent documentation somewhere that will define this. The corresponding AMD code looks like it gets similar info out of a PCI register. The OpenHardwareMonitorLib uses Rdmsr and RdmsrTx to get temperature values out of the MSRs of interest. They may also be documented in the Intel Software Developer Manual but I haven't checked. These are straight out of Intel's docs like CPU Monitoring with DTS/PECI (section 16 "Direct MSR Access"). Private const uint MSR_PP1_ENERY_STATUS = 0圆41 Private const uint MSR_PP0_ENERY_STATUS = 0圆39 Private const uint MSR_DRAM_ENERGY_STATUS = 0圆19 Private const uint MSR_PKG_ENERY_STATUS = 0圆11 Private const uint MSR_RAPL_POWER_UNIT = 0圆06 Private const uint IA32_PACKAGE_THERM_STATUS = 0x1B1 Private const uint MSR_PLATFORM_INFO = 0xCE Private const uint IA32_PERF_STATUS = 0x0198 Private const uint IA32_TEMPERATURE_TARGET = 0x01A2 For example, the IntelCPU class defines some model-specific registers: private const uint IA32_THERM_STATUS_MSR = 0x019C The link to source for OpenHardwareMonitorLib provided in Tomer's answer illustrates what has to happen at a low level to read this information out of different types of CPUs. For example, a temperature sensor for every core, so don't always take the last one as I did in the example above. Note that the CPU of the user can have multiple temperature sensors. You can get a lot of other information about the system with this library. It will most likely work for most other CPUs from AMD and Intel. This C# code gets the temperature of the CPU in Celcius. If (sensor.SensorType = SensorType.Temperature) If (hardware.HardwareType = HardwareType.CPU)įoreach (ISensor sensor in hardware.Sensors) After reading the source for a while this is my take: using OpenHardwareMonitor.Hardware įoreach (IHardware hardware in computer.Hardware) So considering it has no docs it can be a bit hard to work with. How to call a C# library from Native C (using C \CLI and IJW) Fortunately you can get it as a DLL, and the GUI is completely separated from the actual backend which is OpenHardwareMonitorLib. NET C Sharp and of course, only works for Windows. It's part of an open source software named "Open Hardware Monitor". Unfortunately, it has no documentation and actually doesn't really exist as an independent piece of software. Truth to be told, it depends on the hardware.Ī library that works on most hardware is OpenHardwareMonitorLib. Hr = pObject->Get(temp, 0,
0 Comments
Leave a Reply. |