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

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)/ n1 j! [: g) P5 f7 F

3 _4 `, z) a3 {0 f0 F- |& l. S
  h( G, n# v1 Q--------------------------------------------------------------------------------0 v: K- P) a. B0 E, L# [: J9 j8 o4 Y% `
WMI: Windows Management Instrumentation (Windows 管理工具)
4 q  o7 K/ S# Q) m   通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。 ! o2 w% v- d2 j
  利用这个工具可以管理本地或客户端系统中几乎所有的信息。
! J- z  ]/ N" P& `5 p6 e; t: ?   很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。 . t+ N' m' }4 s( P8 S: m$ i

7 v7 Y9 F( y: c1 \5 m--------------------------------------------------------------------------------" Y9 M: G7 k# ^, n$ y' k" N8 Y. K
BCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面" S7 W% F2 Z+ Y0 S0 K" q, F
. a2 c+ T1 D2 N# U& H: A/ r
--------------------------------------------------------------------------------
0 X6 T6 ~, K7 |$ j① 初始化 COM 接口:3 O+ h; J$ E  H8 R" F7 K
  访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。, g7 q; A( |3 P6 [, T8 K
  这两个函数在 #include <comdef.h> 里面定义。
0 @3 X" ^4 O# F# g4 O9 P3 s% T, o
② 获取访问 WMI 权限:
9 {. z& T, b6 a; U   CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);+ l- B, i5 B: o# t. p0 v
  如果这个函数返回 S_OK 获取权限成功, 否则为失败。# V# N7 v0 O3 j7 B

5 I( Y* D9 W$ ^9 z1 B/ z③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:/ n. K1 Z7 h/ r/ \2 o6 m9 `
  这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。
" O0 _/ U+ {( Z% R9 H+ N$ x* n' s9 `: w
void GetWmiInfo(TStrings *lpList, WideString wsClass)
- M4 A. p% q* H+ M7 a( T{1 B* K9 d' {! |" V
  IWbemLocator *pWbemLocator = NULL;
% Q* ^" F( e8 n, i# o9 O  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)& y5 U1 q1 u! x; s* ~, f
  {
0 ^! A6 x6 Y& Y- w     IWbemServices *pWbemServices = NULL;# q* H) t5 t& Q* j2 N9 u
    WideString wsNamespace = (L"root\\cimv2");
" i- X, m( c9 M6 @3 ]+ M     if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)
5 W0 j8 L# Q4 e' w. S9 k1 x+ f( t- K; M      {. {+ t, u- M. c6 f7 a
       IEnumWbemClassObject *pEnumClassObject = NULL;- D3 W+ `) L" D3 \7 B# R
       WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;
  L! }7 K* ^, Q$ `        if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)6 b, Z' I& }# F, ]
        {5 d  w+ @  h- h
          IWbemClassObject *pClassObject = NULL;6 ~! W" d! m2 g9 J3 }' a1 R
          ULONG uCount = 1, uReturned;
! B* B0 o2 L& |. r           if(pEnumClassObject->Reset() == S_OK)) B, q1 {' s) B
           {
/ s6 ]& k4 p/ t( C8 B- J+ S8 A/ o              int iEnumIdx = 0;
- S, j- ?$ C# u& ]! h# `! U8 y              while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)$ P* z5 {7 b9 Y# \( ~
              {
+ ]8 t* v+ g, A+ `4 c                 lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");" H+ n3 N3 C0 X2 p7 q

5 m2 m5 O0 o: ^! N                 SAFEARRAY *pvNames = NULL;
# }6 ~9 W) ~  s( i/ P" o                 if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)
1 P4 Z; E5 b/ h8 r  X" N                  {1 ^1 M; ?- Z# p# k) [+ D
                   long vbl, vbu;  e5 x) j3 q  c7 |+ M
                   SafeArrayGetLBound(pvNames, 1, &vbl);: ~1 w; X+ z4 ^+ B/ ~8 h
                   SafeArrayGetUBound(pvNames, 1, &vbu);2 A3 H' E; Q. A" [. b. u) S8 d% g, S* A5 s
                   for(long idx=vbl; idx<=vbu; idx++)
0 R' ~9 K6 D( ~4 O: H1 f  r- N                     {
$ {$ B1 n2 m0 ~& \- g* J1 P* k0 N                       long aidx = idx;
" T8 E( S1 b. Y                       wchar_t *wsName = 0;
% G( X* Q! w& T                       VARIANT vValue;
( Q& Q+ {0 P3 S- C+ E                       VariantInit(&vValue);2 C2 v7 Q! m& F0 l$ o
                      SafeArrayGetElement(pvNames, &aidx, &wsName);& ~6 @* L( f) s$ u! O
" k' Z+ A% o' C* g3 _2 e1 [
                      BSTR bs = SysAllocString(wsName);
* |: U$ f# V) `6 I( c& }                       HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);
+ s& S' M5 @  a                       SysFreeString(bs);
: i/ K1 N$ O. R; V
2 E  ^& v# h' o1 k                       if(hRes == S_OK)
$ R# K! U& E6 j. T# A! ]* Y  T                        {& u! Y: A! f* `2 @8 y- t& w
                         AnsiString s;: Q- p$ {9 Q& t8 n
                         Variant v = *(Variant*)&vValue;
+ ^9 O  {' X% G  w' m                          if(v.IsArray())
; V5 |8 g+ J7 w6 P' X" y+ f                           {
# t  a7 x0 J7 n* m* p& f8 w! E# Y                             for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++); e: k7 W) G) e, O# i
                             {
% n. H. V& Q- Z+ f' C* B1 N+ Z. ]                                Variant a = v.GetElement(i);: `2 }) l% N8 c
                               if(!s.IsEmpty())2 v* {4 N) }' _4 S2 }2 U
                                 s+=", ";
: ^7 S! e- B, t; U3 ^4 Q: K; a                                s+=VarToStr(a);
3 }4 L' _8 ?# d' s9 V                              }
+ q/ Y/ ]0 ?9 O6 C                           }
% s' D9 P4 E3 Q6 ^7 ^5 h+ e( |  ^                          else
# ]2 w- e/ v( e/ L# ]% g6 Q                           {
+ n: X; m' Q( G2 p                             s = VarToStr(v);
7 f0 r  b' `% R                           }% U! w# u/ o& \9 `/ ]
                         lpList->Add(AnsiString(wsName)+"="+s);/ `" P. ^6 I/ P
                       }* E, X% G  Y4 y7 B+ t+ S
$ x: Z- n+ i6 P
                      VariantClear(&vValue);! Q' s8 U/ M8 E% r' r1 d8 I" N8 l
                      SysFreeString(wsName);7 r  d* b3 ~) {, f9 ]" S
                    }4 g" F& u. L9 u* B1 N8 ~0 D% }
                 }
7 {! w- k6 Q3 N3 g- Q! h7 d- m5 R, U                 if(pvNames)SafeArrayDestroy(pvNames);
8 X0 m; M: ]5 u0 m+ r) Y                 iEnumIdx++;0 G8 E) U/ G, r) q8 ~
              }& E5 L5 r/ ~: |+ p: Q( v7 H* Z" c
           }6 P; F2 M/ e  v* F( a6 ^
          if(pClassObject)pClassObject->Release();. p/ [# Y& r/ w
        }
! ~9 z6 n5 q: @& a        if(pEnumClassObject)pEnumClassObject->Release();
) |/ Z( T5 h5 i+ @+ j5 x      }
% s  B, }  p5 L9 w7 c3 P: `; f     if(pWbemServices)pWbemServices->Release();
( s- L: P9 c: \$ |8 H. C3 ]. V   }, {& k: z, W2 h3 |
  if(pWbemLocator)pWbemLocator->Release();) V& [6 h, X* h8 \1 u8 U3 n& `; K
}* F( v8 o: }4 l( k( |$ h8 k, v4 x
//---------------------------------------------------------------------------8 @+ B" w2 l' t" L# \3 D, O: Z

2 q8 H  d+ d# X- A! C4 I0 T8 E0 [// 通过 WIN32_bios 获取 BIOS 信息:
/ |+ b9 E4 Z5 D1 D" [void __fastcall TForm1::Button1Click(TObject *Sender)
+ z" Y, |; x0 b' B9 O; v{
1 D3 l! ]( S) C+ i  t* z" N2 O! \    Memo1->Lines->Add("================== [WIN32_bios] =================");
8 V3 h! T# m! w# d0 ?    GetWmiInfo(Memo1->Lines, "WIN32_bios");6 Q6 W. a# G5 B# t. j! \( R, _
   Memo1->Lines->Add("");5 u  G6 X3 w5 p. g+ p
}9 V) _+ k1 v- ^# _
5 e  h& _( N7 ?6 l. f
--------------------------------------------------------------------------------) D% j% j; F! f3 b

5 f- [( _% E; Y3 h7 |' u( zWMI 可以访问的信息类型有:! V( t8 s: A$ M
  Win32_1394Controller
3 i7 X) k6 Z1 d, \   Win32_BaseBoard
9 v2 r, x& m" t2 ^8 }5 D/ t5 M8 V7 ^& K   Win32_Battery
; T! W+ `, H" F( ~   Win32_BIOS
4 s  |2 _% j: T- u% b: }  {  y   Win32_Bus" N/ \& A7 l9 K0 _7 ~
  Win32_CacheMemory
: J& }1 I) c8 }$ x' J   Win32_CDROMDrive
. G8 u" I% T2 e! o   Win32_CurrentProbe5 @+ A. {1 t6 U. P
  Win32_DesktopMonitor
& F. _# Y) [  Z1 Q- l! L" d   Win32_DeviceMemoryAddress
" m: ~2 a' {2 n* P7 B/ b' g9 D+ O   Win32_DiskDrive7 `4 f: V) B7 P) O9 [2 j
  Win32_DisplayConfiguration! Y2 M- o/ Z0 ], }  U- V
  Win32_DisplayControllerConfiguration2 V+ O+ H  ]9 d" N, A
  Win32_DMAChannel
