找回密码
 注册
搜索
查看: 12652|回复: 0

[收藏]C++ Builder 通过 WMI 获取系统信息

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)3 G! o# D& k9 w; i0 l

) i; y, }8 M8 j
0 e8 e7 K) r  J) e, Y$ }--------------------------------------------------------------------------------6 I2 y6 i2 y+ O) y& b) c% L
WMI: Windows Management Instrumentation (Windows 管理工具)
6 ~" a; N' y7 q- F, B   通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。 ; F' \1 T  n4 y( ]: b- w1 B5 z
  利用这个工具可以管理本地或客户端系统中几乎所有的信息。
' T; H- p6 ~! u2 d   很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。 % y5 {7 {( A% m. N1 A9 }, n: ~

6 a- `: x. n2 p--------------------------------------------------------------------------------3 |6 b2 M) D! e0 V) V+ Z& o
BCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面' w2 g: c& }0 J8 m) D5 R. n  V
3 }2 k% t; P6 d3 c
--------------------------------------------------------------------------------1 V/ `# i! Z" t$ u# c8 f& [. C
① 初始化 COM 接口:
) ~3 O; G/ p* D' j) y   访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。
* r+ m: D3 F4 ?% m; i   这两个函数在 #include <comdef.h> 里面定义。: |. n- N: U( H$ ~

) q0 J  P' l! W# D& B7 x② 获取访问 WMI 权限:
) m0 S& X$ E+ G5 H   CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);
6 V' N* r  f! h( ^& u   如果这个函数返回 S_OK 获取权限成功, 否则为失败。5 U4 F, u) v1 [* U
) y8 U% n) u# E+ S
③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:
9 t' s4 ~0 x. J   这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。1 f" }6 C" ~3 _7 ?8 y

& ^( J: M/ _; u& Tvoid GetWmiInfo(TStrings *lpList, WideString wsClass); ^9 W: F, h0 e
{
# o9 P) c* {) {: B3 m5 W& ~  IWbemLocator *pWbemLocator = NULL;% u) W1 l/ ^" C# l1 L, P) O- l( [4 v
  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)
$ H* o- F+ ], }7 N  y/ L   {
5 u) d. M; o% o' m! ~$ c* H! B' U     IWbemServices *pWbemServices = NULL;
3 v( F1 ?; }1 g3 ]( i9 b8 `     WideString wsNamespace = (L"root\\cimv2");
  Z- c9 F0 D, r9 e$ ~/ g- g     if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)2 Q" R2 p" f- {  }9 E
     {1 u" `- k, Y! L5 ~! C
       IEnumWbemClassObject *pEnumClassObject = NULL;, c4 M% ^% \( ?- `5 e/ D) o
       WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;$ U  Z; W8 h0 z/ ?
       if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)
# _1 ~2 L: K$ z% b+ n. Q         {- F; S4 q& v( a
          IWbemClassObject *pClassObject = NULL;% E, f- `7 E& @+ R
          ULONG uCount = 1, uReturned;
% o2 e" [) C- A2 ?           if(pEnumClassObject->Reset() == S_OK)
. R5 D% K  [4 n5 K9 q            {5 r. J' ~  q/ j7 h8 L
             int iEnumIdx = 0;
! [( e8 p, B5 g  w; a8 @6 k              while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)
& `4 C) |) ~+ \: c               {  c6 [, c; z# @6 N% g% f
                lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");9 @" e4 f( ]8 ^. }, Y

* X0 s8 R9 a9 v7 l8 [                 SAFEARRAY *pvNames = NULL;1 P9 {, |- X# V4 k, `
                if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)
" {$ W* C! R- P                  {
, l) V& c8 }" O1 j3 V1 B                    long vbl, vbu;
2 h2 z* r( |+ u                    SafeArrayGetLBound(pvNames, 1, &vbl);
: n7 P8 @- g* F) B( |, }+ i                    SafeArrayGetUBound(pvNames, 1, &vbu);
9 D; M. c* T3 T7 R! U, H                    for(long idx=vbl; idx<=vbu; idx++)
+ q- _% o9 a6 i# W                     {, X5 w! U2 Z) h- h' l$ V
                      long aidx = idx;+ @2 N8 r- F+ {  s5 ?
                      wchar_t *wsName = 0;
4 g5 c( o8 O3 m: w# e) _                       VARIANT vValue;2 ^* y7 k# m' v5 K! A; G
                      VariantInit(&vValue);
8 X5 M1 g0 A, c. F( D- S                       SafeArrayGetElement(pvNames, &aidx, &wsName);
! Z: \2 y% }& ]( D2 r8 M3 ]# y) G, \- ~( g. H, k' `
                      BSTR bs = SysAllocString(wsName);" S& b1 H! \7 ?* U( x
                      HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);: F0 A6 G& Q. t# N; F; b
                      SysFreeString(bs);
8 u% ?  J% H6 J) ^/ ^3 B3 D/ |, ^. ]* Q& I% Y" h/ a2 C
                      if(hRes == S_OK)# _0 h3 R7 x3 V0 t3 c; ~! U
                       {' Y' C1 s5 m# z: Y8 L0 U$ [6 V
                         AnsiString s;8 k4 E4 P, X* n' o/ K: ~3 `) w
                         Variant v = *(Variant*)&vValue;
  g. S  c1 p" U8 L                          if(v.IsArray()), ?* o6 w0 K4 a7 v
                          {* F2 ~) ]+ s; Y! d5 F
                            for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)) i. b+ g* U7 ], I  i
                             {
- a; ~& {0 b4 x4 D                                Variant a = v.GetElement(i);
5 C1 x# h2 O4 |# O2 u& l2 A: i                                if(!s.IsEmpty())1 Y2 m5 q0 C8 @+ J/ y  P! ~0 d; [
                                 s+=", ";, M7 i8 g6 @$ I3 ]6 L# W! I8 ?
                               s+=VarToStr(a);
4 p  B* F5 c0 K8 O, I* l8 P" z, N                              }5 `+ r- Z: Y4 s' i3 X1 I2 |
                          }
