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

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)4 [9 ~2 v; h( m2 g

% S1 l5 O! t; g3 |5 l9 e1 @! \( }- Z. K- m; N3 e& ?
--------------------------------------------------------------------------------2 ?$ b5 [$ M- R* y: Q% }6 Y
WMI: Windows Management Instrumentation (Windows 管理工具)
% t2 \0 G! f7 o- R/ X- m   通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。 ( x" a4 k4 L# P: m/ Z
  利用这个工具可以管理本地或客户端系统中几乎所有的信息。$ t# J) P1 ?' y# O; j+ G
  很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。
, b7 D, s: l+ Z7 Q) n3 B2 S& V( X4 K" ?4 \
--------------------------------------------------------------------------------
1 t* K6 ^: ?- ^2 X8 J1 z, fBCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面9 R5 t: {, L& }" e1 ]9 {3 R2 M

; |9 m% |/ a1 c1 _5 J  e--------------------------------------------------------------------------------
' T+ v9 e3 u5 @; y; i* @  i$ v① 初始化 COM 接口:
2 Y4 O; ]& S6 R: H) B   访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。
" M( [! m: `8 ]( H  h$ `. m+ e  R# G& \   这两个函数在 #include <comdef.h> 里面定义。, P8 v6 J9 s6 o+ Y% o; @
) ]+ b; `. b" e5 a
② 获取访问 WMI 权限:( t7 r6 N7 t" [/ J- o
  CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);
; Q9 G! Q( U( p# U/ c; j   如果这个函数返回 S_OK 获取权限成功, 否则为失败。, v& x" M- y" B% F3 G* {+ ^
6 |$ b: Q- J$ `/ n% }9 y
③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:: {3 k, Q0 ?6 [& e- K
  这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。
3 _' M$ j+ _* \* Z; J9 t5 y
1 z) L. C# }/ ^void GetWmiInfo(TStrings *lpList, WideString wsClass)
% Z1 u. N: p% a9 i5 _{7 @& x9 [) E5 N9 ~6 m
  IWbemLocator *pWbemLocator = NULL;
4 E7 [( q  d& i  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)
7 Y& k+ @5 n0 @# ]9 H! D   {" c* n8 ^( D0 t- I2 ~9 r
    IWbemServices *pWbemServices = NULL;8 o/ A- C1 x  S/ C
    WideString wsNamespace = (L"root\\cimv2");
* a' a9 E* h9 F" H     if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)6 G3 p+ J: a- t/ T% a
     {
) b4 p5 h* g0 k: S, v; E* l        IEnumWbemClassObject *pEnumClassObject = NULL;
$ [2 n, A3 n, W% k7 s3 v  @        WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;. `, g3 m9 r3 i( b% j% F. ?
       if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)
; t/ [9 ]6 J/ ?         {% g6 @6 Y. R( U; g
          IWbemClassObject *pClassObject = NULL;# e  \& z) B0 C2 F. p
          ULONG uCount = 1, uReturned;
1 }0 \. k9 H8 \, t4 ~           if(pEnumClassObject->Reset() == S_OK)$ L; e' S  `- o
           {' a1 I' Z, I6 q; i. p% p
             int iEnumIdx = 0;
1 }- u$ q% Y' i4 j" \              while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)
' w% E! h' ]3 o, F& h' @               {
$ r6 E- v9 f" ?2 a                 lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");. A/ w, i$ \9 n+ e/ M
4 {$ D  n  Q, y; V: _
                SAFEARRAY *pvNames = NULL;
2 S6 P- J  A% x( \                 if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)5 K' M4 R+ W. a1 m5 h& ~
                 {
2 j" E  u5 g4 z( @- X) R" n9 A8 |                    long vbl, vbu;
+ a$ z# s9 E- I6 P+ o                    SafeArrayGetLBound(pvNames, 1, &vbl);  J/ Y! g% l: u1 f# N0 r, F
                   SafeArrayGetUBound(pvNames, 1, &vbu);2 e& v" G6 F- @: Y; R( l2 G% w- L' b
                   for(long idx=vbl; idx<=vbu; idx++)
( \2 J3 V5 \0 j& Y) _: O# E& S0 B                     {+ I1 v. L4 a" p! ]+ ^. W
                      long aidx = idx;, j2 t; h0 O7 I" e, ~  E3 ?
                      wchar_t *wsName = 0;' h- i% g  m" {( s6 x: r0 S
                      VARIANT vValue;7 ?/ v' z+ [- b
                      VariantInit(&vValue);( f  M* N" h7 N4 x0 C/ z0 [+ G2 y# }
                      SafeArrayGetElement(pvNames, &aidx, &wsName);& n; r: X8 c7 n! V

, ]/ c0 Z* a5 l1 B                       BSTR bs = SysAllocString(wsName);4 f4 l: P- C( s/ V$ N) T7 V) g7 \
                      HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);
$ H+ |  x( f  k% w' [                       SysFreeString(bs);7 B3 d8 p: ^2 R9 R' O) ~
' p+ }0 G) `6 ~8 {# ]3 c6 v
                      if(hRes == S_OK)
+ l" N; r) B6 x! M& ?* H                        {
3 n+ `' u# y. |& E) o                          AnsiString s;
1 n* V7 o- \# F" i' p. V                          Variant v = *(Variant*)&vValue;- J( w7 n% y/ ~) J5 v+ d- y1 H
                         if(v.IsArray())
5 b6 x4 V6 s, x/ k5 ]                           {& u- E5 X2 M' }2 A' i
                            for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++), O& R* [3 S" U  u4 t" i
                             {0 ?; i5 I3 d- e. D. M" [% b
                               Variant a = v.GetElement(i);
! G, W4 a' K# F                                if(!s.IsEmpty())
# u) @5 P& m! l3 I/ i9 N  [                                  s+=", ";
, f; k# b2 F. h7 p, O3 a) Y+ a% @                                s+=VarToStr(a);
. g4 y: T/ ]5 G" \, g! o                              }) ]3 ~: U7 M! a/ N6 S
                          }" ]; j  B  c8 K
                         else
7 X! ~/ y  M3 `$ c                           {# [) ?, ^5 Q0 G; n4 i
                            s = VarToStr(v);  V- |+ {, H' K- h- ?/ j
                          }: D2 h! ]8 W9 O+ v2 r0 a$ o
                         lpList->Add(AnsiString(wsName)+"="+s);' B- X) Q7 o* B$ ^3 J! y5 }
                       }
: ]9 e: z+ k1 c- P0 a& C/ U
9 n9 F3 P. g* E0 J# f4 G                       VariantClear(&vValue);$ B* Z4 l) j# x6 N. h# J  d
                      SysFreeString(wsName);/ S# y! C+ r- {$ S) @* q3 E7 V
                    }
