
以下代码在Delphi2010中编译通过。说明:此程序并非对所有硬盘有效,获取到的硬盘序列号可能为错,但是如果用来做硬件加密,则无需太在意。
uses ActiveX,ComObj;
Function GetWMIProperty(WMIType, WMIProperty:AnsiString):String;
var
Wmi, Objs, Obj:OleVariant;
Enum:IEnumVariant;
C:Cardinal;
begin
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.Button2Click(Sender: TObject);
begin
showmessage(GetWMIProperty('Processor','ProcessorId')); //获取CPU序列号
showmessage(GetWMIProperty('DiskDrive','SerialNumber')); //获取硬盘序列号
end;

