delphi 枚舉 WMI

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.
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末臊岸,一起剝皮案震驚了整個濱河市绘盟,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌耙替,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,204評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蹂析,死亡現(xiàn)場離奇詭異管削,居然都是意外死亡,警方通過查閱死者的電腦和手機斩箫,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,091評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來撵儿,“玉大人乘客,你說我怎么就攤上這事〉硇” “怎么了易核?”我有些...
    開封第一講書人閱讀 164,548評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長浪默。 經(jīng)常有香客問我牡直,道長,這世上最難降的妖魔是什么纳决? 我笑而不...
    開封第一講書人閱讀 58,657評論 1 293
  • 正文 為了忘掉前任井氢,我火速辦了婚禮,結(jié)果婚禮上岳链,老公的妹妹穿的比我還像新娘。我一直安慰自己劲件,他們只是感情好掸哑,可當我...
    茶點故事閱讀 67,689評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著零远,像睡著了一般苗分。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上牵辣,一...
    開封第一講書人閱讀 51,554評論 1 305
  • 那天摔癣,我揣著相機與錄音,去河邊找鬼纬向。 笑死择浊,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的逾条。 我是一名探鬼主播琢岩,決...
    沈念sama閱讀 40,302評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼师脂!你這毒婦竟也來了担孔?” 一聲冷哼從身側(cè)響起江锨,我...
    開封第一講書人閱讀 39,216評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎糕篇,沒想到半個月后啄育,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,661評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡拌消,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,851評論 3 336
  • 正文 我和宋清朗相戀三年挑豌,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片拼坎。...
    茶點故事閱讀 39,977評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡浮毯,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出泰鸡,到底是詐尸還是另有隱情债蓝,我是刑警寧澤,帶...
    沈念sama閱讀 35,697評論 5 347
  • 正文 年R本政府宣布盛龄,位于F島的核電站饰迹,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏余舶。R本人自食惡果不足惜啊鸭,卻給世界環(huán)境...
    茶點故事閱讀 41,306評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望匿值。 院中可真熱鬧赠制,春花似錦、人聲如沸挟憔。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,898評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽绊谭。三九已至政恍,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間达传,已是汗流浹背篙耗。 一陣腳步聲響...
    開封第一講書人閱讀 33,019評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留宪赶,地道東北人宗弯。 一個月前我還...
    沈念sama閱讀 48,138評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像搂妻,于是被迫代替她去往敵國和親罕伯。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,927評論 2 355

推薦閱讀更多精彩內(nèi)容