delphi WMI 取硬件信息  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi WMI 取硬件信息


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  {Rec_Wmi = record
    ComputerName: string;
    Namespace: string;
    User: string;
    Password: string;
    WMIType: string;
    Enum: IEnumVariant;
    class function GetWmiEnum(WMIType: string; var AEnum: IEnumVariant; Namespace: string = 'root\cimv2';
      Where: string = ''; ComputerName: string = ''; User: string = ''; Password: string = ''): string; static;
    class function GetWMIProperty(Enum: IEnumVariant; WMIProperty: string; Index: integer = 0): OleVariant; static;
    class function SetWMIProperty(Enum: IEnumVariant; WMIProperty: string; value: string; Index: integer = 0): string; static;
    function GetEnum(Where: string = ''): string;
    function GetProperty(WMIProperty: string; Index: integer = 0): OleVariant;
    function SetProperty(WMIProperty: string; value: string): string;
  end;}


  TForm1 = class(TForm)
    Btnv_1: TButton;
    procedure Btnv_1Click(Sender: TObject);
  private
  public
  end;

var
  Form1: TForm1;

implementation
uses
  ActiveX,ComObj;

{$R *.dfm}

function GetWMIProperty(WMIType, WMIProperty:AnsiString):String;
var
  Wmi, Objs, Obj:OleVariant;
  Enum:IEnumVariant;
  C:Cardinal;
begin
  Result := '';
  try
    Wmi:= CreateOleObject(AnsiString('WbemScripting.SWbemLocator'));
    Objs := Wmi.ConnectServer(AnsiString('.'),AnsiString('root\cimv2')).ExecQuery(AnsiString('SELECT * FROM WIN32_'+WMIType));
 
    Enum:=IEnumVariant(IUnknown(Objs._NewEnum));
    Enum.Reset;
    Enum.Next(1,Obj,C);
    Obj:=Obj.Properties_.Item(WMIProperty,0).Value;
    if VarIsArray(Obj) then
    begin
      Result:=Obj[0]
    end else
    begin
      Result := Obj;
    end;
  except
     Result:='error';
  end;
end;

procedure TForm1.Btnv_1Click(Sender: TObject);
begin
  ShowMessage(GetWMIProperty('BIOS','BIOSVersion'));          //获得BIOS版本
  ShowMessage(GetWMIProperty('DiskDrive','SerialNumber'));     //获得第一块硬盘的设备标示
  ShowMessage(GetWMIProperty('Processor','ProcessorId'));     //获得CPUID。
end;

end.
 

推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.042654991149902 seconds