Reading Cpu Temp Windows 10 C++ Visual Studio

  • Download Application - 493.viii KB
  • Download Source - ii.2 MB

Introduction

Since I utilize a homebuilt PC, I wanted a simple awarding to display my CPU temperature unobstrusively on my desktop.

Groundwork

This projection started when I wanted to write a small utility to display the CPU temperature in a corner of the desktop using the open source OpenHardwareMonitorLib.dll (). Web Page here. This library provides extensive hardware information, including multiple system temperatures accessible through a Windows Management Interface (WMI). However, I institute the express documentation available somewhat confusing and had ameliorate luck using the public class enumerations provided past OHM directly, the cardinal being to telephone call the Update methods correctly for items and subitems before attempting to enumerate them.

Once I got it working, I created a wrapper class, OHData which has 1 public property, DataList and one public method, Update.

Since I was non able to extract BIOS information from OHM (even though information technology is visible when debugging), I also created a small separate wrapper class, WMBIOS, which gets the relevant information using System.Management (add this reference to your project besides).

Here are samples from the demo application:

The temperature window:

Image 1

The arrangement information popup window:

Image 2

Using the Code

To use the code, include a copy of OpenHardwareMonitorLib.dll in your project, add a reference to it. So create a local instance of OHdata, for example OHData od = new OHData(), phone call the update method and get the results in a List<OHMItem> object, each OHItem consisting of a Sensor Type, Sensor Name, and reading (all strings). The OHMItem grade construction is a public fellow member of OHData. OHItems in the retrieved list are preformatted for most types, such as voltages, temperatures, and fan speeds, ready to utilize in a ListView. To get but the CPU temperature, you can search the List for Type = "Temperature" and Name = "CPU" if you merely want to use that.

          using          Organization;          using          System.Collections.Generic;          using          System.Linq;          using          System.Windows.Forms;          using          System.Reflection;          using          System.IO;          using          Organisation.Management;          using          OpenHardwareMonitor;          using          OpenHardwareMonitor.Collections;          using          OpenHardwareMonitor.Hardware;            namespace          <your project          namespace          goes here> {          static          grade          Program     {                                 [STAThread]          static          void          Master()         {             Application.EnableVisualStyles();             Application.SetCompatibleTextRenderingDefault(fake);             Application.Run(new          MainForm());         }             }                        public          class          OHData     {                   public          List<ohmitem> DataList         {          get          {          return          ReportItems;             }          set up          {              }          }                   public          void          Update()         {             UpdateOHM();         }                    public          class          OHMitem         {          public          OHMitem()             {             }          public          cord          name             {          get          {          return          Name;                 }          set          {                     Name =          value;                 }             }          public          string          blazon             {          go          {          return          OHMType;                 }          set          {                     OHMType =          value;                 }             }          public          string          reading             {          become          {          return          OHMValue;                 }          set          {                     OHMValue =          value;                 }             }          individual          cord          Proper noun =          String.Empty;          individual          string          OHMType =          Cord.Empty;          individual          string          OHMValue =          Cord.Empty;          }                   private          Listing<ohmitem> ReportItems =          new          List<ohmitem>();                   individual          void          AddReportItem(cord          ARIName,          string          ARIType,          string          ARIValue)         {                       if          (ARIType ==          "          Data")             {          return;              }             OHMitem ARItem =          new          OHMitem();             ARItem.name = ARIName;             ARItem.blazon = ARIType +          "          : ";             ARItem.reading = ARIValue;          if          (ARIType ==          "          GpuAti")             {                 ARItem.blazon =          "          Graphics Card";             }          if          (ARIType ==          "          Temperature")             {          try          {          double          temp = Convert.ToDouble(ARIValue);                     ARItem.reading = ((((9.0          /          5.0) * temp) +          32).ToString("          000.0") +          "                      F");                 }          catch          {          return;                 }             }          if          (ARIType ==          "          Clock")             {          endeavour          {          double          temp = Catechumen.ToDouble(ARIValue);          if          (temp <          grand)                     {                         ARItem.reading = (temp.ToString("          F1") +          "                      MHZ");                     }          else          {                         temp = temp /          grand;                         ARItem.reading = (temp.ToString("          F1") +          "                      GHZ");                     }                 }          take hold of          {          render;                 }              }          if          (ARIType ==          "          Control"          || ARIType ==          "          Load")             {          try          {          double          temp = Convert.ToDouble(ARIValue);                     ARItem.name = ARIName;                     ARItem.reading = (temp.ToString("          F0") +          "                      %");                 }          catch          {          return;                 }             }          if          (ARIType ==          "          Voltage")             {          endeavor          {          double          temp = Convert.ToDouble(ARIValue);                     ARItem.name = ARIName;                     ARItem.reading = (temp.ToString("          F1") +          "                      V");                 }          catch          {          render;                 }             }                       if          (ARIType ==          "          Fan")             {          try          {          double          rpm = Convert.ToDouble(ARIValue);                     ARItem.name = ARIName;                     ARItem.reading = (rpm.ToString("          F0") +          "                      RPM");                 }          catch          {          render;                 }             }              ReportItems.Add(ARItem);         }                   private          OpenHardwareMonitor.Hardware.Computer computerHardware =          new          OpenHardwareMonitor.Hardware.Reckoner();                   private          void          UpdateOHM()         {          string          s =          Cord.Empty;          cord          name =          string.Empty;          string          blazon =          string.Empty;          string          value          =          cord.Empty;          int          ten, y, z, n;          int          hardwareCount;          int          subcount;          int          sensorcount;             ReportItems.Clear();             computerHardware.MainboardEnabled =          truthful;             computerHardware.FanControllerEnabled =          truthful;             computerHardware.CPUEnabled =          true;             computerHardware.GPUEnabled =          true;             computerHardware.RAMEnabled =          true;             computerHardware.HDDEnabled =          true;             computerHardware.Open();             hardwareCount = computerHardware.Hardware.Count();          for          (ten =          0; x < hardwareCount; x++)             {                 name = computerHardware.Hardware[ten].Proper name;                 type = computerHardware.Hardware[x].HardwareType.ToString();          value          =          "          ";                  AddReportItem(proper name, type,          value);                  subcount = computerHardware.Hardware[x].SubHardware.Count();                                             for          (y =          0; y < subcount; y++)                 {                     computerHardware.Hardware[x].SubHardware[y].Update();                 }                           if          (subcount >          0)                 {          for          (y =          0; y < subcount; y++)                     {                         sensorcount = computerHardware.Hardware[x].                         SubHardware[y].Sensors.Count();                         name = computerHardware.Hardware[x].SubHardware[y].Name;                         type = computerHardware.Hardware[x].SubHardware[y].                         HardwareType.ToString();          value          =          "          ";                         AddReportItem(name, type,          value);          if          (sensorcount >          0)                         {          for          (z =          0; z < sensorcount; z++)                             {                                                                  proper name = computerHardware.Hardware[x].                                 SubHardware[y].Sensors[z].Name;                                 type = computerHardware.Hardware[10].                                 SubHardware[y].Sensors[z].SensorType.ToString();          value          = computerHardware.Hardware[x].                                 SubHardware[y].Sensors[z].Value.ToString();                                 AddReportItem(proper name, type,          value);                                                              }                         }                     }                 }                 sensorcount = computerHardware.Hardware[x].Sensors.Count();          if          (sensorcount >          0)                 {          for          (z =          0; z < sensorcount; z++)                     {                         name = computerHardware.Hardware[x].Sensors[z].Name;                         type = computerHardware.Hardware[x].Sensors[z].SensorType.ToString();          value          = computerHardware.Hardware[x].Sensors[z].Value.ToString();                         AddReportItem(name, type,          value);                                              }                 }             }            computerHardware.Close();         }     }                       public          course          WMIBIOS     {          public          string          Name         {          go          {          return          name;             }                     }          public          string          Manufacturer         {          go          {          return          manufacturer;             }         }          public          cord          Date         {          get          {          return          FormatDate(date);             }          }          public          cord          Version         {          get          {          return          version;             }          }          public          void          Update()         {             update();         }          individual          cord          name =          String.Empty;          individual          string          manufacturer =          String.Empty;          private          string          date =          String.Empty;          private          string          version =          String.Empty;                   private          void          update()         {                                       ManagementObjectSearcher searcher =          new          ManagementObjectSearcher(@"          \\.\root\cimv2",          "          SELECT * FROM Win32_BIOS");             ManagementObjectCollection drove = searcher.Get();          foreach          (ManagementObject o          in          collection)             {          endeavor          {                     proper noun = Catechumen.ToString(o.GetPropertyValue("          Name"));                 }          catch          {                     name =          String.Empty;                 }          effort          {                     date = Catechumen.ToString(o.GetPropertyValue("          ReleaseDate"));                 }          catch          {                     appointment =          String.Empty;                 }          try          {                     manufacturer = Catechumen.ToString(o.GetPropertyValue("          Manufacturer"));                 }          take hold of          {                     manufacturer =          String.Empty;                 }          try          {                     version = Convert.ToString(o.GetPropertyValue("          SMBIOSBIOSVersion"));                 }          take hold of          {                     version =          String.Empty;                 }             }             searcher.Dispose();          render;         }                   private          string          FormatDate(string          rawdata)         {          string          outcome =          String.Empty;          string          year =          String.Empty;          string          calendar month =          String.Empty;          string          24-hour interval =          String.Empty;          effort          {                 year = rawdata.Substring(0,          iv);                 month = rawdata.Substring(4,          2);                 day = rawdata.Substring(half-dozen,          2);             }          take hold of          {          return          effect;             }             result = month +          "          -"          + day +          "          -"          + year;          return          issue;         }     } }

