?
Solved

CPU temperature&fans speeds

Posted on 2004-10-21
23
Medium Priority
?
1,711 Views
Last Modified: 2016-10-30
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
Comment
Question by:Henry_milk
  • 6
  • 4
  • 4
  • +4
20 Comments
 
LVL 17

Accepted Solution

by:
Wim ten Brink earned 1000 total points
ID: 12372139
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
ID: 12372164
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

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

Assisted Solution

by:SJohnson
SJohnson earned 1000 total points
ID: 12378225
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
ID: 12378232
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
ID: 12378887
@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
ID: 12382278
IEnumVariant isn't in D6.

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

Expert Comment

by:DavidBirch2dotCom
ID: 12388925
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
ID: 12399368
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
ID: 12399397
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
ID: 12414293
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
ID: 12506073
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
ID: 12506083
I'm using Delphi 6.
0
 
LVL 7

Expert Comment

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

Expert Comment

by:SJohnson
ID: 12519514
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
ID: 12527772
Yes, i had the same error on D7

0
 
LVL 1

Expert Comment

by:SJohnson
ID: 12528054
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
 

Expert Comment

by:Cyber Wheater
ID: 41865971
Dear Expert Comment
This code {2004-10-26} does not work at Windows 7.
What can we do there?
0
 

Expert Comment

by:Cyber Wheater
ID: 41865973
Dear Expert Comment
This code {2004-10-26} does not work at Windows 7.
What can we do there?
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This is an update to some code that someone else posted on Experts Exchange. It is an alternate approach, I think a little easier to use, & makes sure that things like the Task Bar will update.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses

578 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