I found on internet but I didn't got how can I use ManagementObjectSearcher in delphi. My main question which file I have to add in 'uses'.
I found one code but can't make it run in my system.
I found on internet but I didn't got how can I use ManagementObjectSearcher in delphi. My main question which file I have to add in 'uses'.
I found one code but can't make it run in my system.
Himadri, the code wich you refer uses the ManagementObjectSearcher
in Delphi Prism
, the ManagementObjectSearcher
is a .Net class for retrieve information about the WMI
. you can access the WMI from delphi using 3 alternatives.
1) importing the Microsoft WMIScripting Library
from Component->Import Component and then select Import type library.
program WMI_Test;
{$APPTYPE CONSOLE}
uses
ActiveX,
Variants,
SysUtils,
WbemScripting_TLB in '..\..\..\Documents\RAD Studio\5.0\Imports\WbemScripting_TLB.pas';//
Procedure AccessWMI;
var
WMIServices : ISWbemServices;
Root : ISWbemObjectSet;
Item : Variant;
begin
WMIServices := CoSWbemLocator.Create.ConnectServer('.', 'root\cimv2','', '', '', '', 0, nil);
Root := WMIServices.ExecQuery('Select Caption FROM Win32_OperatingSystem','WQL', 0, nil);
Item := Root.ItemIndex(0);
Writeln(VarToStr(Item.Caption));
end;
begin
try
CoInitialize(nil);
AccessWMI;
Readln;
CoUninitialize;
except
on E:Exception do
Begin
CoUninitialize;
Writeln(E.Classname, ': ', E.Message);
Readln;
End;
end;
end.
2) use a COM late binding
program WMI_Test;
{$APPTYPE CONSOLE}
uses
SysUtils
,ActiveX
,ComObj
,Variants;
Procedure AccessWMI;
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
objWMIService := GetWMIObject('winmgmts:\\localhost\root\cimv2');
colItems := objWMIService.ExecQuery('SELECT Caption FROM Win32_OperatingSystem','WQL',0);
oEnum := IUnknown(colItems._NewEnum) as IEnumVariant;
if oEnum.Next(1, colItem, iValue) = 0 then
Writeln(VarToStr(colItem.Caption));
end;
begin
try
CoInitialize(nil);
try
AccessWMI;
Readln;
finally
CoUninitialize;
end;
except
on E:Exception do
Begin
Writeln(E.Classname, ': ', E.Message);
Readln;
End;
end;
end.
3) using a WMI library, like the GLibWMI VCL Component Library
you can found several samples in S.O about the WMI and Delphi
Another option is use the Delphi 2007 .Net
personality (if you have the RadStudio 2007) which is based on .NET Framework 2.0
check this sample to access the ManagementObjectSearcher
from an Delphi 2007 .Net
console application.
program WmiTest;
{$APPTYPE CONSOLE}
uses
System.Management,
SysUtils;
var
Searcher : ManagementObjectSearcher ;
Collection : ManagementObjectCollection;
iter : ManagementObjectCollection.ManagementObjectEnumerator;
WmiObject : ManagementObject;
begin
try
Searcher :=ManagementObjectSearcher.Create('SELECT Caption FROM Win32_OperatingSystem');
Collection:=Searcher.Get();
iter:=Collection.GetEnumerator;
while(iter.MoveNext()) do
begin
WmiObject:=ManagementObject(iter.Current);
Writeln(WmiObject['Caption']);
end;
Readln;
except
on E:Exception do
Writeln(E.Classname, ': ', E.Message);
end;
end.