Points of Involvement

Between these two classes, most useful system data is available. Note that OHM returns unlike sensors and results depending on the underlying hadware. For example, arrangement board voltages that show upward on my home built gigabyte motherboard PC don't prove upward in the list on my Lenovo Carbon X1.

The "Temperature.exe" sample application shows how to use them. It opens a pocket-size partially transparent window in the correct lower corner of the screen with the CPU temperature brandish, updating every 60 seconds. Right-clicking the window opens a context menu with options to quit, brandish organization information, and changes the update interval. Note that I had to add a custom manifest to the project for Windows 8 and 10 with a higher access privilege, probably because OpenHardwareMonitor interacts with the hardware direct. This wasn't necessary in Windows seven. This shows upwards as "app.manifest" in Solution Explorer. A custom manifest tin exist added in the Awarding Window of Project->Properties. With Version ane.0.2.3, I added a backup method for calculating available Free and Used Memory if this is missing from the OHMonitor data, and more than information from other sources regarding the BIOS. At that place are yet some OHMonitor subitems where the expected data remains blank. If someone finds a solution to extracting the missing fields, delight allow me know. Using Visual Studio 2015 to run the demo app in debug style, it's clear that more data is really bachelor in the OHMonitor class than is coming through when the program runs, so the method for reading the enumerated values could be improved. I have been able to discover only limited online documentation for the OHM library so far.

