|
|
Victor Chen, (C++ 爱好者)
' o( j' e) E1 ]+ q+ F; i9 F
. g4 {+ M. q0 H( T) n
* h5 j4 u0 B9 J3 K$ Z; f--------------------------------------------------------------------------------, C" Q+ M, o7 L2 T
WMI: Windows Management Instrumentation (Windows 管理工具)" f2 B. y8 k) m
通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。 3 _8 ^$ }# V: t! k
利用这个工具可以管理本地或客户端系统中几乎所有的信息。. U, }- F8 B) }0 S9 k* h4 c3 E3 `
很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。
$ ~! s5 u: m$ d# N; [1 M( v, @
3 {% \, i* M* D, p# F--------------------------------------------------------------------------------, a2 p, G/ `3 M* C
BCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面
, `5 B0 E8 F: j. q7 i1 o/ Z3 n9 f7 S% A$ q. x# M: c
--------------------------------------------------------------------------------/ u n3 I* U: E( a
① 初始化 COM 接口:$ L, G/ A9 f% h' g+ y! U. @
访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。, o9 {( n9 Z" u! l- g7 H U
这两个函数在 #include <comdef.h> 里面定义。
' [/ }* q. i' P# @) l Y3 l7 R) ~) r' D, `5 h! m
② 获取访问 WMI 权限:3 ?) f" S+ T4 H3 M( ?- K) o
CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);
+ Z2 k) W( j! r4 p) X5 Q) H 如果这个函数返回 S_OK 获取权限成功, 否则为失败。
. [$ E% G0 b* \/ Q4 m9 Z9 G4 d
- e6 N; j9 r& X" M, S" b" D: f③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:
% X' |3 O! x w2 P; |$ f: { 这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。$ w$ b& [( _4 `0 k
* j' r( M: o6 W5 ]; Vvoid GetWmiInfo(TStrings *lpList, WideString wsClass)
" m$ ?/ r! a, C{
3 d" J$ D9 T2 V9 C$ K1 t IWbemLocator *pWbemLocator = NULL;
, ^! H$ T7 O/ W _: a( Y0 B6 N1 D& q4 M if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)/ u: h$ z9 i5 F" ~2 V$ Z/ j" [
{
1 `" V( r" S; t9 ` IWbemServices *pWbemServices = NULL;5 _1 _6 e/ y* t; S( J' M" q3 H/ @
WideString wsNamespace = (L"root\\cimv2");
: ]1 s8 G5 A* n$ \& w0 a if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)7 z8 a A+ i! w/ I' ~) ]
{/ d/ G& V) C1 y
IEnumWbemClassObject *pEnumClassObject = NULL;9 {/ R4 _! z) l1 c" h1 \
WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;
( P8 {7 P2 X E& j/ @2 d0 M if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)
* G, M" n8 n/ U( Y1 N2 u" g {
3 K- g5 d) `% }; d/ s0 D4 o IWbemClassObject *pClassObject = NULL;
: i! S; x% C. r4 v1 W& V4 ]- y ULONG uCount = 1, uReturned;
* {9 g% b$ @1 ]' N. V% w if(pEnumClassObject->Reset() == S_OK)
5 A( T* W& S: Y q7 M9 Y2 s& G& u {- n- a8 O1 n0 w
int iEnumIdx = 0;% i! x6 ]' U: ~" |3 e7 ` I8 H
while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)0 {) M" f( M8 k8 e
{
, C% M. g; A. C7 r8 q& ^+ p lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");
& Q% h" c! A" a6 |1 @* P) a8 s6 J4 y( x" T2 [5 ^: }* |
SAFEARRAY *pvNames = NULL;, T6 D: X4 E: T, e2 p& V
if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)
, ]* T7 p1 ^3 \# s' W8 h3 T4 A {4 V9 l. Z: s0 S& u, l& |0 a* m. g
long vbl, vbu;5 _1 i {: e$ i
SafeArrayGetLBound(pvNames, 1, &vbl);
$ X, t2 V+ a4 d$ @4 A; {2 z SafeArrayGetUBound(pvNames, 1, &vbu);' H" \9 \# y4 z! a- f
for(long idx=vbl; idx<=vbu; idx++)
7 P- T$ K- e' ~/ v1 |' U {, D$ c+ F/ ~: ^: O8 C
long aidx = idx;% @' I. q; T. K3 r
wchar_t *wsName = 0;7 J" R0 O e6 q, X( Z; p
VARIANT vValue;) C. H6 h/ W3 g
VariantInit(&vValue);
% h L* |/ o# i SafeArrayGetElement(pvNames, &aidx, &wsName);
X- a- ]8 w: G7 l. e: q/ k% @3 p1 Z( C' M# I- A
BSTR bs = SysAllocString(wsName);
8 k) f- }' m: b6 U& Q* V HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);
) j4 t0 M" O( T SysFreeString(bs);
6 Q7 i2 G) S+ }* `; p8 x3 g9 d5 @' x M& A) T
if(hRes == S_OK)
, k/ p' e9 \- ^ L s {* f$ G' f' c, A8 V
AnsiString s;
% I, l* G! q# v5 e. G. Y7 J' t Variant v = *(Variant*)&vValue;
- ?$ `1 X0 y- _( D3 q6 \: E* Z if(v.IsArray())6 ?+ o3 n( p4 y, t% S
{
) x2 S3 s s2 I6 k. U for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)8 L! I6 P8 o+ ~
{
& H7 z3 ]1 B: c% U Variant a = v.GetElement(i);& M2 u( e( k$ _; S: X. B
if(!s.IsEmpty())
" L* \# o) _& {: j* | h s+=", ";
, L! X# J0 A. X7 M& N# e8 Y0 ? s+=VarToStr(a);
' ^ n5 M- K2 Y! l( S7 u8 H }
/ U7 O) a, z* z1 r6 F }; E( _6 ?+ e% D Y, d& i- X3 m' M8 w
else
( N. q( J, Q+ v5 b# d {
- t7 E' u/ ~! h* D! I* L s = VarToStr(v);
2 q' ^% x1 r) T) ~% C }1 i0 E3 B/ L7 H( j8 V
lpList->Add(AnsiString(wsName)+"="+s);
4 d8 [7 t G9 _7 c8 q3 F }5 W6 z2 z! A+ i3 X* A
: s y7 b `% a5 ^+ h VariantClear(&vValue);5 ^: U0 u& v! D4 p1 t; J8 s& J
SysFreeString(wsName);
1 V% C( |4 `$ O( j1 Y }
b1 b7 G: E7 B) v: k }. [- s! a3 P5 x- D) G% |
if(pvNames)SafeArrayDestroy(pvNames);; | w' M1 ~3 i0 c! W) E) |
iEnumIdx++;" m! p# g5 ]/ ]% R' N
} C% a9 n i8 `* [1 I0 g' i
}9 m% f# b7 @" p2 ^$ E& P" F( L
if(pClassObject)pClassObject->Release();
& E% |1 Z/ x# E7 G9 I3 U6 _' h }
* [" R0 F, r9 S0 Z if(pEnumClassObject)pEnumClassObject->Release();3 A/ o- X# X7 G7 I. [3 i3 o- x
}/ T+ B. ?, m1 y# P
if(pWbemServices)pWbemServices->Release();
4 ?+ |! M% q! J- s) ?9 ^ }
8 g2 J& x* ^3 u) s# m- v3 u% c9 ^ if(pWbemLocator)pWbemLocator->Release();/ t- p4 e! X& |/ t6 W5 q
}" S" J, F( Y# q( c; N
//---------------------------------------------------------------------------
: e9 p/ Y: t. [$ N0 C/ H+ W6 `$ ~
- S+ e: m" Y. A. \+ S// 通过 WIN32_bios 获取 BIOS 信息:
* k( T$ _* `- l( T4 m1 Gvoid __fastcall TForm1::Button1Click(TObject *Sender)
) c$ V+ [ ]9 |# |- A( o0 W/ V{
+ ] }+ P) e3 j) R& F- h Memo1->Lines->Add("================== [WIN32_bios] =================");* B" f3 K. G; z
GetWmiInfo(Memo1->Lines, "WIN32_bios");
1 y9 D6 A+ r% v Memo1->Lines->Add("");3 H$ Q# j% p/ T$ Z, W: I( X5 Z7 h c
}
; s6 M: g7 _3 Q4 i1 ]% p
4 B/ h% \& r' N; H, x X" @' h' E--------------------------------------------------------------------------------
8 |6 f) Z! W" ?7 H
1 l# _5 j0 A! q ]& iWMI 可以访问的信息类型有:% A; R0 k# D& O
Win32_1394Controller
7 D) _. P/ g$ @9 [ Win32_BaseBoard8 J4 }+ `, h* k: F/ g6 F' J; B: s! M
Win32_Battery
7 q; c1 d! t4 |. C Win32_BIOS+ Z/ P e( _8 j+ W g" I, p4 d
Win32_Bus
( K) |7 ~; t3 `2 @2 A9 ?, K; r Win32_CacheMemory
% Q# v, S+ `& Y Win32_CDROMDrive
- g5 X/ h# y- r( V5 Z' U Win32_CurrentProbe
' B2 ]: n0 i% K- K: t6 g4 U( H Win32_DesktopMonitor
/ Z6 @+ C7 G5 {" J Win32_DeviceMemoryAddress. c/ Z* f- L+ P8 m9 P/ ^
Win32_DiskDrive
( ]9 s5 `0 U" q5 v% a# ~ Win32_DisplayConfiguration
9 c# J L" ~# ?) y% V* p" r Win32_DisplayControllerConfiguration2 g; h+ q7 Y. F
Win32_DMAChannel
; r2 j6 O, f) n$ {! Q, J) b Win32_Fan* n/ c( r, F: v+ y
Win32_FloppyController
/ `7 @- G+ m8 }! X; R Win32_FloppyDrive6 [; g8 E0 R& T- _7 I
Win32_HeatPipe9 Q; I9 q, _9 D$ x
Win32_IDEController
6 F' H% f3 U5 l$ H3 e/ a( T, k( G Win32_InfraredDevice7 t+ M) H0 H/ E' z
Win32_IRQResource
# _4 L0 L8 ~% {. o6 L# m) x4 | Win32_Keyboard
: M) H/ i' i$ ]- ^' O* [5 t& c' Z' m3 V; U Win32_MemoryArray
9 s9 {- Y! S- x0 e3 M$ H Win32_MemoryDevice- _$ f; a$ I# p8 N0 I
Win32_MotherboardDevice
( r q# G: E/ M. Z9 Y. H Win32_NetworkAdapter6 m" Z& o' [8 b5 B; c
Win32_NetworkAdapterConfiguration3 i' x8 g) X8 @
Win32_OnBoardDevice9 p# G$ p( U- x6 M; l( q
Win32_ParallelPort
# H" r6 e5 r# @ Win32_PCMCIAController- ~* Q T* F! k2 G
Win32_PhysicalMemory
j N$ N! S6 Z, ~5 { Win32_PhysicalMemoryArray
3 I/ N8 d: b: m$ Q Win32_PnPEntity0 A) w+ h Q$ O0 I z& _6 o
Win32_PointingDevice
4 {& Y3 D" b" V, ~+ `8 L Win32_PortableBattery
* V _ K1 ~6 f7 s Win32_PortConnector
3 }+ ?6 t5 k D( `9 a( P( s1 X Win32_PortResource( P6 I4 V0 l- j" s
Win32_POTSModem
1 x0 }4 n9 X1 B, @6 J- R8 N, h; v Win32_PowerManagementEvent6 B* z$ ^6 T) w/ i/ N
Win32_Printer
0 j+ e" D& O: s0 A# N- M Win32_PrinterConfiguration- j9 Q0 m0 Y9 `6 G, G' E5 [
Win32_PrintJob
8 {, I& ?( F' F" C' e# L Win32_Processor
) k: |# i, H9 P9 a! T* c5 |) s Win32_Refrigeration
* r( E5 i* m- j1 `8 x Win32_SerialPort
: L1 F( I. A+ P) J8 G Win32_SerialPortConfiguration
* J( Z6 G5 R% K4 g4 \- x- q Win32_SMBIOSMemory
' h8 Z. N2 Y) d' u% W Win32_SoundDevice
5 T+ k/ F, X/ q7 l* j Win32_SystemEnclosure( H1 n" ]/ u# q, H$ c
Win32_SystemMemoryResource$ D0 v! ?0 T" r. {
Win32_SystemSlot2 O. E* Y4 Y' K8 P* d- Y
Win32_TapeDrive4 z& Z* O/ U8 G! [. ?
Win32_TemperatureProbe( K+ t/ N; ?! R) d
Win32_UninterruptiblePowerSupply( l2 A# p ?. T; ^
Win32_USBController
' x6 L3 ?7 w! Q) q2 S* f9 U Win32_VideoConfiguration
, v1 z/ h$ `% U% w. Q" Q/ V Win32_VideoController" R( K; Y: f2 I' B6 U, J! F
Win32_VoltageProbe
1 H& I! Q. s* I7 G4 v/ y9 \( a- k) D/ h O7 E1 w" ~
以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios"); |
|