10

我正在尝试制作delphi程序服务器和客户端,以便保护我的应用程序并确保所有用户都受到控制,我应该给他们一个无法更改的唯一密钥以不丢失它们句柄,所以我认为它应该是硬盘驱动器序列号 + Bios SN,但我记得移除主板电池时可以更改 Bios,所以它不会工作。所以我现在的选择是硬盘真实序列号我尝试下面的代码来获取它,但它没有用

    unit Unit2;

interface

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

type
  TForm4 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form4: TForm4;

implementation

{$R *.dfm}

function GetWMIstring (wmiHost, wmiClass, wmiProperty : string):string;
var  // These are all needed for the WMI querying process
  Locator:  ISWbemLocator;
  Services: ISWbemServices;
  SObject:  ISWbemObject;
  ObjSet:   ISWbemObjectSet;
  SProp:    ISWbemProperty;
  Enum:     IEnumVariant;
  Value:    Cardinal;
  TempObj:  OleVariant;
  SN: string;
begin
  try
  Locator := CoSWbemLocator.Create;  // Create the Location object
  // Connect to the WMI service, with the root\cimv2 namespace
   Services :=  Locator.ConnectServer(wmiHost, 'root\cimv2', '', '', '','', 0, nil);
  ObjSet := Services.ExecQuery('SELECT * FROM '+wmiClass, 'WQL',
    wbemFlagReturnImmediately and wbemFlagForwardOnly , nil);
  Enum :=  (ObjSet._NewEnum) as IEnumVariant;
  while (Enum.Next(1, TempObj, Value) = S_OK) do
  begin
    SObject := IUnknown(tempObj) as ISWBemObject;
    SProp := SObject.Properties_.Item(wmiProperty, 0);
    if VarIsNull(SProp.Get_Value) then
      result := ''
    else
    begin
      SN := SProp.Get_Value;
      result :=  SN;
    end;
  end;
  except // Trap any exceptions (Not having WMI installed will cause one!)
   on exception do
    result := '';
   end;
end;

procedure TForm4.Button1Click(Sender: TObject);
var
x:string;
Y:string;

begin


    X:=GetWMIstring('','Win32_BIOS','SerialNumber');
    Y:=GetWMIstring('','Win32_DiskDrive"','SerialNumber')     ;

    ShowMessage(x+';'+y);
end;

end.

*所以请任何人纠正我的代码或给我另一个想法最好的问候*

4

2 回答 2

24

您的代码不起作用,因为您在 WMI 类名中传递了双引号。

更改此代码

GetWMIstring('','Win32_DiskDrive"','SerialNumber');

对此

GetWMIstring('','Win32_DiskDrive','SerialNumber');

顺便说一句,如果你遵循这个问题的答案的建议,你可以改进你的 WMI 函数(GetWMIstring)How can I improve the WMI performance using delphi?

试试这个示例(此代码使用后期绑定,不需要WbemScripting_TLB单位)

  uses
      ActiveX,
      ComObj;

    var
      FSWbemLocator : OLEVariant;
      FWMIService   : OLEVariant;

    function  GetWMIstring(const WMIClass, WMIProperty:string): string;
    const
      wbemFlagForwardOnly = $00000020;
    var
      FWbemObjectSet: OLEVariant;
      FWbemObject   : OLEVariant;
      oEnum         : IEnumvariant;
      iValue        : LongWord;
    begin;
      Result:='';
      FWbemObjectSet:= FWMIService.ExecQuery(Format('Select %s from %s',[WMIProperty, WMIClass]),'WQL',wbemFlagForwardOnly);
      oEnum         := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
      if oEnum.Next(1, FWbemObject, iValue) = 0 then

  if not VarIsNull(FWbemObject.Properties_.Item(WMIProperty).Value) then

     Result:=FWbemObject.Properties_.Item(WMIProperty).Value;

    FWbemObject:=Unassigned;
    end;

    procedure TForm4.Button1Click(Sender: TObject);
    var
      x:string;
      Y:string;
    begin
      FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
      FWMIService   := FSWbemLocator.ConnectServer('localhost', 'root\CIMV2', '', '');

      X:=GetWMIstring('Win32_BIOS','SerialNumber');
      Y:=GetWMIstring('Win32_PhysicalMedia','SerialNumber');

      ShowMessage(x+';'+y);
    end;
于 2012-11-04T03:36:42.260 回答
0

您应该避免,当安装更多设备时,PHYSICALDRIVE0 不需要在数据库中排在第一位。PHYSICALDRIVE1 是我的第一个。我添加查询位置,现在可以正常工作了。对于 Win32_DiskDrive 是属性名称DeviceID,对于 Win32_PhysicalDrive 是属性名称Tag。这里改变了部分:

function  GetWMIstring(const WMIClass, WMIProperty, WMIWhere:string): string;
...
  qry:='SELECT '+WMIProperty+' FROM '+WMIClass;
  if WMIWhere<>'' then qry:=qry + ' WHERE '+WMIWhere;
  FWbemObjectSet:= FWMIService.ExecQuery(qry,'WQL',wbemFlagForwardOnly);
...
  x:=GetWMIstring('Win32_DiskDrive','SerialNumber','DeviceID="\\\\.\\PHYSICALDRIVE0"'));
  y:=GetWMIstring('Win32_PhysicalMedia','SerialNumber','Tag="\\\\.\\PHYSICALDRIVE0"'));

/ 之前不要忘记调用 CoInitialize !!!

于 2018-04-03T13:51:28.227 回答