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.
delphi 枚举 WMI
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 1. Swift枚举基本概念 Swift中的枚举比OC中的枚举强大, 因为Swift中的枚举是一等类型, 它可以像...
- 有时候也是坑了,会定义很多枚举,然后你还得根据这些不同的枚举来设置不同的显示,比如控制器title,之前一直写if...
- 枚举是什么? 直接上代码:首先我们创建一个枚举类型的文件,命名为ColorEnum。 然后创建一个Pen类,假设P...
- 在JavaScript中,对象的属性分为可枚举和不可枚举之分,它们是由属性的enumerable值决定的。可枚举性...