unit frmmainu;
{$mode Delphi}{$H+}
interface
uses
Windows, Classes, SysUtils, Variants, Forms, Controls, Graphics, Dialogs,
StdCtrls, ActiveX, ComObj, JwaWbemCli;
type
{ Tfrmmain }
Tfrmmain = class(TForm)
btnQuery1: TButton;
btnEnum: TButton;
memo: TMemo;
procedure btnEnumClick(Sender: TObject);
procedure btnQuery1Click(Sender: TObject);
private
procedure ShowProp(SProp: OleVariant);
public
end;
const
RPC_C_AUTHN_LEVEL_DEFAULT = 0;
RPC_C_IMP_LEVEL_IMPERSONATE = 3;
RPC_C_AUTHN_WINNT = 10;
RPC_C_AUTHZ_NONE = 0;
RPC_C_AUTHN_LEVEL_CALL = 3;
EOAC_NONE = 0;
var
frmmain: Tfrmmain;
implementation
{$R *.lfm}
//------------------------------------------------------------------------------
procedure Tfrmmain.btnQuery1Click(Sender: TObject);
const
strLocale = '';
strUser = '';
strPassword = '';
strNetworkResource = 'root\cimv2';
strAuthority = '';
WQL = 'SELECT * FROM Win32_Volume'; // SELECT * FROM MSNdis_80211_ReceivedSignalStrength Where active=true
var
FWbemLocator : IWbemLocator;
FWbemServices : IWbemServices;
FUnsecuredApartment : IUnsecuredApartment;
ppEnum : IEnumWbemClassObject;
apObjects : IWbemClassObject;
puReturned : ULONG;
pVal : OleVariant;
pType : Integer;
plFlavor : Integer;
Succeed : HRESULT;
begin
// Set general COM security levels
if Failed(CoInitializeSecurity(nil, -1, nil, nil, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, nil, EOAC_NONE, nil)) then Exit;
// Obtain the initial locator to WMI
if Succeeded(CoCreateInstance(CLSID_WbemLocator, nil, CLSCTX_INPROC_SERVER, IID_IWbemLocator, FWbemLocator)) then
try
// Connect to WMI through the IWbemLocator::ConnectServer method
if Succeeded(FWbemLocator.ConnectServer(strNetworkResource, strUser, strPassword, strLocale, WBEM_FLAG_CONNECT_USE_MAX_WAIT, strAuthority, nil, FWbemServices)) then
try
// Set security levels on the proxy
if Failed(CoSetProxyBlanket(FWbemServices, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, nil, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, nil, EOAC_NONE)) then Exit;
if Succeeded(CoCreateInstance(CLSID_UnsecuredApartment, nil, CLSCTX_LOCAL_SERVER, IID_IUnsecuredApartment, FUnsecuredApartment)) then
try
// Use the IWbemServices pointer to make requests of WMI
Succeed := FWbemServices.ExecQuery('WQL', WQL, WBEM_FLAG_FORWARD_ONLY, nil, ppEnum);
if Succeeded(Succeed) then
begin
memo.lines.add('Running WMI query...');
// Get the data from the query
while (ppEnum.Next(WBEM_INFINITE, 1, apObjects, puReturned)=0) do
begin
apObjects.Get('Caption', 0, pVal, pType, plFlavor);
memo.lines.add('"' + AnsiToUTF8(pVal) + '"');
VarClear(pVal);
end;
end
else
memo.lines.add(Format('Error executing WQL sentence %x',[Succeed]));
finally
FUnsecuredApartment := nil;
end;
finally
FWbemServices := nil;
end;
finally
FWbemLocator := nil;
end;
end;
//------------------------------------------------------------------------------
procedure Tfrmmain.btnEnumClick(Sender: TObject);
const
WbemUser ='';
WbemPassword ='';
WbemComputer ='localhost';
wbemFlagForwardOnly = $00000020;
var
FSWbemLocator : OLEVariant;
FWMIService : OLEVariant;
FWbemObjectSet: OLEVariant;
classItems : OLEVariant;
classItem : Variant;
FWbemObject : Variant;
classEnum : IEnumvariant;
oEnum : IEnumvariant;
sValue : string;
begin;
FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
FWMIService := FSWbemLocator.ConnectServer(WbemComputer, 'root\CIMV2', WbemUser, WbemPassword);
classItems := FWMIService.SubclassesOf();
classEnum := IUnknown(classItems._NewEnum) As IEnumvariant;
while oEnum.Next(1, classItem, nil) = 0 do
begin
sValue := classItem.Path_.Class;
memo.lines.add(Format('Class %s', [sValue]));
classItem := Unassigned;
end;
{FWbemObjectSet:= FWMIService.ExecQuery('SELECT * FROM Win32_MemoryDevice','WQL',wbemFlagForwardOnly);
oEnum := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
while oEnum.Next(1, FWbemObject, nil) = 0 do
begin
sValue := FWbemObject.Properties_.Item('Caption').Value;
memo.lines.add(Format('Caption %s', [sValue]));// String
FWbemObject := Unassigned;
end;}
end;
//------------------------------------------------------------------------------
procedure Tfrmmain.ShowProp(SProp: OleVariant);
var
StrValue: string;
Count: Cardinal;
begin
StrValue := '';
if VarIsNull(SProp.Get_Value) then StrValue := '<empty>'
else
case SProp.CIMType of
Cim_Uint8, Cim_Sint8, Cim_Uint16, Cim_Sint16, Cim_Uint32, Cim_Sint32, Cim_Sint64:
if VarIsArray(SProp.Get_Value) then
begin
if VarArrayHighBound(SProp.Get_Value, 1) > 0 then
for Count := 1 to VarArrayHighBound(SProp.Get_Value, 1) do
StrValue := StrValue + ' ' + IntToStr(SProp.Get_Value[Count]);
end
else
StrValue := IntToStr(SProp.Get_Value);
Cim_Real32, Cim_Real64: StrValue := FloatToStr(SProp.Get_Value);
Cim_STRING: StrValue := SProp.Get_Value;
else memo.lines.add('Unknown type');
end;
memo.lines.Add(StrValue);
end;
//------------------------------------------------------------------------------
end.
网友评论