+ |' }4 H7 o0 N0 H7 |+ ]                          else1 A: u+ m5 p; X. s) x- B
                          {
1 z& E1 y2 {" g1 b6 U                             s = VarToStr(v);7 y& f# A3 `+ g6 c. d% x
                          }3 [; o% s1 [" {# j( z
                         lpList->Add(AnsiString(wsName)+"="+s);0 Q6 n& ]6 _/ g3 _
                       }0 a- T) X2 c- O, _# p

* ~1 L  X8 C) K5 {$ Z( ], X                       VariantClear(&vValue);) s0 c- C* O: d! ^
                      SysFreeString(wsName);* W0 [5 \; T& ?8 w1 X3 l
                    }1 L. a, ~" v+ }# S; ]6 Y; v
                 }
4 n  J1 `+ x$ W+ Q. T0 Y                 if(pvNames)SafeArrayDestroy(pvNames);
# t- U% m4 N  `. w9 i                 iEnumIdx++;3 |+ _8 J% l0 @, `2 R2 p
              }- S2 {, ^* Q! u5 B: J# ~- Y" ^$ z
           }
! A2 ~1 N1 V, k+ \) K+ ?           if(pClassObject)pClassObject->Release();
) v6 s6 E6 j- f4 K& S: s         }
; E; m: Y6 u3 @- P1 v8 D        if(pEnumClassObject)pEnumClassObject->Release();- f) S; i* Z  h( _/ A, i
     }. W: `  Y9 t* [0 p5 t
    if(pWbemServices)pWbemServices->Release();
. X5 h. _' W3 G2 e   }- i) l6 K7 ~: ~! j
  if(pWbemLocator)pWbemLocator->Release();
2 f% l* V5 \3 s. K5 i( c, g; Y}
) `6 Q0 S! X( @( j# ?& S//---------------------------------------------------------------------------' o- P& e0 j* d- J1 w0 ]0 E: |6 J! s

6 l# u2 H7 y9 Z9 R) |// 通过 WIN32_bios 获取 BIOS 信息:
: m$ W: V! C0 E, z' d/ E/ ]9 m, ^! L$ \void __fastcall TForm1::Button1Click(TObject *Sender)
( ~9 ?8 C* f4 p" T$ j$ T/ h{
2 y1 }6 g7 C3 T: P6 [1 ~    Memo1->Lines->Add("================== [WIN32_bios] =================");
  p/ u4 S: M  d5 w    GetWmiInfo(Memo1->Lines, "WIN32_bios");! Z% o  ^9 l& R: L* J1 t: g
   Memo1->Lines->Add("");5 W9 ^( D9 V! v# u7 ]4 K
}
- h2 p. e! j# j2 \
  E( s' D% U4 q+ H  f# ?, a7 J# I--------------------------------------------------------------------------------9 X) E( T) `" J4 I
! B3 d/ t2 L( z/ h
WMI 可以访问的信息类型有:
0 F* g' p7 y8 j8 I! E   Win32_1394Controller
7 A9 R; f7 B1 G- L- V1 s) ]9 ~   Win32_BaseBoard
; X) {2 Q: C' _4 O* k9 E   Win32_Battery' c* Q0 t0 O( z' a( m, D7 U
  Win32_BIOS- K) i, `# E- i/ `* P/ @3 u2 y2 x
  Win32_Bus
, g, u4 C: M/ C9 t   Win32_CacheMemory
+ ]& x0 p1 K, w" {   Win32_CDROMDrive
  |+ p3 L% x7 I1 a1 C1 |" q6 c   Win32_CurrentProbe
