Results 1 to 15 of 15

Thread: [1.0/1.1] Could anyone explain this code for me?

  1. #1

    Thread Starter
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    [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
    Last edited by drattansingh; May 26th, 2006 at 08:10 AM.

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    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
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

    Thread Starter
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    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

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    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.
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  5. #5

    Thread Starter
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    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?

  6. #6
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    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
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  7. #7
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: [1.0/1.1] Could anyone explain this code for me?

    and you get a more accurate reading in the system BIOS

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  8. #8
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    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
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  9. #9
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  10. #10

    Thread Starter
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    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

  11. #11
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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?

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  12. #12

    Thread Starter
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    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.

  13. #13
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  14. #14

    Thread Starter
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    Re: [1.0/1.1] Could anyone explain this code for me?

    Thanks for the information

  15. #15
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: [1.0/1.1] Could anyone explain this code for me?

    anytime

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width