|
|
Victor Chen, (C++ 爱好者)
' q' e P6 ?# S, m, V) K: e+ U
" }- D2 @0 {3 P+ q4 d8 d. u p
( |! I, }1 @, F--------------------------------------------------------------------------------4 |; S; Z$ o- c# q
WMI: Windows Management Instrumentation (Windows 管理工具)
3 t8 f0 _# M \3 p5 Z# W) i 通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。
2 K6 ^3 i& W+ y 利用这个工具可以管理本地或客户端系统中几乎所有的信息。
5 q! |' N' R) n' V5 p( a 很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。 , V _2 y, h- n* _8 m4 u
& C x& o% P! I" ~--------------------------------------------------------------------------------0 f6 f4 ~! s8 P# C
BCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面
/ K/ Y& e; w1 C: w/ Y: N. t& H9 R9 E9 ^1 f9 x3 z. |* p. z+ `2 g
--------------------------------------------------------------------------------
1 Z# F: f7 r1 X' }# [8 }① 初始化 COM 接口:1 {' }0 f0 t5 B# G( c
访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。
4 q9 ^3 r# o9 q) x, t 这两个函数在 #include <comdef.h> 里面定义。- X8 n5 z9 ^0 e3 G5 J( n. _ z5 s
3 N- Z0 Z6 d0 X0 Z+ m/ ~: \
② 获取访问 WMI 权限:
$ @7 n2 @# A$ c2 e+ I! Q2 t; s CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);9 L' S, x" a8 B4 A% w
如果这个函数返回 S_OK 获取权限成功, 否则为失败。
0 |& N" |" Y- n L. w$ t- Y
; S e, z: {* z) T6 K( |4 H③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:
u- _1 t! D: k0 [ H! h* ~ 这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。) ^) y) e. ^/ i! D
( d+ a1 N9 X8 t. |
void GetWmiInfo(TStrings *lpList, WideString wsClass)
! y; D8 L4 x2 g5 ^{8 M: _7 m u8 B5 @2 R2 M* b2 Q8 Y4 m
IWbemLocator *pWbemLocator = NULL;6 `6 o0 W- D/ a& W& u
if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)
- J' H J. E {4 E/ L {
; M, g9 f) `% q) x" T/ N IWbemServices *pWbemServices = NULL;8 }! K N( _6 f) z' x! @8 ~
WideString wsNamespace = (L"root\\cimv2");) ]% V. i! \1 _0 Z' R) V
if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)9 C/ ]' ?1 ?% {
{
9 u" j+ x. y, [* B IEnumWbemClassObject *pEnumClassObject = NULL;
4 u' p7 c1 ?2 [+ x& V WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;
: y( ?5 ~; z# k2 S3 p7 a6 ^2 S if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)) f; h( `/ G$ d/ L1 R- q% Y
{ V0 k% @1 q( G; G
IWbemClassObject *pClassObject = NULL;
0 {) ?3 W" Q7 R2 c7 @ ULONG uCount = 1, uReturned;
* `9 u" @# O, K9 L8 k if(pEnumClassObject->Reset() == S_OK)
2 U+ C K8 r. X, ^" ~7 U; s {( Y: x: M5 f1 Y
int iEnumIdx = 0;
8 E( b- @$ b! k' b1 B while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)
! r4 G' w% K+ s {
% a E+ D8 M+ `" ^# f) o lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");
* _) @- u7 `; \9 l) d
; _. f' x2 W6 J) y SAFEARRAY *pvNames = NULL;
% ]9 a" G+ g" S if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)
# w- `2 @8 I5 I5 q$ b {
+ V p; q6 f. g8 \( R( |9 @ long vbl, vbu;/ U; ~% ]: Q6 H1 A
SafeArrayGetLBound(pvNames, 1, &vbl);) B4 O0 Y! J& i" Y
SafeArrayGetUBound(pvNames, 1, &vbu);
0 `. s3 @+ ^5 D. ]4 F# q Y3 `: { for(long idx=vbl; idx<=vbu; idx++)9 A5 l1 j0 e5 }' z0 H
{/ Q, v% a1 A8 {
long aidx = idx;
9 C2 ? @9 P( N0 M8 E. ?' s4 E4 o% _ wchar_t *wsName = 0;
+ W6 F+ K: ?1 r( I. e. U8 W3 g VARIANT vValue;! k; Y' k6 k8 [- u+ f1 u
VariantInit(&vValue);0 x* r: \! ^# ?5 ^/ u7 @
SafeArrayGetElement(pvNames, &aidx, &wsName);
+ O/ W4 _. Z6 I' |. L* Z% w
" D$ U6 A4 Y7 {. s# s6 L7 p. K BSTR bs = SysAllocString(wsName);- X o& e1 |6 t9 d) r1 S# C
HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);
3 ^$ r z' b( X$ U SysFreeString(bs);8 F$ ^, y' D i }. c5 X
. n4 @' [$ x2 k* ? if(hRes == S_OK), L) x4 C- ]) P2 e% i& z
{. v% A) S& o! T* f
AnsiString s;: ~ k$ @* n5 G. i
Variant v = *(Variant*)&vValue;
( {9 E' o* N. G9 q6 Y R# L( } if(v.IsArray())4 h) Y, i0 A& V, L [) y
{; p) s5 G: i9 F7 E$ [/ r+ g
for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)" h4 \- e7 }- a; Z+ a9 V
{; w# O0 r9 K+ j% l% R+ {
Variant a = v.GetElement(i);; f, a6 w- h; E9 S" j: W) p
if(!s.IsEmpty())
3 ~2 A0 e" P" }* M+ k s+=", ";7 ~5 {, Y/ y4 j' v; P9 r
s+=VarToStr(a);
' k, U1 V+ A& Z4 n' R, X8 t }
7 q* Y" r- o$ h' }- P1 Q( `1 w }
: r" d' i5 i; J else M9 E5 d7 b/ X; v r, h2 E1 o
{
- ^) I+ `1 n$ I# F- k$ } s = VarToStr(v);
7 D3 L# S. J8 h. z, f, F# V }
2 e4 |# i0 r! J lpList->Add(AnsiString(wsName)+"="+s);
( R( Q7 c) [( S, d$ L( \ }
( @; b; H* W. h) R T* f8 K5 r* `0 R$ F& k# ]
VariantClear(&vValue);
* x; ?2 M3 F* u& \0 l( H* P SysFreeString(wsName);
! T$ r% | X3 e4 H }5 W( c- _6 q- i; [
}
4 {2 b9 G, q3 x, K3 r if(pvNames)SafeArrayDestroy(pvNames);$ n S u1 b. v; S
iEnumIdx++;- E l5 {) {, D
}2 L! h& H0 \& P) h! p
}. |5 ^+ i( N5 f: n2 f
if(pClassObject)pClassObject->Release();
4 b9 K* x) a7 o- t: D* N2 p }$ Q. o% u$ j0 F8 J
if(pEnumClassObject)pEnumClassObject->Release();
5 C5 P/ J' Q, e2 u ~ }' n! c! w/ b1 D8 |; ^
if(pWbemServices)pWbemServices->Release();
3 [0 J; n: y) \- q' Z0 n }% W7 a& p: ~1 {, B0 u, G& o
if(pWbemLocator)pWbemLocator->Release();5 w; N2 C0 r8 S+ O+ K/ k
}
( E6 Z. T. Q3 V }; u7 C//---------------------------------------------------------------------------, h) J- x# B1 g
7 W6 N! k. A8 h! V0 p( a. p( o
// 通过 WIN32_bios 获取 BIOS 信息:
; p: e# x# ^1 r, g2 @1 e# uvoid __fastcall TForm1::Button1Click(TObject *Sender)) w, k0 ^. V1 d
{- L) m$ M6 {0 _, K4 X
Memo1->Lines->Add("================== [WIN32_bios] =================");% P2 e2 N* Z. `6 T
GetWmiInfo(Memo1->Lines, "WIN32_bios");
. D! L* Y: B7 i9 L! W Memo1->Lines->Add("");
* O1 f3 ^. A4 t4 Y6 h0 E( Z}; I( w( h; i" q
( q. r, {; f4 f4 n
--------------------------------------------------------------------------------
' r! L" t9 B k5 Q& p @ J3 G: B1 v l$ `: k
WMI 可以访问的信息类型有:/ m% J1 p# h. O( n. b8 `8 V
Win32_1394Controller
4 U) L' f6 }( M7 ~1 U( @ Win32_BaseBoard
+ E: D) g/ ^% O: t: C+ x: f3 e2 b M Win32_Battery% [" F) W! c) x) [. V
Win32_BIOS& o, ^1 X* a7 D; |; L- }) i
Win32_Bus& i# y' j" B/ V7 k/ P V! @
Win32_CacheMemory
6 H8 ~% e, C# f8 t1 ^2 |& q Win32_CDROMDrive
- o i" H. S$ z9 [$ O4 n- ? Win32_CurrentProbe; b$ n9 f2 B% d/ ~
Win32_DesktopMonitor# z6 |5 u8 _* t1 Y
Win32_DeviceMemoryAddress
& K- }2 C0 C* y6 G Win32_DiskDrive$ s2 Y! g/ r. Z: c* L
Win32_DisplayConfiguration
" o8 d8 c# O" N) I& x Win32_DisplayControllerConfiguration% n$ |# ^/ j8 \- T& k6 K, a- U
Win32_DMAChannel
9 D \- r* k3 I1 `5 r/ _9 P4 r Win32_Fan
& ]: D2 M- f0 _, Y/ K7 U Win32_FloppyController
$ N6 e H. L" G1 ?8 p( T Win32_FloppyDrive
9 h, \8 v( G4 ^" k! K( R Win32_HeatPipe7 R7 k2 f. i6 T
Win32_IDEController
6 I1 `3 x2 y9 {6 z+ ?' w Win32_InfraredDevice% [- q7 r$ L( x* s9 T% c
Win32_IRQResource+ A- V2 z9 I: s8 D3 a+ r
Win32_Keyboard0 h+ H9 b' V& {* B
Win32_MemoryArray5 S1 l! h6 V9 ^( N5 ?6 F
Win32_MemoryDevice' H8 D2 V6 n( C" t, I5 |: a: ? c
Win32_MotherboardDevice
6 d; d. M3 R" u7 Q6 ~+ d5 y" a6 z8 e( u Win32_NetworkAdapter
: O* e+ n6 _/ w9 X& R6 y9 _/ [- m Win32_NetworkAdapterConfiguration
) C: n; Q- A1 X6 J. Y Win32_OnBoardDevice
3 p5 V p6 v8 l4 B) [+ N3 j Win32_ParallelPort
0 I' d5 M0 M0 Y Win32_PCMCIAController
( p% q' ]% Q5 v Win32_PhysicalMemory
; J6 x d4 L: r' Z! P8 u Win32_PhysicalMemoryArray/ [0 }' x( g. \* O3 ?! I6 r
Win32_PnPEntity$ d$ N1 n p3 F0 f; u$ K$ d4 G
Win32_PointingDevice5 V1 T4 z4 l% i5 g# t
Win32_PortableBattery
* J: `8 }( c, M/ {7 z5 d Win32_PortConnector( L q1 J0 K+ Y, Q
Win32_PortResource
3 U; v7 H4 j* q6 d* O Win32_POTSModem
$ V* Y; ^* y9 C( ` Win32_PowerManagementEvent
4 ^/ V3 F, S5 n. f- i, ^# m Win32_Printer
+ i' P/ M4 X" b3 h: f, Z) Y* f3 i Win32_PrinterConfiguration
. G, p% K& U6 n/ @ Win32_PrintJob& i: O( m6 n$ ~4 B- _( H
Win32_Processor# \- D- R! I% F
Win32_Refrigeration* ?1 x% S. Y! F0 Z* M7 S
Win32_SerialPort
) G! Z3 o* X' e6 o3 D. w8 ~ Win32_SerialPortConfiguration/ s; o" D; G$ H
Win32_SMBIOSMemory
/ C) b) o3 Y1 T5 Q& v Win32_SoundDevice: e8 x5 R/ x% Z* a! o
Win32_SystemEnclosure
" c6 ^5 F8 p# k- x" @: G* ]1 } Win32_SystemMemoryResource
- o; H' ^% n' Q" d+ ]: H( s$ q, F' [ Win32_SystemSlot
1 e3 Q( h) J' P+ p% ~. P3 G Win32_TapeDrive, T3 [1 m+ F8 E( J$ y6 e9 p; E
Win32_TemperatureProbe
/ n/ G t+ o6 d$ W; h Win32_UninterruptiblePowerSupply$ ~5 d4 y; r* |, @# _' U' z3 z+ I
Win32_USBController. l$ S, X- }& K
Win32_VideoConfiguration6 t P5 b" V5 @/ {1 h2 Q
Win32_VideoController
3 z* O" n# z9 q6 r Win32_VoltageProbe
. x ]* ~/ W0 O5 |- j/ t& }3 a+ b1 m9 ~/ Z; c5 S3 F
以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios"); |
|