如何从Delphi代码中获取主板ID或序列号?是否有任何示例代码或文章可供我参考?
ymzxtsji1#
尝试使用WMI Win32_BaseBoard Class。请参阅以下示例:
**选项1)**执行前需要从Component->Import Component导入Microsoft WMIScripting Library,然后选择Import type library
Component
Import Component
Microsoft WMIScripting Library
Import type library
program GetWMI_MotherBoardInfo; {$APPTYPE CONSOLE} uses ActiveX, Variants, SysUtils, WbemScripting_TLB in '..\..\..\Documents\RAD Studio\5.0\Imports\WbemScripting_TLB.pas';// Function GetMotherBoardSerial:string; var WMIServices : ISWbemServices; Root : ISWbemObjectSet; Item : Variant; begin WMIServices := CoSWbemLocator.Create.ConnectServer('.', 'root\cimv2','', '', '', '', 0, nil); Root := WMIServices.ExecQuery('Select SerialNumber From Win32_BaseBoard','WQL', 0, nil); Item := Root.ItemIndex(0); Result:=VarToStr(Item.SerialNumber); end; begin try CoInitialize(nil); Writeln('Serial MotherBoard '+GetMotherBoardSerial); Readln; CoUninitialize; except on E:Exception do Begin CoUninitialize; Writeln(E.Classname, ': ', E.Message); Readln; End; end; end.
**选项2)**使用OLEVariant、IBindCtx Interface和IMoniker Interface
OLEVariant
program GetWMI_MotherBoardSerial; {$APPTYPE CONSOLE} uses SysUtils ,ActiveX ,ComObj ,Variants; function GetMotherBoardSerial:String; var objWMIService : OLEVariant; colItems : OLEVariant; colItem : OLEVariant; oEnum : IEnumvariant; iValue : LongWord; function GetWMIObject(const objectName: String): IDispatch; var chEaten: Integer; BindCtx: IBindCtx; Moniker: IMoniker; begin OleCheck(CreateBindCtx(0, bindCtx)); OleCheck(MkParseDisplayName(BindCtx, StringToOleStr(objectName), chEaten, Moniker)); OleCheck(Moniker.BindToObject(BindCtx, nil, IDispatch, Result)); end; begin Result:=''; objWMIService := GetWMIObject('winmgmts:\\localhost\root\cimv2'); colItems := objWMIService.ExecQuery('SELECT SerialNumber FROM Win32_BaseBoard','WQL',0); oEnum := IUnknown(colItems._NewEnum) as IEnumVariant; if oEnum.Next(1, colItem, iValue) = 0 then Result:=VarToStr(colItem.SerialNumber); end; begin try CoInitialize(nil); try Writeln('Serial MotherBoard '+GetMotherBoardSerial); Readln; finally CoUninitialize; end; except on E:Exception do Begin Writeln(E.Classname, ': ', E.Message); Readln; End; end; end.
5hcedyr02#
如果你想展示主板系列,使用这个东西。要在Windows 10中显示主板编号,请在WMI服务中查询以下内容:
wmic baseboard get product,Manufacturer,version,serialnumber
//USES: Winapi.ActiveX, System.Win.ComObj function TForm2.GetMotherBoardSerial: string; var objWMIService: OLEVariant; colItems: OLEVariant; colItem: OLEVariant; oEnum: IEnumvariant; iValue: Longword; function GetWMIObject(const objectName: string): IDispatch; var chEaten: Integer; BindCtx: IBindCtx; Moniker: IMoniker; begin OleCheck(CreateBindCtx(0, bindCtx)); OleCheck(MkParseDisplayName(BindCtx, StringToOleStr(objectName), chEaten, Moniker)); OleCheck(Moniker.BindToObject(BindCtx, nil, IDispatch, Result)); end; begin Result := ''; objWMIService := GetWMIObject('winmgmts:\\localhost\root\cimv2'); colItems := objWMIService.ExecQuery('SELECT SerialNumber FROM Win32_BaseBoard', 'WQL', 0); oEnum := IUnknown(colItems._NewEnum) as IEnumVariant; if oEnum.Next(1, colItem, iValue) = 0 then Result := VarToStr(colItem.SerialNumber); end;
zxlwwiss3#
我有另一个解决方案:
function TForm1.GetSerialMotherBoard: String; var a, b, c, d: LongWord; begin asm push EAX push EBX push ECX push EDX mov eax, 1 db $0F, $A2 mov a, EAX mov b, EBX mov c, ECX mov d, EDX pop EDX pop ECX pop EBX pop EAX end; result := inttohex(a, 8) + '-' + inttohex(b, 8) + '-' + inttohex(c, 8) + '-' + inttohex(d, 8); end;
3条答案
按热度按时间ymzxtsji1#
尝试使用WMI Win32_BaseBoard Class。
请参阅以下示例:
**选项1)**执行前需要从
Component
->Import Component
导入Microsoft WMIScripting Library
,然后选择Import type library
**选项2)**使用
OLEVariant
、IBindCtx Interface和IMoniker Interface5hcedyr02#
如果你想展示主板系列,使用这个东西。要在Windows 10中显示主板编号,请在WMI服务中查询以下内容:
wmic baseboard get product,Manufacturer,version,serialnumber
zxlwwiss3#
我有另一个解决方案: