Solved

CPU temperature&fans speeds

Posted on 2004-10-21
1,621 Views
Last Modified: 2008-03-17
Sorry for my english,but i from brazil.
I need help with urgency,because  i can lose my job !
I need to get cpu temperature e fans speeds,with delphi 6,so,How i do this?

0
Question by:Henry_milk
    18 Comments
     

    Expert Comment

    by:jonas78
    0
     
    LVL 17

    Accepted Solution

    by:
    uses
      Windows, SysUtils, Classes, ActiveX, ComObj, WbemScripting_TLB;

    function NextItem( var Enum: IEnumVARIANT; const riid: TGUID; out ppObject ): Boolean;
    var
      OleProperty: OleVariant;
      NumProp: LongWord;
    begin
      try
        Result := Succeeded( Enum.Next( 1, OleProperty, NumProp ) ) and ( NumProp > 0 ) and Succeeded( IDispatch( OleProperty ).QueryInterface( riid, ppObject ) );
      except
        Result := False;
        IUnknown( ppObject ) := nil;
      end;
    end;

    procedure RunMe;
    const
      //sQuery = 'select * from Win32_Fan';
      sQuery = 'select * from Win32_TemperatureProbe';
    var
      Enum: IEnumVariant;
      Item: SWbemObject;
      PropEnum: IEnumVariant;
      Prop: SWbemProperty;
    begin
      Enum := CoSWbemLocator.Create.ConnectServer( '', 'root\cimv2', '', '', '', '', 0, nil ).ExecQuery( sQuery, 'WQL', wbemFlagBidirectional, nil )._NewEnum as IEnumVariant;
      while NextItem( Enum, SWBemObject, Item ) do begin
        WriteLn( string( Item.Path_.Path ) );
        PropEnum := Item.Properties_._NewEnum as IEnumVariant;
        while NextItem( PropEnum, SWBemProperty, Prop ) do begin
          try
            WriteLn( '+ ', string( Prop.Name ), ': ', Trim( VarToStr( Prop.Get_Value ) ) );
          except on E: Exception do WriteLn( '+ ', E.Message );
          end;
        end;
      end;
    end;

    The procedure RunMe should write the properties of this WMI object to the default output device, however the computer I'm working on doesn't support any fan or temperature measurement so I don't get any results here. But I also used it with the query 'select * from Win32_Processor' and this gave the processor information quite nicely.
    It could be that WMI support fan and temperature overview on XP only, or maybe it's just limited to specific situations.

    Is there another option? Not afaik...

    Btw. Why are you at risk of losing your job if you don't find a solution?
    0
     
    LVL 17

    Expert Comment

    by:Wim ten Brink
    At http://www.workshop-alex.org/Sources/WbemScripting_TLB.pas you can find WbemScripting_TLB.pas for Delphi 7. Maybe with some minor adjustments you can use it in Delphi 6, else just import the type library C:\WINNT\system32\wbem\wbemdisp.tlb in Delphi 6, without letting it generate components for it!
    0
     

    Author Comment

    by:Henry_milk
    I still have  the problem.Anybody?
    0
     
    LVL 1

    Assisted Solution

    by:SJohnson
    Henry,

    Alex's code worked fine for me.  I'm using D7 though.  You say 'I still have the problem', but what problems are you refering too?  Did you follow his instructions about importing the type library if it doesn't work with Delphi 6??

    Also, you can't use WriteLn in a GUI app, so you have to change the WriteLn statement.  I just did:

    procedure RunMe;
    const
      //sQuery = 'select * from Win32_Fan';
      sQuery = 'select * from Win32_TemperatureProbe';
    var
      Enum: IEnumVariant;
      Item: SWbemObject;
      PropEnum: IEnumVariant;
      Prop: SWbemProperty;
    begin
      Enum := CoSWbemLocator.Create.ConnectServer( '', 'root\cimv2', '', '', '', '', 0, nil ).ExecQuery( sQuery, 'WQL', wbemFlagBidirectional, nil )._NewEnum as IEnumVariant;
      while NextItem( Enum, SWBemObject, Item ) do begin
        Form1.Memo1.Lines.Add(  string(Item.Path_.Path ));
        PropEnum := Item.Properties_._NewEnum as IEnumVariant;
        while NextItem( PropEnum, SWBemProperty, Prop ) do begin
          try
            Form1.Memo1.Lines.Add( '+    ' + string( Prop.Name ) + ' : ' + Trim( VarToStr( Prop.Get_Value ) ) );
          except on E: Exception do WriteLn( '+ ', E.Message );
          end;
        end;
      end;
    end;

    Worked perfectly.

    Provide a bit more information so we can help.

    Stu
    0
     
    LVL 1

    Expert Comment

    by:SJohnson
    Oh yeah, don't forget, this wont work on all computers.  The computer MUST have sensors that are readable by this API.  My Dell Inspiron 8200 laptop doesn't work, but my desktop does (generic brand PC).

    Stu
    0
     
    LVL 17

    Expert Comment

    by:Wim ten Brink
    @SJohnson, Yeah, well... I know... The WriteLn() was just as an example. I actually created a console application for that sample code but didn't want to confuse the questioneer with the things needed to initialize the COM system. (If you're using forms and an application, it's done automatically.) Your example works just as well... :-)

    Chances are, of course, that his system doesn't have any support to request the fan or temperature information. It's a new feature, actually. For the temperature, you'll need a temperature sensor in your computer or you'll get NO information. For the fan, the same story. Not all fans will return any information about themselves. My brand-less computer at home doesn't support these things so I don't see anything. And neither do my laptop or desktop at home. So I think that quite a few systems don't have these sensors, or else I just picked some systems without these sensors.

    AFAIK, there is no API that provides this information...

    Some class information that could be useful for you:

    class Win32_Fan : CIM_Fan
    {
      boolean ActiveCooling;
      uint16 Availability;
      string Caption;
      uint32 ConfigManagerErrorCode;
      boolean ConfigManagerUserConfig;
      string CreationClassName;
      string Description;
      uint64 DesiredSpeed;
      string DeviceID;
      boolean ErrorCleared;
      string ErrorDescription;
      datetime InstallDate;
      uint32 LastErrorCode;
      string Name;
      string PNPDeviceID;
      uint16 PowerManagementCapabilities[];
      boolean PowerManagementSupported;
      string Status;
      uint16 StatusInfo;
      string SystemCreationClassName;
      string SystemName;
      boolean VariableSpeed;
    };

    class Win32_TemperatureProbe : CIM_TemperatureSensor
    {
      sint32 Accuracy;
      uint16 Availability;
      string Caption;
      uint32 ConfigManagerErrorCode;
      boolean ConfigManagerUserConfig;
      string CreationClassName;
      sint32 CurrentReading;
      string Description;
      string DeviceID;
      boolean ErrorCleared;
      string ErrorDescription;
      datetime InstallDate;
      boolean IsLinear;
      uint32 LastErrorCode;
      sint32 LowerThresholdCritical;
      sint32 LowerThresholdFatal;
      sint32 LowerThresholdNonCritical;
      sint32 MaxReadable;
      sint32 MinReadable;
      string Name;
      sint32 NominalReading;
      sint32 NormalMax;
      sint32 NormalMin;
      string PNPDeviceID;
      uint16 PowerManagementCapabilities[];
      boolean PowerManagementSupported;
      uint32 Resolution;
      string Status;
      uint16 StatusInfo;
      string SystemCreationClassName;
      string SystemName;
      sint32 Tolerance;
      sint32 UpperThresholdCritical;
      sint32 UpperThresholdFatal;
      sint32 UpperThresholdNonCritical;
    };

    At MSDN you can find more specific information about these classes...
    0
     
    LVL 9

    Expert Comment

    by:ginsonic
    IEnumVariant isn't in D6.

    I try to compile it on D6 and get 6 different errors.
    0
     
    LVL 7

    Expert Comment

    by:DavidBirch2dotCom
    this is an interesting thread, havnt got a clue how to answer it but I thought I'd try it out ... having problems installing that component - how do i do it  ?
    0
     

    Author Comment

    by:Henry_milk
    I put the code of RunMe in a  button,but don't see anything.Maybe model of my mother,I try on 3 computer different and don't returns any information.
    0
     

    Author Comment

    by:Henry_milk
    SJohnson,you could explain me what do you do for alex's code worked with you?
    0
     
    LVL 17

    Expert Comment

    by:Wim ten Brink
    IEnumVariant can be found in the ActiveX control and exists in this unit in both Delphi 5 and Delphi 7...

    My code is not fully complete. To make it complete, create a new console application and save it as ShowInfo.dpr. Then make it look like this:

    program ShowInfo;
    {$APPTYPE CONSOLE}
    uses Windows, SysUtils, Classes, ActiveX, ComObj, WbemScripting_TLB;
    function NextItem( var Enum: IEnumVARIANT; const riid: TGUID; out ppObject ): Boolean;
    var
      OleProperty: OleVariant;
      NumProp: LongWord;
    begin
      try
        Result := Succeeded( Enum.Next( 1, OleProperty, NumProp ) ) and ( NumProp > 0 ) and Succeeded( IDispatch( OleProperty ).QueryInterface( riid, ppObject ) );
      except
        Result := False;
        IUnknown( ppObject ) := nil;
      end;
    end;
    procedure RunMe;
    const
      //sQuery = 'select * from Win32_Fan';
      sQuery = 'select * from Win32_TemperatureProbe';
    var
      Enum: IEnumVariant;
      Item: SWbemObject;
      PropEnum: IEnumVariant;
      Prop: SWbemProperty;
    begin
      Enum := CoSWbemLocator.Create.ConnectServer( '', 'root\cimv2', '', '', '', '', 0, nil ).ExecQuery( sQuery, 'WQL', wbemFlagBidirectional, nil )._NewEnum as IEnumVariant;
      while NextItem( Enum, SWBemObject, Item ) do begin
        WriteLn( string( Item.Path_.Path ) );
        PropEnum := Item.Properties_._NewEnum as IEnumVariant;
        while NextItem( PropEnum, SWBemProperty, Prop ) do begin
          try
            WriteLn( '+ ', string( Prop.Name ), ': ', Trim( VarToStr( Prop.Get_Value ) ) );
          except on E: Exception do WriteLn( '+ ', E.Message );
          end;
        end;
      end;
    end;
    begin
      RunMe;
      Write('Press <Enter> to quit');
      ReadLn;
    end. // <--- A dot, not semicolon!

    This is the whole project. No forms, no separate units. Just a simple console application. Compile it, run it and see the information appear onscreen, if your system is able to produce it... If not, it will be pretty empty.
    0
     

    Author Comment

    by:Henry_milk
    Alex,sorry,but the line " WriteLn( '+ ', string( Prop.Name ), ': ', Trim( VarToStr( Prop.Get_Value ) ) );"
    have a error"Undeclared identifier :'VarToStr'
    0
     

    Author Comment

    by:Henry_milk
    I'm using Delphi 6.
    0
     
    LVL 7

    Expert Comment

    by:DavidBirch2dotCom
    I get the same error in D7 pro ?
    0
     
    LVL 1

    Expert Comment

    by:SJohnson
    VarToStr is the the Variants unit.

    Try this.  My GUI version of Alex's code.

    http://users.on.net/jonstu/hardwareinfo.zip (220K)
    0
     

    Author Comment

    by:Henry_milk
    Yes, i had the same error on D7

    0
     
    LVL 1

    Expert Comment

    by:SJohnson
    I didn't, which is why I posted a fully working version above.  Has anyone who couldn't previously get it working tried the source I posted?
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
    Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
    With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
    In this Experts Exchange video Micro Tutorial, I'm going to show how small business owners who use Google Apps can save money by setting up what is called a catch-all email address in their Gmail accounts. By using the catch-all feature, small busin…

    933 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now