: D! \9 o, ^5 q+ x) J8 ^9 `   Win32_DesktopMonitor0 x, J" F8 o4 j/ K' U
  Win32_DeviceMemoryAddress% V3 l/ ^. g6 T/ c0 }
  Win32_DiskDrive
; ?$ v+ _; J" Z7 L   Win32_DisplayConfiguration5 T. L4 H5 g4 @  w5 c# z3 h% K0 u
  Win32_DisplayControllerConfiguration& R! N. X/ ?9 }% e
  Win32_DMAChannel+ C7 }: t) [7 u2 d2 n* v2 D! [. E
  Win32_Fan
3 r  N$ ?: U* d- q1 a- a' r2 e   Win32_FloppyController* K' O: e0 O, U7 J6 R7 C
  Win32_FloppyDrive
- d# ]0 w! b2 k0 q6 `! c0 ^   Win32_HeatPipe! F/ T+ B! w* _0 a8 ?+ [# y* `# I
  Win32_IDEController
, X$ A& M5 o" }2 q+ Q7 X" d   Win32_InfraredDevice5 ?1 O: J% g5 I+ `' T( J9 b' w+ z
  Win32_IRQResource
$ I  x/ M( K- x! ~& b   Win32_Keyboard
) Q! K# J9 R0 n- Z- x7 R; t   Win32_MemoryArray
# O& [0 `4 ?+ D1 H7 j   Win32_MemoryDevice( {9 c/ D5 Z8 d+ i
  Win32_MotherboardDevice
# R1 X) i. g! ?   Win32_NetworkAdapter3 W0 B8 {3 S8 r4 O* f( E7 V
  Win32_NetworkAdapterConfiguration- E! B; i' F3 u* l: x: }7 ~' {; I
  Win32_OnBoardDevice! K) }) \: w+ w9 P& f5 \
  Win32_ParallelPort
/ w7 G5 h6 R$ ]% f& _   Win32_PCMCIAController) z% [5 h  g- e5 K
  Win32_PhysicalMemory7 H1 e# p, b, u- B, [
  Win32_PhysicalMemoryArray
+ e+ s9 ^( q9 S) u+ S. C   Win32_PnPEntity0 K- \. {% \6 s& n% f
  Win32_PointingDevice
2 A# ~0 v9 m1 N8 {# A8 F   Win32_PortableBattery1 s, d! |) U* {; z) i  h0 f. V
  Win32_PortConnector7 \" s" U2 T: E( E6 ?7 H2 V: u
  Win32_PortResource
8 k. @* l$ l3 T+ T7 u0 f   Win32_POTSModem
4 u/ ?; J; p! `2 l% Z   Win32_PowerManagementEvent( ]1 W4 ^9 z9 f* ~8 n
  Win32_Printer
0 a% `: q$ x& J   Win32_PrinterConfiguration
9 o/ h; ~8 s" g& d* X- g4 E   Win32_PrintJob
# M2 b1 c: |  p7 w. n   Win32_Processor& s1 F1 p" s  k  U
  Win32_Refrigeration3 e7 H0 X6 l. X- R. S6 t- j
  Win32_SerialPort
. W  n. ~4 N: d8 M7 q   Win32_SerialPortConfiguration& w4 o5 {' h/ g* `$ f7 B
  Win32_SMBIOSMemory
1 s% l/ N, W6 U   Win32_SoundDevice
4 M  }; }' I, l   Win32_SystemEnclosure- J& Y, `/ W# Z/ R! J8 u% I
  Win32_SystemMemoryResource- A' T  b" D+ x- g; h  @8 i' R
  Win32_SystemSlot5 |$ q  D6 k, J' J5 d6 d
  Win32_TapeDrive
3 H; ?- l; r( @* C% a3 @( H$ @   Win32_TemperatureProbe
0 H. F$ U, f, w! u' }9 S   Win32_UninterruptiblePowerSupply9 G% }! j. `; ~  }; n: S9 p: \
  Win32_USBController
; c' X8 n2 D, x3 \2 Z& C   Win32_VideoConfiguration# e9 J& V6 K* m4 G: H
  Win32_VideoController
3 @% W6 A+ Y. B* G; f. {$ E% P   Win32_VoltageProbe
2 y6 \4 o0 J5 N: p
( Q2 b8 }( u% u) `/ a2 x$ c以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|宁德市腾云网络科技有限公司 ( 闽ICP备2022007940号-5|闽公网安备 35092202000206号 )

GMT+8, 2026-1-2 15:56 , Processed in 0.022249 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表