找回密码
 注册
查看: 13283|回复: 0

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)
" d5 y2 p' [7 q* t7 P! h8 E2 A( ]$ |2 h5 K, P
) m( w7 |" @4 v; N/ ]4 [
--------------------------------------------------------------------------------: s( c4 k7 |* _8 W8 b. ?
WMI: Windows Management Instrumentation (Windows 管理工具)( }2 Z, t+ @0 ^: O9 S) [; Z8 X
  通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。 ; v; i1 b% W5 _! X% f- \$ _8 `
  利用这个工具可以管理本地或客户端系统中几乎所有的信息。
) {3 Q9 L/ q) e, B- g   很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。 8 D  g" g% @5 C% j; Z6 T. g
$ K1 ^9 W5 m$ o+ r# u0 j
--------------------------------------------------------------------------------
6 @& x/ w( W+ m5 r! G3 O: nBCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面
3 ?: e) o2 H, q) b* M- O7 G2 s: ^0 n% ?9 u0 e1 A# w) g
--------------------------------------------------------------------------------
! V1 @/ x6 D4 C6 p% U' e① 初始化 COM 接口:
: P8 C* }1 V- E& Y9 }) o+ C# X+ R   访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。
3 D7 _5 Y' N5 T6 ~3 J( C   这两个函数在 #include <comdef.h> 里面定义。/ d( g) f/ c* i; N/ L$ p

& e/ S0 `( ]+ T& f) S$ U7 R" E; {② 获取访问 WMI 权限:/ u9 T" F! o7 N9 a
  CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);
) `6 f; \( k$ I* ?   如果这个函数返回 S_OK 获取权限成功, 否则为失败。
) p/ |8 ~* N  \! B  y
: X" z/ ?% w- x/ ^! d% N③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:, L9 P6 w; l( N5 o6 ]
  这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。5 }; @+ a2 x8 Z  c) D' P
8 v8 Y5 p5 l3 A
void GetWmiInfo(TStrings *lpList, WideString wsClass)
* v4 L4 c* z$ h+ o{
+ I$ h7 N% d' \$ T; ]  s; x  IWbemLocator *pWbemLocator = NULL;
. O. G" J7 N0 j% R  K4 w' x  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)3 ^! d( N2 C( `7 O- R
  {, [* e0 X( l$ A; c
    IWbemServices *pWbemServices = NULL;! e+ J$ T2 v2 s9 k
    WideString wsNamespace = (L"root\\cimv2");7 F1 ]* d  Y0 k
    if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)" o2 A0 B' ]: M( H2 O
     {
3 u: m7 I; F; Q- T$ h        IEnumWbemClassObject *pEnumClassObject = NULL;0 E7 y! n9 g. \, H* b: @1 ?
       WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;
' n& h/ H; k" Z1 m/ @* O  w1 x( f* R        if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)
& G( l2 ]! h! g8 h         {2 X% x5 y) j6 T$ M& Z/ x: U
          IWbemClassObject *pClassObject = NULL;
+ U* U# N  u1 ^' i- {           ULONG uCount = 1, uReturned;4 x1 B4 C* Q: K6 E% V, S$ b1 O
          if(pEnumClassObject->Reset() == S_OK)
- g- u! w. ]2 m% h( q            {: ]4 E  H% U9 P5 m' Y  q
             int iEnumIdx = 0;
- \6 D% L& [, _# f( |5 k              while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)* ~5 R7 d- {+ T2 C8 S( p7 G
              {) i, h- j" n5 Q7 V3 W6 k
                lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");5 z: ^% K, X  M

% ]  O) ^0 t/ t1 \2 _1 d8 X                 SAFEARRAY *pvNames = NULL;0 E5 j& c( N8 y7 e! ]
                if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)
