【问题标题】:How to get motherboard id or serial number in Delphi?如何在 Delphi 中获取主板 ID 或序列号?
【发布时间】:2011-01-30 15:13:18
【问题描述】:

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

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

【问题讨论】:

    标签: delphi serial-number motherboard


    【解决方案1】:

    尝试使用 WMI Win32_BaseBoard Class

    查看这些示例:

    选项 1) 在执行之前你需要从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) 使用 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.
    

    【讨论】:

    • 我唯一不明白的是'winmgmts:\\localhost\root\cimv2'
    • @PRUZ 我在其他一些返回“无”的计算机上检查了相同的代码。它有什么问题?
    • @himadri,第二个代码使用了后期绑定调用,不需要在delphi中导入wmi库,您可以在此链接word.mvps.org/faqs/interdev/earlyvslatebinding.htm中阅读更多关于后期绑定的内容@
    • winmgmts:\\localhost\root\cimv2 是类Win32_BaseBoard 所在的命名空间,您可以在此链接msdn.microsoft.com/en-us/library/aa822575%28VS.85%29.aspx 中阅读有关wmi 命名空间的更多信息
    • @PRUZ 你知道我的第二条评论吗?那I check the same code in some other computer which returns 'none'. What's the matter with it?
    【解决方案2】:

    我有另一个解决方案:

    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;
    

    最好的问候 惠灵顿

    【讨论】:

    • 你能解释一下你的代码在做什么吗?你确定它返回主板序列号,而不是 CPUID?
    • 我阅读了很多关于此的问题并得出结论,CPU 没有 ID 并更改我的建议以获取 HDD ID。我上面的程序在不同的计算机上捕获了相同的 ID。
    • 我觉得这个功能有问题!我发现每次同一个程序(使用它的程序)在不同的启动上运行时,它都会返回不同的值。示例(8次运行):1)000306D4-02100800-7FFBBF-BFEBFBFF,2)0003FBBF-BFEBFBFF,3)0003FBBF-BFEBFBFF,4)000306D4-02100800-7FFBBF-BFEBFBFF,5)000306D4- 02100800-7FFAFBBF-BFEBFBFF, 6} 000306D4-03100800-7FFAFBBF-BFEBFBFF, 7) 000306D4-01100800-7FFAFBBF-BFEBFBFF, 8) 000306D4-00100800-7FFAFBBF-BFEBFBFF。很奇怪!
    • 我认为没有解决办法...我从 2009 年就开始尝试了。
    【解决方案3】:

    如果你想显示主板序列号,使用这个东西。 要在 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;
    

    【讨论】:

    • 我收到了这个:To be filled by O.E.M.
    猜你喜欢
    • 2011-04-06
    • 2012-10-17
    • 2011-05-12
    • 2011-10-16
    • 1970-01-01
    • 2017-09-14
    • 2016-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多