如何在Delphi中获取主板ID或序列号? [英] How to get motherboard id or serial number in Delphi?

查看:987
本文介绍了如何在Delphi中获取主板ID或序列号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I get the motherboard ID or serial number from Delphi code?

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

Is there any example code or articles that I can look at?

推荐答案

尝试使用WMI 请参阅以下示例:

选项1),在执行之前,您需要从Component-> Import Component导入Microsoft WMIScripting Library,然后选择Import type library

Option 1) before execute you need import the Microsoft WMIScripting Library from Component->Import Component and then select 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接口 IMoniker界面

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中获取主板ID或序列号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