我正在尝试制作Delphi程序的服务器和客户端以保护我的应用程序,确保所有用户都在控制之下。为了给他们提供一个独特的密钥,以防止失去处理它们的能力,我认为应该是硬盘序列号+ BIOS SN。但是,我记得BIOS可以更改,当移除主板电池时,它将无法使用。所以现在我选择的是HDD真实序列号,我尝试使用以下代码来获取它,但它没有起作用。
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.
*所以请问有人能够帮忙校正我的代码或者给我其他的建议吗? 最好的祝福。*
"
真的很难被发现。 :-) - Ken WhiteWin32_PhysicalMedia
WMI类来检索数据,然后修改您的函数以检索WMI数据,我在我的答案中发布了一个示例。 - RRUZ