delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
500 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi



Loremo - the 1.5 liter car coming in 2009




Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (16)


Grab the PC Serial Number & BIOS info using WMI callsComponent available for this articleFormat this article printer-friendly!Bookmark function is only available for registered users!
Windows Management Instrumentation Calls in Delphi
Product:
Delphi 5.x (or higher)
Category:
System
Skill Level:
Scoring:
Last Update:
03/22/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet SMBIOS WMI BIOS Serial-Number Windows-Management Hardware DMI
Times Scored:
17
Visits:
32305
Uploader: Doug Good
Company:
Reference: http://users.eastlink.ca/~dblondeau/downloads.htm
Component Download: http://yourpage.blazenet.net/ditto/files/wmidemo.zip
 
Question/Problem/Abstract:
How do I get the serial number of the PC and other hardware information using Delphi?
Answer:



As long as the PC you're using has the WMI Core components installed (freely available from Microsoft for all 32 bit Windows versions) you can use Windows Management Instrumentation calls to grab information that otherwise would be nearly impossible to get.  Here are just a few of the things you can easily grab via code:

PC identity information such as serial number, manufacturer, and model
PC configuration info such as amount of RAM, installed network cards, etc
A list of services and processes running on the PC
Much, much, much more is available. Literally thousands of pieces of info.

WMI will also let you grab this info from any PC on your network, not just your local machine, as long as you have proper access rights!

