加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

如何编写Delphi程序来控制CPU风扇速度并监控温度?

发布时间:2020-12-15 09:39:25 所属栏目:大数据 来源:网络整理
导读:我想使用Delphi创建一个程序来控制计算机的风扇速度并监控温度. 这样做涉及哪些API调用? 您可以分享哪些好的代码示例? 解决方法 您可以使用WMI类 Win32_TemperatureProbe和 Win32_Fan, 来自delphi,您必须使用Component- Import Component- Import type lib
我想使用Delphi创建一个程序来控制计算机的风扇速度并监控温度.

这样做涉及哪些API调用?

您可以分享哪些好的代码示例?

解决方法

您可以使用WMI类 Win32_TemperatureProbe和 Win32_Fan,
来自delphi,您必须使用Component-> Import Component-> Import type library-> Next->“选择库” – > Next->将单元添加到项目 – >导入Microsoft WMIScripting V1.x库. ;完.

看到这段代码.只是一个简单的例子.

program GetWMI_Info;

{$APPTYPE CONSOLE}

uses
  ActiveX,Variants,SysUtils,WbemScripting_TLB in '......DocumentsRAD Studio5.0ImportsWbemScripting_TLB.pas';

procedure ShowTemperatureInfo();
var
  WMIServices: ISWbemServices;
  Root       : ISWbemObjectSet;
  Item       : Variant;
  I          : Integer;
begin
 {
 http://msdn.microsoft.com/en-us/library/aa394493%28VS.85%29.aspx
 The Win32_TemperatureProbe? WMI class represents the properties of a temperature sensor (electronic thermometer).
 Most of the information that the Win32_TemperatureProbe WMI class provides comes from SMBIOS.
 Real-time readings for the CurrentReading property cannot be extracted from SMBIOS tables.
 For this reason,current implementations of WMI do not populate the CurrentReading property.
 The CurrentReading property's presence is reserved for future use.
 }

  Writeln('Temperature Info');
  Writeln('----------------');

  WMIServices := CoSWbemLocator.Create.ConnectServer('.','rootcimv2','',nil);
  Root  := WMIServices.ExecQuery('Select * FROM Win32_TemperatureProbe','WQL',nil);
  for I := 0 to Root.Count - 1 do
  begin
    Item := Root.ItemIndex(I);
      Writeln('Accuracy                        '+VarToStr(Item.Accuracy));
      Writeln('Availability                    '+VarToStr(Item.Availability));
      Writeln('Caption                         '+Item.Caption);
      Writeln('Config Manager Error Code       '+VarToStr(Item.ConfigManagerErrorCode));
      Writeln('Config Manager User Config      '+VarToStr(Item.ConfigManagerUserConfig));
      Writeln('Creation Class Name             '+VarToStr(Item.CreationClassName));
      Writeln('Current Reading                 '+VarToStr(Item.CurrentReading));
      Writeln('Description                     '+VarToStr(Item.Description));
      Writeln('Device ID                       '+VarToStr(Item.DeviceID));
      Writeln('Error Cleared                   '+VarToStr(Item.ErrorCleared ));
      Writeln('Error Description               '+VarToStr(Item.ErrorDescription));
      Writeln('Install Date                    '+VarToStr(Item.InstallDate));
      Writeln('Is Linear                       '+VarToStr(Item.IsLinear));
      Writeln('Last Error Code                 '+VarToStr(Item.LastErrorCode));
      Writeln('Lower Threshold Critical        '+VarToStr(Item.LowerThresholdCritical));
      Writeln('Lower Threshold Fatal           '+VarToStr(Item.LowerThresholdFatal));
      Writeln('Lower Threshold NonCritical     '+VarToStr(Item.LowerThresholdNonCritical));
      Writeln('Max Readable                    '+VarToStr(Item.MaxReadable));
      Writeln('Min Readable                    '+VarToStr(Item.MinReadable));
      Writeln('Name                            '+VarToStr(Item.Name));
      Writeln('Nominal Reading                 '+VarToStr(Item.NominalReading));
      Writeln('Normal Max                      '+VarToStr(Item.NormalMax));
      Writeln('Normal Min                      '+VarToStr(Item.NormalMin ));
      Writeln('PNP Device ID                   '+VarToStr(Item.PNPDeviceID));
      Writeln('Power Management Capabilities   '+VarToStr(Item.PowerManagementCapabilities));
      Writeln('Power Management Supported      '+VarToStr(Item.PowerManagementSupported));
      Writeln('Resolution                      '+VarToStr(Item.Resolution));
      Writeln('Status                          '+VarToStr(Item.Status));
      Writeln('Status Info                     '+VarToStr(Item.StatusInfo));
      Writeln('System Creation Class Name      '+VarToStr(Item.SystemCreationClassName));
      Writeln('System Name                     '+VarToStr(Item.SystemName));
      Writeln('Tolerance                       '+VarToStr(Item.Tolerance));
      Writeln('Upper Threshold Critical        '+VarToStr(Item.UpperThresholdCritical));
      Writeln('Upper Threshold Fatal           '+VarToStr(Item.UpperThresholdFatal));
      Writeln('Upper Threshold NonCritical     '+VarToStr(Item.UpperThresholdNonCritical));
      Writeln('');
  end;