That said, I have establish the library to return near relevant data from my homebuilt generic hardware PC which uses a Gigabyte GA-Z170X-UD5 motherboard and Windows 10-64 as well as my older Lenovo Carbon X1 laptop which runs Windows 10-32. Most importantly for a home builder, the CPU temperature is accurate when checked against other sources, such as the CorsairLink utility that came with my liquid CPU cooler.

Update eleven-07-2017  Following a posted proffer, I tried leaving the OHM ComputerHardware instance open after reading its values, rather than using the .Close() method subsequently each employ.  This seems to miss some subitems in the hardware enumeration, and then you must Open up and Shut the instance each time you query it.  I have not had further success in using the WMI interface provided by OHM, for which there is little if whatsoever documentation.  The sample application now averages the reported CPU temperatures to go the temp it displays.  Overall, the information it reports is at present very shut to OHM's own application and to data from the BIOS or tertiary party system data utilities such as CorsairLink.  Boosted outside items I added to the list with this version are the current display resolution and the Windows ten Build number.

Update xi-24-2019 Following a proposition by Member 14615649, Added an Options feature and temperature logging. Yellow and Cerise warning temperature thresholds tin can be customized equally can the transparency of the small-scale display window. The program will too save CPU temperatures to a log and display them in graph course using the internet DataVisualization API.  To zoom portions of the chart, click and drag to select an area of interest on information technology. Hovering over a datapoint on the graph shows its bodily temperature reading.

History

  • 07-xxx-2016 1st posting ver 1.0.0.9
  • 06-07-2017 iind posting ver 1.0.2.iii
  • 08-07-2017 3rd posting ver 1.0.2.four
  • 11-07-2017 4th posting ver 1.0.2.8
  • xi-24-2019 5th posting ver i.0.4.0

cornshisee1972.blogspot.com

Source: https://www.codeproject.com/Articles/1115618/CPU-Temperature-System-Information

0 Response to "Reading Cpu Temp Windows 10 C++ Visual Studio"

Publicar un comentario

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel