[1.0/1.1] Could anyone explain this code for me?
HI guys. I have a piece of code to find the temperature of a computer but I just cannot understand it. Could someone help in explaining this piece of code to me and also why it is finding the temperature for some operating systems and others it is returning an error?
Code:
using System.Management; // Need to add reference System.Management
....
string strsearch=string.Format(@"Select * from MSAcpi_ThermalZoneTemperature");
System.Management.ManagementObjectSearcher p_objSet=new ManagementObjectSearcher();
p_objSet.Scope=new ManagementScope(@"root\wmi");
p_objSet.Query=new ObjectQuery(strsearch);
try
{
foreach(System.Management.ManagementObject objItem in
p_objSet.Get())
{
int RawData=Convert.ToInt32(objItem["CurrentTemperature"]);
RawData = (RawData-2732)/10;
txtCpu.Text = string.Format(RawData.ToString());
}// end foreach
}// end try
catch(ManagementException ee)
{
MessageBox.Show("ERROR: \r\n\r\n" +ee.Message);
}// end try catch
Thanks - Jennifer
Re: [1.0/1.1] Could anyone explain this code for me?
Code:
//This is an SQL search statement, fetches all records in the MSAcpi_ThermalZoneTemperature table
string strsearch=string.Format(@"Select * from MSAcpi_ThermalZoneTemperature");
//Create a new system searching object
System.Management.ManagementObjectSearcher p_objSet=new ManagementObjectSearcher();
//set the searching scope to wmi
p_objSet.Scope=new ManagementScope(@"root\wmi");
// Query the search term
p_objSet.Query=new ObjectQuery(strsearch);
try
{
/*
* The search will return a set of ManagementObject(s)
* and we want to iterate through all of them
*/
foreach(System.Management.ManagementObject objItem in
p_objSet.Get())
{
//Convert record "CurrentTemperature" column value to 32 Bit integer
int RawData=Convert.ToInt32(objItem["CurrentTemperature"]);
//I don't know about this line, maybe it converts to fahrenheit
RawData = (RawData-2732)/10;
//Add the resulting integer to the text box
txtCpu.Text += string.Format(RawData.ToString());
}// end foreach
}// end try
catch(ManagementException ee)
{
MessageBox.Show("ERROR: \r\n\r\n" +ee.Message);
}// end try catch
Re: [1.0/1.1] Could anyone explain this code for me?
Thank ComputerJr. I understood most of your explainations except for one. The table: MSAcpi_ThermalZoneTemperature, where dooes this come from? What type of database is this supposed to be implemented in?
Is it safe to assume that the reason why it is not working for some motherboards and platforms is that this table does not exist or it's under a different name?
I'm trying to figure out how to extend this code to read the temperature from other motherboards as well.
Jennifer
Re: [1.0/1.1] Could anyone explain this code for me?
MSAcpi_ThermalZoneTemperature is a table in the WMI database (generated and updated by windows OS)
Supported methods through WinMmgt are either the SMBIOS table or the ACPI
Thermal Zone.
Many motherborads on the market does not support these methods, and
specialized utilities
with the knowledge of all the temperature sensor chips can provide that
informatio
in a custom non standard way.
Re: [1.0/1.1] Could anyone explain this code for me?
So is there any way I could expand this code to retrieve the CPU temperature for many more different kind of motherboards?
Re: [1.0/1.1] Could anyone explain this code for me?
I guess not.
that's the reason every major motherboards manufacturer provides their own "CPU temp" monitoring program
Re: [1.0/1.1] Could anyone explain this code for me?
and you get a more accurate reading in the system BIOS
Re: [1.0/1.1] Could anyone explain this code for me?
Quote:
Originally Posted by Techno
and you get a more accurate reading in the system BIOS
well, that's right but the point is not to restart your computer every time you want to check the temperature. I don't know if there is a way to read from the BIOS in C# or .NET platform in general
Re: [1.0/1.1] Could anyone explain this code for me?
no there isn't unfortunately
the only way to get a "close" or reasonable reading is through that code/WMI script as this is what Windows uses :)
Re: [1.0/1.1] Could anyone explain this code for me?
Ok I understand. My next question was how to read from the BIOS but I saw you already answered it. Is it that it cant be done or you dont know how to. I've been searching on google but there is little material on this subject. - Jennifer
Re: [1.0/1.1] Could anyone explain this code for me?
no you cannot read the BIOS from C#
BIOS is a low level thing which .NET does not do - you would have to use assembler and even then you cannot access the BIOS as it is protected, so pretty much you have to boot from a DOS disk and do it from that
why do u wanna do such a thing anyway?
Re: [1.0/1.1] Could anyone explain this code for me?
I'm doing an appliation like a watchdog that must retrieve the CPU temperature of a machine. The code I have works with INTEL motherboards but not with others. So i wanted to know if it could be modified to suit a wider range of motherboards.
Re: [1.0/1.1] Could anyone explain this code for me?
INTEL rule! anyway away from this.
no there isnt, you would have to use the Win32 API for that or the WMI statement.
even if you were wanting to retrieve the info from the BIOS using assembly, each motherboard is differently manufacturered therefore the address codes of the BIOS's will be different.
Re: [1.0/1.1] Could anyone explain this code for me?
Thanks for the information :thumb:
Re: [1.0/1.1] Could anyone explain this code for me?