CPU temperature&fans speeds

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?

Henry_milkAsked:
Who is Participating?
 
Wim ten BrinkConnect With a Mentor Self-employed developerCommented:
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
 
jonas78Commented:
0
 
Wim ten BrinkSelf-employed developerCommented:
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Henry_milkAuthor Commented:
I still have  the problem.Anybody?
0
 
SJohnsonConnect With a Mentor Commented:
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
 
SJohnsonCommented:
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
 
Wim ten BrinkSelf-employed developerCommented:
@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
 
ginsonicCommented:
IEnumVariant isn't in D6.

I try to compile it on D6 and get 6 different errors.
0
 
DavidBirch2dotComCommented:
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
 
Henry_milkAuthor Commented:
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
 
Henry_milkAuthor Commented:
SJohnson,you could explain me what do you do for alex's code worked with you?
0
 
Wim ten BrinkSelf-employed developerCommented:
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
 
Henry_milkAuthor Commented:
Alex,sorry,but the line " WriteLn( '+ ', string( Prop.Name ), ': ', Trim( VarToStr( Prop.Get_Value ) ) );"
have a error"Undeclared identifier :'VarToStr'
0
 
Henry_milkAuthor Commented:
I'm using Delphi 6.
0
 
DavidBirch2dotComCommented:
I get the same error in D7 pro ?
0
 
SJohnsonCommented:
VarToStr is the the Variants unit.

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

http://users.on.net/jonstu/hardwareinfo.zip (220K)
0
 
Henry_milkAuthor Commented:
Yes, i had the same error on D7

0
 
SJohnsonCommented:
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
 
Cyber WheaterCommented:
Dear Expert Comment
This code {2004-10-26} does not work at Windows 7.
What can we do there?
0
 
Cyber WheaterCommented:
Dear Expert Comment
This code {2004-10-26} does not work at Windows 7.
What can we do there?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.