Hardware Delphi

Title: Grab the PC Serial Number & BIOS info using WMI calls
Question: 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