1 F6 [5 L( N+ y6 G8 [% A7 [: s                  {
/ M' r; E1 q$ B; I7 b0 I                    long vbl, vbu;
2 Y! l, m! @: o" L                    SafeArrayGetLBound(pvNames, 1, &vbl);
3 K- T  t9 j9 r                    SafeArrayGetUBound(pvNames, 1, &vbu);
7 V. O# Q- o% T- j9 ]% b/ }                    for(long idx=vbl; idx<=vbu; idx++)
' I/ T8 x0 `: K                     {
# P* O; }5 b* U2 A5 O* h                       long aidx = idx;
+ k; i/ [+ M: s7 k                       wchar_t *wsName = 0;, `1 C3 D, ?! R6 v: v7 Z
                      VARIANT vValue;  W  y7 K; A- Y" G* I
                      VariantInit(&vValue);
. V9 }1 ~6 H5 X0 v6 L! S( v  p% V                       SafeArrayGetElement(pvNames, &aidx, &wsName);
3 T4 L- F% c* f$ G" {
$ A3 ~& s- L; b) A5 u: t                       BSTR bs = SysAllocString(wsName);0 W, ?) y5 `. a$ x  |2 {
                      HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);7 U3 j1 @( D: t/ t0 p) d: A
                      SysFreeString(bs);* y3 L3 P) L. j$ z3 r- {1 l% S
4 \, ^, l7 R/ G' ~* X
                      if(hRes == S_OK)' x  m, s0 s* {: d: B
                       {
$ t: g3 y' c4 g8 p2 t                          AnsiString s;. h% C: C8 `  p0 B
                         Variant v = *(Variant*)&vValue;
. e" a' X6 K' ?+ H                          if(v.IsArray())
; A4 @# m# V( z8 h$ j                           {
% o+ _# u9 x0 s  Q: Y                             for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)
0 t4 a0 j# m1 E! T9 S* Y5 y1 e* d                              {: }! f( e$ p' |3 V& {
                               Variant a = v.GetElement(i);  H9 P6 ]) l& k+ A4 c# y
                               if(!s.IsEmpty()); L' x% p6 m/ C1 s7 U
                                 s+=", ";8 ^( ~/ e( w% x( F; `, G* q. w  _8 d
                               s+=VarToStr(a);
# y- P; Z: u+ l$ N0 B2 B                              }
8 J  ^; L: U4 R, P$ m                           }5 t' r6 v3 d6 g; N+ V9 W
                         else
+ G" P- s! }8 `$ I$ f% n7 W3 U                           {# ]) [( {/ `& L. b( B
                            s = VarToStr(v);
, J+ ?4 C% q4 G7 A# ]% g                           }
+ |: V, Q0 Q' `+ h                          lpList->Add(AnsiString(wsName)+"="+s);& Z7 u, _& R% w+ N; w% Q2 Z# K0 k; Z
                       }
+ L5 e, {, ^( G, Z8 x; T; I
: F$ y% e) v. T" ~! ?                       VariantClear(&vValue);3 q" V& Q9 d: C/ }! N# t. {: }, M
                      SysFreeString(wsName);
8 W8 r# t6 U: B- K* \+ Q                     }+ ~8 q5 Q6 k1 y& D" }% `9 l( B$ U8 A8 Y
                 }& [6 I& S% g" R6 m+ P
                if(pvNames)SafeArrayDestroy(pvNames);
* K9 G8 P: O+ P5 N4 c1 A; @                 iEnumIdx++;9 n1 P1 J3 p7 J' @
              }
3 e* O" l1 q6 ]& j$ C            }2 T$ X. G$ W4 ?
          if(pClassObject)pClassObject->Release();
; N  m6 X, \4 [6 ?1 Z         }& o# ~; o3 P' V- Z+ b# e
       if(pEnumClassObject)pEnumClassObject->Release();
( y. V8 p4 J2 }6 u      }
4 J3 T  B$ [5 |7 V1 m2 Y     if(pWbemServices)pWbemServices->Release();3 b# i( Z3 ~, I! ~2 \! Z
  }. {8 D* a6 ]; E- O- ?; A
  if(pWbemLocator)pWbemLocator->Release();
- X1 ?1 Y8 w* B3 V" D$ m}
$ _* n; H0 H& H& [//---------------------------------------------------------------------------
$ _9 S6 d) `3 O
- g) x! V8 @0 |3 a: f/ m, a. b/ n// 通过 WIN32_bios 获取 BIOS 信息:
* R$ w4 j: ?& q  L+ t, a. Gvoid __fastcall TForm1::Button1Click(TObject *Sender)3 u  A5 S9 T4 `* N) S9 c3 v
{8 L* y# Q5 K5 s9 ?7 a
   Memo1->Lines->Add("================== [WIN32_bios] =================");
7 c, g5 s2 U  ~1 a  K6 H    GetWmiInfo(Memo1->Lines, "WIN32_bios");* A6 W4 c& R! s6 @; T7 v
   Memo1->Lines->Add("");0 ]8 J/ C; _2 B' t/ |
}. H' a) J! q# C* c
  C$ U1 `5 {: d# w
--------------------------------------------------------------------------------
( n) ]) \8 Z, q# g0 \
; W% z; U( Z' A2 {$ XWMI 可以访问的信息类型有:- H: x  j+ v% p  }7 W
  Win32_1394Controller$ H  L, b. m8 Z! m) x
  Win32_BaseBoard
* L4 ]! p2 x  t   Win32_Battery
2 y( d* h5 v2 y  B3 D' L. J" c' `   Win32_BIOS/ ]0 s4 {5 s, I7 Q$ F
  Win32_Bus
- }; I: O$ H9 d" b9 k3 f   Win32_CacheMemory3 O. x( D: O3 n' O/ h
  Win32_CDROMDrive$ y# n4 J- @+ F3 @; E$ f: i$ T
  Win32_CurrentProbe
+ C) e" K. H% x' [! @' O   Win32_DesktopMonitor, q  c1 B& k* B/ R  J9 k: x
  Win32_DeviceMemoryAddress
( s" n5 ?. b( W; G2 z" {% ?5 W   Win32_DiskDrive9 Q& c( J8 V) T
  Win32_DisplayConfiguration/ E7 U" d7 y/ a6 w/ C
  Win32_DisplayControllerConfiguration
+ U  g5 c2 C7 j   Win32_DMAChannel
* ~/ m' _3 F5 g+ N- E$ _& q   Win32_Fan
9 P( _4 _4 r7 q, w7 i   Win32_FloppyController6 o' A3 g, h! ^) b: g& [
  Win32_FloppyDrive/ F% f* I/ i8 Y) u9 ^
  Win32_HeatPipe: \3 V5 r! e- _( p* Q1 t
  Win32_IDEController* y5 ^# w  h' q0 s9 E& P9 `; i$ ~
  Win32_InfraredDevice0 O6 D: D) j0 n  _# J; T8 @. f
  Win32_IRQResource
& F* D1 _6 l" M# l: k! @3 |" v   Win32_Keyboard2 C. a2 A' P7 P6 W( W- r9 L; Z
  Win32_MemoryArray* q4 @/ c0 E) w$ `
  Win32_MemoryDevice
; n0 h- d" X) q! m8 Q- a2 T   Win32_MotherboardDevice; m8 `9 h" n: |
  Win32_NetworkAdapter2 O; ]' M: D( x% K0 ?+ p
  Win32_NetworkAdapterConfiguration' o# v% ]6 e" A
  Win32_OnBoardDevice
9 ~# h% j# Z/ w: J0 v   Win32_ParallelPort
% O9 o' z; L" ^7 L3 t+ b0 y) q   Win32_PCMCIAController
  D( x- `! o( A) ~# v! l% J   Win32_PhysicalMemory
& c0 ~" `% P7 C" i) C# R   Win32_PhysicalMemoryArray
2 h+ }8 S+ z8 O9 s0 i   Win32_PnPEntity
% e' W* J& r; H. l   Win32_PointingDevice
8 R3 i3 h& b. }8 j6 a   Win32_PortableBattery& [- o) I5 r5 @
  Win32_PortConnector
) w; B5 r; |. T4 {, b: {   Win32_PortResource4 O- r) V* |7 A+ y$ T; z7 i
  Win32_POTSModem
) ]2 w; `# R4 ~) n0 E1 b# i+ X   Win32_PowerManagementEvent* B9 Z) B) I. ?+ j
  Win32_Printer
- o, j( t& _/ s* r/ D6 |   Win32_PrinterConfiguration& I7 b5 y# ^. x1 |
  Win32_PrintJob
5 X& S$ N% U: r! p8 P" |   Win32_Processor
% a, D4 B) ]! M7 x5 t8 P! l   Win32_Refrigeration
: U) B$ j: R/ f$ B0 P1 T8 u   Win32_SerialPort0 G; r0 z0 Q& n0 v) s9 G( I
  Win32_SerialPortConfiguration
7 ^( Y3 N2 f% W   Win32_SMBIOSMemory$ o: E6 V, _4 b# G1 W* x5 c
  Win32_SoundDevice* M( R8 E' |0 U4 S+ Y$ q: i3 j3 w
  Win32_SystemEnclosure6 `7 H6 l" y. {3 d% ~0 e" N
  Win32_SystemMemoryResource4 s; X; I7 a# J
  Win32_SystemSlot3 |% e7 ]/ H5 y
  Win32_TapeDrive
0 G. e  ]* g* x9 y9 a   Win32_TemperatureProbe
5 K  ]8 b) |2 h: m   Win32_UninterruptiblePowerSupply$ c  ^' p. c' x( s! @! O8 p; f5 |
  Win32_USBController0 E3 E, k! i9 {
  Win32_VideoConfiguration6 K' `! N; Z: d" \
  Win32_VideoController
, D' E: B5 \. v   Win32_VoltageProbe
' ~; v) g3 _, d9 `& L& K* F! F2 @) G1 G) E0 f5 ^3 \8 q/ r, N1 ?
以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-10-1 20:48 , Processed in 0.019002 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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