There are a couple tricks for using WMI in Delphi.  First, the WMI core components have to be installed in the OS.  Only Windows 2000 and XP and possibly ME include the core components by default.  For Windows 98 and NT (I'm not sure about ME) you will need to download the WMI core from Microsoft. Go to http://www.microsoft.com/downloads/search.asp?
and do a Keyword search for "WMI" (sans quotes). Make sure you select
All Downloads in the "Show Results For" box.  You want the file labeled
Windows Management Instrumentation (WMI) CORE 1.5 (Windows 95/98/NT 40)
Version 1.5 dated Feb. 11, 2000.

After installing the WMI core, you'll need to import this type library into Delphi: Microsoft WMI Scripting v1.1 Library (Version 1.1)
Go to Project/Import Type Library and Install it in the IDE.  

In your programs uses statement, you'll need to include ActiveX and WbemScripting_TLB. (WbemScripting_TLB will be available after the import I mentioned earlier)

Here's the WMI calling function that I used in the demo:

function GetWMIstring (wmiHost, wmiClass, wmiProperty : string):string;
var  // These are all needed for the WMI querying process
  Locator:  ISWbemLocator;
  Services: ISWbemServices;
  SObject:  ISWbemObject;
  ObjSet:   ISWbemObjectSet;
  SProp:    ISWbemProperty;
  Enum:     IEnumVariant;
  Value:    Cardinal;
  TempObj:  OleVariant;
  SN: string;
begin
  try
  Locator := CoSWbemLocator.Create;  // Create the Location object
  // Connect to the WMI service, with the root\cimv2 namespace
   Services :=  Locator.ConnectServer(wmiHost, 'root\cimv2', '', '', '','', 0, nil);
  ObjSet := Services.ExecQuery('SELECT * FROM '+wmiClass, 'WQL',
    wbemFlagReturnImmediately and wbemFlagForwardOnly , nil);
  Enum :=  (ObjSet._NewEnum) as IEnumVariant;
  while (Enum.Next(1, TempObj, Value) = S_OK) do
  begin
    SObject := IUnknown(tempObj) as ISWBemObject;
    SProp := SObject.Properties_.Item(wmiProperty, 0);
    if VarIsNull(SProp.Get_Value) then
      result := ''
    else
    begin
      SN := SProp.Get_Value;
      result :=  SN;
    end;
  end;
  except // Trap any exceptions (Not having WMI installed will cause one!)
   on exception do
    result := '';
   end;
end;

Call it like this:

getWMIstring('','Win32_BIOS','SerialNumber');

The WMIHost parameter is left blank for the local PC.  "Win32_BIOS" is the
WMI class that we're accessing, and "SerialNumber" is the property we want to grab.  Microsoft has a complete reference of classes and properties, as well as available events and methods on their web site:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/r_wmicls_6r6t.asp

This function is just the very tip of the iceberg.  WMI is extremely powerful.  
I'm still in the early stages of learning my way around WMI so this code is probably not the "best way" to do things.  But it works.

I'd like to thank Denis Blondeau [denisblondeau@hotmail.com] for sharing his WMI code with the rest of us.  This function was adapted from his original code, and is posted with permission.  He's got a great WMI demo on his web site, at http://users.eastlink.ca/~dblondeau/downloads.htm.

The demo I included here is much simpler, and is easier for WMI novices to cut their teeth on.  But you'll definitely want his SWBEM demo to see the power of WMI and Delphi.

Download and run the demo from my website here:
http://yourpage.blazenet.net/ditto/files/wmidemo.zip

And please, if you write any WMI code, share it with the rest of us. Working together, we'll all benefit.

--Doug Good
  dgood@psea.org





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
wmi and delphi with linux ?
    andrea (Feb 27 2003 7:21PM)

Do you know if it is possible to call the wmi provider on a Microsoft client from a Linux computer running kylix ? (ie: the software runs on the linux server and gets the wmi info from the client)

thanx

andrea
Respond

Bug
    Steve Moss (Sep 13 2002 11:52AM)

Doug:

Thanks for this code, which is really useful. However, there is a problem in that the interfaces returned in TempObj (via the Enum.Next() call) are not released. In some tests I have performed here, this can cause programs which use the code to hang on program exit.

The problem is that, unlike interfaces, OleVariants (of which TempObj is one) are not referenced-counted by Delphi: every successful call to Enum.Next(), therefore, creates a new interface which is never released.

Small adjustments are needed to the code, like this for instance:

function GetWMIstring (wmiHost, wmiClass, wmiProperty : string):string;
var  // These are all needed for the WMI querying process
  Locator:  ISWbemLocator;
  Services: ISWbemServices;
  SObject:  ISWbemObject;
  ObjSet:   ISWbemObjectSet;
  SProp:    ISWbemProperty;
  Enum:     IEnumVariant;
  Value:    Cardinal;
  TempObj:  OleVariant;
  SN: string;
begin
  Result := '';
  try
  Locator := CoSWbemLocator.Create;  // Create the Location object
  // Connect to the WMI service, with the root\cimv2 namespace
  Services := Locator.ConnectServer(wmiHost, 'root\cimv2', '', '', '','', 0, nil);
  ObjSet := Services.ExecQuery('SELECT * FROM '+wmiClass, 'WQL',
    wbemFlagReturnImmediately and wbemFlagForwardOnly , nil);
  Enum := (ObjSet._NewEnum) as IEnumVariant;
  while Enum.Next(1, TempObj, Value) = S_OK do
  begin
    try SObject := IUnknown(TempObj) as ISWBemObject; except SObject := nil; end;
    TempObj := Unassigned;  // Always need to free interface in TempObj
    if SObject <> nil then
    begin
      SProp := SObject.Properties_.Item(wmiProperty, 0);
      SN := SProp.Get_Value;
      if not VarIsNull(SN) then
      begin
        Result :=  SN;
        Break;
      end;
    end;
  end;
  except // Trap any exceptions (Not having WMI installed will cause one!)
  end;
end;
Respond

RE: Bug
Doug Good (Sep 25 2002 4:10PM)

Thanks for the update!
Note to future readers:  This code is much cleaner than what I posted, and should be used in place of that in my original article.  The goal of this article was for education, mine as much as anyone else's.  Thanks Steve!!  

Respond

Motherboard
    tHE DSS (Sep 10 2002 10:24PM)

Is there a way to get the motherboard manufacturer, and stuff like that, with this?

I've looked all over it, and can only seem to find reference to the Chipset.

SiSoft Sandra can pull up this kind of information, and the speed at which it runs, I assume it's using pretty much this method.

Cheers.
Respond

RE: Motherboard
Francisco (Jun 12 2005 7:30PM)

see the class CIM_BIOS or Win32_BIOS

Até a próxima..
Respond

On NT 4.0?
    admiral00 admiral00 (Aug 21 2002 12:24PM)

_

How can i get the "System Model" without WMI calls??

Is it possible to get the "System Model" on a NT 4.0 Workstation
without installing  WMI ?????

It must be bossible because i have a Program which is programmed
in Delphi which is screening the "System Model" on a NT 4.0
Workstation! But i don't have the source to this proggi!!!

I don't want to use any Components!!!

_
Respond

On NT 4.0?
Harry (Nov 18 2002 1:27PM)

Getting the PC serial No and Bios Info is possible for Window98/95 etc I am not getting for Window Nt/2000/XP

Rgds
Harry
Respond

RE: On NT 4.0?
Doug Good (Nov 25 2002 7:24PM)

WMI is only installed on Windows ME, 2000, and XP.  Other versions of windows require you to install WMI Core Components.  Microsoft makes them freely available.  The details are all present in the original article I posted here.  (See paragraph 4).

Respond

RE: On NT 4.0?
mohammad nejat mohammad (Aug 1 2007 6:20PM)

i am univercity in iran
Respond

WMI Slooooow
    Doug Good (Jun 4 2002 11:05PM)

I thought it was extremely slow myself. Whether it's the implementation or in the WMI subsystem itself, I don't know. But you're right, it's not at all fast. Unless, of course, there's a better way to acces the information that I just don't know about yet. And that could be the case.
Respond

Pitty it's SO slow
    anonymus (Jun 4 2002 9:56PM)

Is it just me or is retreiving information from WMI so slow??
Respond

WMI
    Mustafa Afyonluoglu (Mar 28 2002 3:08PM)

This is really very lite, very practical and very usefull information.

Small Note: Perhaps it would be also usefull if any link to other resources string (such as "'Win32_BIOS', 'SerialNumber') will be available.

Thank you !...

Respond

RE: WMI
Doug Good (Mar 28 2002 4:38PM)

Here are some classes and properties I found that were of particular interest to me:

CLASS                            PROPERTY
-------------------------------------------------------------------------------------------------------
Win32_BIOS                    SerialNumber
                                       Manufacturer

Win32_ComputerSystem    
                                       Caption                 = Computername
                                       Domain                  
                                       Manufacturer            
                                       Model                  
                                       Name                    = Computername
                                       PrimaryOwnerName      
                                       SystemStartupDelay      
                                       SystemType            
                                       TotalPhysicalMemory    
                                       Username                

Win32_ComputerSystemProduct
                                       IdentifyingNumber         (ie serial number)  
                                       Name                  
                                       UUID                    
                                       Vendor                  

Win32_UserAccount       Gets all network user names and info
                                      including Full Name (proper access required)

As I mentioned in the article, you really *must* download and run the SWBEM demo program, as it will enumerate all the possible pieces of info WMI can return, and you'll see not only how to make the calls, but what data to expect from them on your own PC.  Also see the Microsoft reference I mentioned, as it has the same info.
Respond

That is so cool
    Steen Hemmingsen (Mar 22 2002 5:18PM)

This is the coolest article I have read in a very long time.
Damn, you are good (God ?)
Keep up the good work.

Steen Hemmingsen
Respond

It fails on my computer
anonymus (Feb 23 2005 3:32PM)

I have Win2k SP4 and the code hangs on the ConnectServer starement...I keep getting the CPU view for the Winmngmt.exe process in import NTDLL.DbgBreakpoint procedure.

I've disabled all the debugging features, and I can't get past this. The program keeps hanging on this DLL entry and does not go further, even outside the IDE.

Can anyone help?
Respond

RE: It fails on my computer
dominique stock (Aug 24 2005 4:53PM)

don't launch it with the debug....
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
Hans Gulö
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)