9 F3 e5 o' d; i( ]! R+ o' y   Win32_Fan7 @0 Q2 G0 q) ~) F( ]. S& J
  Win32_FloppyController
8 _7 l& [/ N4 y  n% n, D   Win32_FloppyDrive
4 ?5 T" K8 F( d4 q' O: {: g0 J- Y   Win32_HeatPipe
: X/ F8 `! Y$ V4 d   Win32_IDEController
' i# K  H/ ?7 ^. `0 i   Win32_InfraredDevice) I8 O  m( q" d( ]1 ~
  Win32_IRQResource2 w1 H" r( @0 Q& l3 t
  Win32_Keyboard
7 t& c$ s1 h0 T$ w/ ~) q7 K7 b: T   Win32_MemoryArray
/ ?7 |$ D: H2 h, v* }   Win32_MemoryDevice: e2 f1 k  z* ~; w
  Win32_MotherboardDevice* t+ N/ E! u4 s2 a( f" o& d7 x
  Win32_NetworkAdapter
1 U/ h+ ^4 }' a   Win32_NetworkAdapterConfiguration3 S$ D" r$ T- ~( S+ r. D
  Win32_OnBoardDevice8 V, h" K( t6 s. b3 W0 Y% V
  Win32_ParallelPort5 J) a& S% t( P9 w& ]2 H! S8 `
  Win32_PCMCIAController
; F4 I4 n, N+ B( W   Win32_PhysicalMemory
6 o% @3 M/ y: W  A   Win32_PhysicalMemoryArray$ U  z5 B2 ]$ w6 ]
  Win32_PnPEntity
% h) H  O1 |8 D. j   Win32_PointingDevice
6 C& e& T* L1 M; i: z! j   Win32_PortableBattery
; x2 P4 G3 [5 |; d. v7 ~   Win32_PortConnector
" d, A$ o* A: B. K+ R! O. U   Win32_PortResource3 H% ^+ i/ \% c
  Win32_POTSModem: O4 Q  V# d1 O' ~" v7 q
  Win32_PowerManagementEvent+ y2 r- z3 @# k4 g8 G% Z
  Win32_Printer
9 P1 l- }0 @2 U, S, f" U: i   Win32_PrinterConfiguration
$ T0 i' ~) ?9 |0 E& {   Win32_PrintJob. ?1 P$ O5 ^/ {! x0 [
  Win32_Processor( z0 L. ?8 c  S: W) A1 r. o* Z
  Win32_Refrigeration/ C2 x/ I% l+ D6 G/ j4 J9 D  L: L
  Win32_SerialPort
6 i; r6 y9 z& d9 B: d* k4 M1 A" Q) ?   Win32_SerialPortConfiguration$ V' M3 k" q) [- E; ?
  Win32_SMBIOSMemory! J* n6 b/ M0 D& u
  Win32_SoundDevice7 S1 |) V; O8 Z: q
  Win32_SystemEnclosure
( a' K" n, d  Z* Q6 ^   Win32_SystemMemoryResource* e- w) |" R2 I. v  f. O8 J0 O
  Win32_SystemSlot
! C" u  t' u* P2 k- X3 i, c   Win32_TapeDrive
7 ?, f/ ?. d9 h! ]4 o& f1 L   Win32_TemperatureProbe
' L  \4 V' E4 h" H   Win32_UninterruptiblePowerSupply% e; f9 Q! k/ r: ~
  Win32_USBController( M5 Q2 C' W+ t6 V" Q
  Win32_VideoConfiguration8 H  c7 ~8 E1 |4 {$ w
  Win32_VideoController9 j- N1 M  O% Q# Y0 M5 b3 R+ |
  Win32_VoltageProbe
9 {: O/ H/ C$ U1 f1 ?3 d" m# [* `4 q/ F" @) k- q- k
以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-11-29 12:18 , Processed in 0.018698 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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