end;

procedure  ShowCPUFanInfo();
var
  WMIServices: ISWbemServices;
  Root       : ISWbemObjectSet;
  Item       : Variant;
  I          : Integer;
begin
 {
  http://msdn.microsoft.com/en-us/library/aa394146%28VS.85%29.aspx
  The Win32_Fan WMI class represents the properties of a fan device in the computer system. For example,the CPU cooling fan.
 }
  Writeln('CPU FAN Info');
  Writeln('----------------');
  WMIServices := CoSWbemLocator.Create.ConnectServer('.',nil);
  Root  := WMIServices.ExecQuery('Select * FROM Win32_Fan',nil);
  for I := 0 to Root.Count - 1 do
  begin
    Item := Root.ItemIndex(I);

    Writeln('ActiveCooling                     '+VarToStr(Item.ActiveCooling));
    Writeln('Availability                      '+VarToStr(Item.Availability));
    Writeln('Caption                           '+VarToStr(Item.Caption));
    Writeln('Config Manager ErrorCode          '+VarToStr(Item.ConfigManagerErrorCode));
    Writeln('Config Manager UserConfig         '+VarToStr(Item.ConfigManagerUserConfig));
    Writeln('Creation ClassName                '+VarToStr(Item.CreationClassName));
    Writeln('Description                       '+VarToStr(Item.Description));
    Writeln('DesiredSpeed                      '+VarToStr(Item.DesiredSpeed));
    Writeln('DeviceID                          '+VarToStr(Item.DeviceID));
    Writeln('ErrorCleared                      '+VarToStr(Item.ErrorCleared));
    Writeln('ErrorDescription                  '+VarToStr(Item.ErrorDescription));
    Writeln('InstallDate                       '+VarToStr(Item.InstallDate));
    Writeln('LastErrorCode                     '+VarToStr(Item.LastErrorCode));
    Writeln('Name                              '+VarToStr(Item.Name));
    Writeln('PNPDeviceID                       '+VarToStr(Item.PNPDeviceID));
    Writeln('PowerManagement Capabilities      '+VarToStr(Item.PowerManagementCapabilities));
    Writeln('PowerManagement Supported         '+VarToStr(Item.PowerManagementSupported));
    Writeln('Status                            '+VarToStr(Item.Status));
    Writeln('StatusInfo                        '+VarToStr(Item.StatusInfo));
    Writeln('SystemCreation ClassName          '+VarToStr(Item.SystemCreationClassName));
    Writeln('SystemName                        '+VarToStr(Item.SystemName));
    Writeln('VariableSpeed                     '+VarToStr(Item.VariableSpeed));
    Writeln('');
  end;

End;


begin
  try
    CoInitialize(nil);
        ShowTemperatureInfo();
        ShowCPUFanInfo();
        Readln;
    CoUninitialize;
  except
    on E:Exception do
    Begin
        CoUninitialize;
        Writeln(E.Classname,': ',E.Message);
        Readln;
    End;
  end;
end.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读