gpt4 book ai didi

delphi - 如何在Delphi中获取主板ID或序列号?

转载 作者:行者123 更新时间:2023-12-03 14:42:51 27 4
gpt4 key购买 nike

如何从 Delphi 代码中获取主板 ID 或序列号?

有我可以查看的示例代码或文章吗?

最佳答案

尝试使用 WMI Win32_BaseBoard Class .

查看这些示例:

选项 1) 在执行之前,您需要从 Component->Import Component 导入 Microsoft WMIScripting 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) 使用 OLEVariantIBindCtx InterfaceIMoniker Interface

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.

关于delphi - 如何在Delphi中获取主板ID或序列号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2497253/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com