( S7 w3 L# Z# e; H% e& S                  }
% B4 T; T& E* e: ~3 U8 _                 if(pvNames)SafeArrayDestroy(pvNames);; C" K) W/ Q  I# P) g
                iEnumIdx++;
* H9 E$ ?, V! `9 b4 Y/ x               }2 {, u, H6 b# K
           }
* M5 R+ s$ i. R           if(pClassObject)pClassObject->Release();
9 U7 _5 _; I  H1 |2 [- ^         }
: B/ I! b# \7 k2 }) n  y        if(pEnumClassObject)pEnumClassObject->Release();
' ^/ p/ X7 {+ V1 ]- x      }
3 k7 @' s* g, d+ r% ^% \3 w     if(pWbemServices)pWbemServices->Release();
) P: K1 l& L, K, D2 |, J# L- p# S   }7 \5 t$ R) v. ]% E6 H
  if(pWbemLocator)pWbemLocator->Release();% G0 _$ Y% z- J1 I5 G' I1 w
}) M( \& Q3 @, q+ b2 D1 P3 M
//---------------------------------------------------------------------------3 H2 y$ b. N' {1 Y' o

8 e/ {# r8 h) l" V) y2 c$ t. }// 通过 WIN32_bios 获取 BIOS 信息:
* V+ e7 v  P4 D8 _void __fastcall TForm1::Button1Click(TObject *Sender)# m# A: K6 P* K; j4 z" S
{
/ I2 T4 s! Y4 X/ `/ x# o% S    Memo1->Lines->Add("================== [WIN32_bios] =================");( X: _' |/ ~& L& {# [* S4 T  u; b3 H4 b
   GetWmiInfo(Memo1->Lines, "WIN32_bios");
- W" G; P3 H1 m) t: @( i    Memo1->Lines->Add("");. p5 u8 o) @( R( m% k' o2 j
}
5 c7 v3 l% Y) y" E$ t
, r% c1 C, r0 t7 d--------------------------------------------------------------------------------2 x; j0 F" U# m6 X3 b

: r  d4 A4 I. r. T0 }  KWMI 可以访问的信息类型有:
! x- c" p% \- v+ ^9 d   Win32_1394Controller
- N  D6 g* S8 t$ X, \. V   Win32_BaseBoard6 S$ o" l, ]# l/ Y# r
  Win32_Battery
