views:

943

answers:

1

hey folks

how to get motherboard id or serial number ?

is there any example or articles o try to get ?

thanks in advance

+12  A: 

try using the WMI Win32_BaseBoard Class .

see theses samples:

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.

Option 2) using OLEVariant, IBindCtx Interface and IMoniker 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.
RRUZ
u r the man RRUZ ,will learn some thing :)
radick
@PRUZ, can you explain the second code... It works but I want some explanation..
Himadri
The only thing I didn't understand is 'winmgmts:\\localhost\root\cimv2'
Himadri
@PRUZ I check the same code in some other computer which returns 'none'. What's the matter with it?
Himadri
@himadri, the second code uses a Late Binding call, wich does not requires import the wmi library inside delphi, you can read more abot Late binding in this link http://word.mvps.org/faqs/interdev/earlyvslatebinding.htm
RRUZ
The `winmgmts:\\localhost\root\cimv2` is the namespace where is located the class `Win32_BaseBoard`, you can read more about wmi namespaces in this link http://msdn.microsoft.com/en-us/library/aa822575%28VS.85%29.aspx
RRUZ
@PRUZ And you know something about my second comment? that `I check the same code in some other computer which returns 'none'. What's the matter with it?`
Himadri