Delphi dan Windows Management Instrumentation (WMI) – tutorial singkat
March 22, 2008 at 2:45 pm | In Applications, Code Samples, Tutorials | 2 CommentsTags: delphi, Windows Management Instrumentation, WMI
Oleh Zamrony P. Juhara
Artikel ini saya tulis untuk menjawab sebuah pertanyaan yang diajukan di milis Delphindo mengenai bagaimana mendapatkan serial number motherboard atau prosesor.
Impor WMI Scripting Type Library
Langkah pertama adalah mengimpor WMI scripting library. Saya menggunakan Turbo Delphi Explorer 2006, Di IDE Delphi klik Component->Import Component. Langkah untuk mengimpor type library tidak berbeda jauh. Pada dialog Type of Component, pilih Import Type Library. Lanjutkan dengan menekan tombol Next. Akan ditampilkan dialog seperti berikut:
Buat aplikasi baru
Jika impor sukses, di komputer Anda akan tercipta file bernama WbemScripting_TLB.pas biasanya didirektori Lib dimana instalasi Delphi berada.
Buat aplikasi baru tambahkan kontrol TLabel dan TMemo ke form. Buat handler event ONShow form dan ketik kode berikut ini:
unit ufrmMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics,
Controls, Forms, Dialogs,ActiveX,WbemScripting_TLB, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Memo1: TMemo;
procedure FormShow(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function VarToString(const oleVar:oleVariant):string;
var
lowBound, highBound, I:Integer;
begin
if VarIsArray(oleVar) then
begin
Result:='[';
LowBound:=VarArrayLowBound(oleVar,1);
HighBound:=VarArrayHighBound(oleVar,1);
for I:=lowBound to highBound do
begin
if I=highBound then
Result:=Result+VarToString(oleVar[I])
else
Result:=Result+VarToString(oleVar[I])+',';
end;
Result:=Result+']';
end else
begin
Result:=VarToStr(oleVar);
end;
end;
function SelectWMI(wql:string):string;
var
I,J:Integer;
line:string;
Locator:ISWbemLocator;
Services:ISWbemServices;
ResultSet:ISWbemObjectSet;
Enum, Enum2:IEnumVariant;
RowObj, ColObj:OleVariant;
Value:LongWord;
begin
Locator:=CoSWbemLocator.Create as ISWbemLocator;
Services:=Locator.ConnectServer('.', '', '', '', '', '', 0, nil);
ResultSet:=Services.ExecQuery(wql, 'WQL',
wbemFlagReturnImmediately,nil);
Enum:=ResultSet._NewEnum as IEnumVariant;
line:=wql+#13#10#13#10;
for I:=1 to ResultSet.Count do
begin
Enum.Next(1, RowObj, Value);
if Value <= 0 then Break;
if VarIsNull(RowObj) then Continue;
Enum2 := IUnknown(RowObj.Properties_._NewEnum)
as IEnumVariant;
for J:=1 To RowObj.Properties_.Count do
begin
Enum2.Next(1, ColObj, Value);
if Value <= 0 then Break;
if VarIsNull(ColObj) then Continue;
line:=line+Format('%s=%s'+#13#10,
[VarToString(ColObj.Name), VarToString(ColObj.Value)]);
end;
line:=line+#13#10;
end;
Result:=line;
end;
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 exceptions (WMI tidak terinstall!)
on exception do
result := '';
end;
end;
procedure TForm1.FormShow(Sender: TObject);
begin
Label1.Caption:='Processor ID: '+getWMIString('',
'Win32_Processor',
'ProcessorId');
Memo1.Text:=selectWMI('SELECT * FROM Win32_Processor');
end;
end.
Pada kode di atas saya bermaksud mengambil informasi prosesor yang ada pada komputer saya menggunakan kelas WMI Win32_Processor. Lebih jauh mengenai kelas WMI lainnya silakan lihat di sini. Output aplikasi tersebut di komputer saya adalah sebagai berikut:
Kode di atas adalah hasil comot sana-sini. Terutama dari artikel Grab the PC Serial Number & Bios info using WMI calls dan i-drift – Delphi
2 Comments »
RSS feed for comments on this post. TrackBack URI
Leave a comment
Blog at WordPress.com. | Theme: Pool by Borja Fernandez.
Entries and comments feeds.
akhirnya …
post lagi … setelah 5 bulan
Comment by nobody — March 24, 2008 #
wah keren pak…
saya lagi butuh tutorial ttg WMI ini terutama interaksi dgn delphi, mungkin ada yg tahu/punya?
pls..
Thank’s
Comment by ecep — July 3, 2008 #