9 N- z3 _9 Q0 L9 d   Win32_BIOS% @& `8 E8 A3 L! x2 F# y0 O! l( [
  Win32_Bus
  r. z- `$ o( g/ n! z9 G   Win32_CacheMemory
$ f+ H) X8 k( T, e   Win32_CDROMDrive8 ~, d+ W7 \3 @4 b7 y0 K
  Win32_CurrentProbe, q8 V7 Y8 F7 c+ k3 R6 M0 h" F
  Win32_DesktopMonitor
8 Z6 {5 F- X6 r& V, x( o  a   Win32_DeviceMemoryAddress7 O6 o! _$ L4 O" y/ }8 k+ B
  Win32_DiskDrive1 W( q, }  _3 u; O, P& |
  Win32_DisplayConfiguration7 p' F1 |, ^7 v+ `6 F) u- P0 L% o
  Win32_DisplayControllerConfiguration
  ]8 j% b( i- y   Win32_DMAChannel' u4 V- a  t! O5 }* D
  Win32_Fan& f! d+ z; [0 x7 V! Y. I2 Z
  Win32_FloppyController/ F( i. R4 s' Y2 `  c+ f1 Q) {3 ]* F
  Win32_FloppyDrive
( @: C3 Y$ L6 l2 g3 g9 |   Win32_HeatPipe. ]' v$ V; u5 q8 b
  Win32_IDEController* k- }- z, n# I9 y, P$ G- S+ _
  Win32_InfraredDevice/ |0 Q% v4 I: |, d; |. C
  Win32_IRQResource$ D9 ^9 N7 T/ a8 H! k8 W
  Win32_Keyboard+ L# n1 |/ v# K
  Win32_MemoryArray
" Y+ I$ ]4 O, h( D   Win32_MemoryDevice4 R8 f: L  N* _# a" G3 }& n5 @
  Win32_MotherboardDevice; L8 o% \) F) H! h% |3 f6 @
  Win32_NetworkAdapter" |3 b) w8 j: o, V1 [
  Win32_NetworkAdapterConfiguration
  z: ]5 z9 m# J* N/ Q* A   Win32_OnBoardDevice
" o! w/ T8 k7 P# _   Win32_ParallelPort5 C2 @7 P, `6 W  b
  Win32_PCMCIAController& l! q5 m  }) Y! L
  Win32_PhysicalMemory
! k8 t) x; G3 P& r( w   Win32_PhysicalMemoryArray
! e9 J( o# i- e6 {. U   Win32_PnPEntity
5 W7 S5 c$ j! b" {2 N2 b   Win32_PointingDevice# l1 i5 z2 ?3 C, P
  Win32_PortableBattery
. x# ~$ v- Z3 h8 Y# R4 @. ^   Win32_PortConnector
  l4 g& {0 X7 v   Win32_PortResource1 \+ x3 k- q0 T/ d1 E4 l1 Q; d: q; P
  Win32_POTSModem$ k4 s/ H6 {; g1 ^! `3 y6 C
  Win32_PowerManagementEvent  y6 q* o# c* @
  Win32_Printer
4 G7 j1 D8 z# E# c1 ]4 E   Win32_PrinterConfiguration7 f: Z( V( x, x
  Win32_PrintJob
' F' H) Y( ~/ t: x& ~$ u   Win32_Processor, S0 ?5 n; W) i; |3 u
  Win32_Refrigeration
4 W, T6 g: x2 V1 ~   Win32_SerialPort$ z. L  h* V9 M: D; y7 v0 W* [4 R
  Win32_SerialPortConfiguration; s: o8 [1 u# d
  Win32_SMBIOSMemory7 i0 a$ f: R4 s4 ]
  Win32_SoundDevice
- e4 `: }/ I, t5 C& w4 Z, e& u   Win32_SystemEnclosure
/ v' \- W5 V, H. l9 }6 T! ~: f4 [# T   Win32_SystemMemoryResource
7 o: i$ ~1 n  S- y- Q5 p   Win32_SystemSlot) y  c: Q& y! D+ @+ l4 T
  Win32_TapeDrive1 {5 c8 ^# P" N2 a
  Win32_TemperatureProbe
0 l! a2 a5 a  t: |8 a   Win32_UninterruptiblePowerSupply
: S5 A8 }, g$ G3 t! Q' k8 Z   Win32_USBController/ t( w3 f9 e* x: U$ I  u7 T  M
  Win32_VideoConfiguration* U$ Z5 H# k) L! Y7 ^" c
  Win32_VideoController
  Y' S( }. S  H( M; d& C; t: \" Y! H3 W   Win32_VoltageProbe0 o" c5 S4 J  F8 G
) G9 l8 Y1 S6 i7 L1 E: k
以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-11-20 21:18 , Processed in 0.021271 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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