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

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)2 ?( n" C$ L5 I: b$ q+ g: s( a

9 S( t. w. s/ @) G/ c# C3 y% c- M* @
4 g: ^6 h& p& x$ o# f: t* a--------------------------------------------------------------------------------
2 G" @& }8 r+ ?6 tWMI: Windows Management Instrumentation (Windows 管理工具)
+ n0 D9 ^( t7 r% _# |   通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。 3 s4 K8 @  V( Q
  利用这个工具可以管理本地或客户端系统中几乎所有的信息。$ h3 D& {( W) {% U
  很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。
2 q1 n1 e) J' ^7 e2 F7 G/ M
* K$ }. p5 w( ~& w--------------------------------------------------------------------------------
1 s7 {8 U2 z! S! f: vBCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面
9 ^. q9 Q' [3 X* y( [% t7 ]/ G
, Y9 Q0 o# g$ O  }" G--------------------------------------------------------------------------------  C# Z: e3 h$ U$ P0 B3 y5 U$ _
① 初始化 COM 接口:$ _+ F; i8 H. J
  访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。" Q  }8 E+ o' @2 s8 R- n8 C  N) F
  这两个函数在 #include <comdef.h> 里面定义。( F5 W; I* Y# W1 ?4 O* v

* f7 F& F) d8 Y8 F② 获取访问 WMI 权限:
# B$ T8 B% b0 a1 |) O   CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);1 u; e- X, o; N5 P$ K$ q! Z' y
  如果这个函数返回 S_OK 获取权限成功, 否则为失败。
0 D+ ~; B; }2 x+ M- Q
, n4 s# k7 m# h; x# A! g③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:& A1 ^: @4 u5 E
  这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。- O* }( K: N1 G6 Z, f9 {
9 \( i# Y4 `8 `( f/ N
void GetWmiInfo(TStrings *lpList, WideString wsClass)+ X7 O* k& F& ?
{
5 P& G, D, |* V) y; C  IWbemLocator *pWbemLocator = NULL;
* c3 J6 ]' O" }" Q  O  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)
/ T# ?; x, G5 A- ]3 u1 J7 P. D; ]& ]9 d   {
  b. B6 X# V3 F7 w% \     IWbemServices *pWbemServices = NULL;
+ z. Y/ y' K- P0 p/ u/ ^3 t     WideString wsNamespace = (L"root\\cimv2");# D  E# U* }& g: G. H: F
    if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)
. w  J- d; i/ U1 g      {% j( {$ y6 a" V. Y) J8 p0 z
       IEnumWbemClassObject *pEnumClassObject = NULL;
; p) m6 u: S7 D! ~7 V        WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;
' `' t: v. Q: |$ B- p        if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)
8 p2 D& z/ K3 ?) k4 w5 f         {
9 ^8 Y4 L8 k6 s           IWbemClassObject *pClassObject = NULL;, K( V- v. x$ W: J) X7 I  {
          ULONG uCount = 1, uReturned;% V' ~5 s' U# I+ Q! Z$ Y
          if(pEnumClassObject->Reset() == S_OK)( t' M! B- M9 i' P
           {
2 p8 Q6 A, \1 @1 M8 b9 W              int iEnumIdx = 0;
1 R- {! C! e# }, m, A              while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)& v( D1 G) f" f: ^" x: u! B8 W
              {
) y, \3 v6 G# X9 h: ?0 c                 lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");  u1 C- e- a" I4 \

. f6 n  k: S3 w1 G' Q- b9 y                 SAFEARRAY *pvNames = NULL;
( j! T6 x( k" q% r: W                 if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)
0 D; d" ^3 d' v3 B, G9 q                  {) B7 e/ s7 h' F4 U" m
                   long vbl, vbu;
9 @0 Q; W8 T3 ], D' ~                    SafeArrayGetLBound(pvNames, 1, &vbl);
$ Z/ d5 J3 Z1 h$ c" `                    SafeArrayGetUBound(pvNames, 1, &vbu);- d% ?" x& h1 ~! r( e! l1 r4 i
                   for(long idx=vbl; idx<=vbu; idx++)1 f  ^4 Q, t4 I$ b  j/ L
                    {
3 Y4 J( Z1 [* `2 h! W) M" H7 l- K                       long aidx = idx;
; Y9 w# b. a4 E0 p                       wchar_t *wsName = 0;
* j, k6 v& I! r; g                       VARIANT vValue;
( t/ f( a( {- j3 M% [! M- k                       VariantInit(&vValue);: _, d- s7 }& a2 X- V2 l
                      SafeArrayGetElement(pvNames, &aidx, &wsName);. U  Y' F: Z2 D

) L" w! F' [9 b* ]8 D* h4 X: E                       BSTR bs = SysAllocString(wsName);
3 \) w8 ~5 T' ~( [) H  U" j                       HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);
& k0 J; n& K) z* [$ Q) F                       SysFreeString(bs);! \% r3 ^8 v( d
) X/ ?2 t/ f3 ]) I6 l9 J( Z
                      if(hRes == S_OK), i2 Z- u2 q0 c5 e& X1 G, g
                       {! M" |  m: p. a; O: F
                         AnsiString s;  \  n6 ]# u/ I) }. w
                         Variant v = *(Variant*)&vValue;! \& J( p4 r! n5 p/ q
                         if(v.IsArray())6 Y0 O* t' }0 L: m
                          {
: X  c* d5 ~5 I                             for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)
  u2 ]$ f& K* B- u% F                              {9 u# K8 V8 S) P# t/ g
                               Variant a = v.GetElement(i);
3 k- L. c) F2 ~! B0 ^                                if(!s.IsEmpty()), l8 b4 C0 C! Q5 `3 r; f
                                 s+=", ";
; k& c# U. F0 V                                s+=VarToStr(a);
- @# \. ^* n: g/ t. n                              }7 E* O+ \! _' s0 a, l5 N, X
                          }
7 _5 ?/ V% p* ^8 W3 r                          else% c  G% ^( F( A6 Z( ?+ B% D" \
                          {
6 C. L! J1 ?0 e) l& {                             s = VarToStr(v);
- O$ U; [% k- N( W9 d9 L" a1 L9 s) p                           }/ B; R9 f$ I( \. j5 J
                         lpList->Add(AnsiString(wsName)+"="+s);
* v5 B9 S; \9 Q7 h1 _2 N/ U  w                        }
6 J! i- A8 \  _/ J* O6 w! W+ \7 o  c, a3 ~+ T& L! A9 o/ H( s& d
                      VariantClear(&vValue);& z$ ?" w. K5 `0 Z
                      SysFreeString(wsName);6 F* B# g7 Y1 {9 ^
                    }0 _% }( O. d: m( F
                 }
, A  B% A. e$ x5 H. u                 if(pvNames)SafeArrayDestroy(pvNames);
; i! ]3 `7 L1 u8 `                 iEnumIdx++;7 D4 C, B( \: q+ V3 X
              }* p- I5 A9 e" z3 k6 l
           }
0 A6 f2 ~; D$ A+ D4 Z! v           if(pClassObject)pClassObject->Release();
6 c/ ^  `4 U+ V: f, [         }
- A# x* {& z3 [2 [0 i        if(pEnumClassObject)pEnumClassObject->Release();* a! F, |6 {+ w0 x: J
     }
( U! o: ?4 t! f$ h- U     if(pWbemServices)pWbemServices->Release();
  L7 @( ]2 |' C   }
" R7 ^- S6 O; o2 R4 |% P6 n  if(pWbemLocator)pWbemLocator->Release();
) I# f8 J6 z& T0 c& a}
9 G% n# w) R. X* h. {//---------------------------------------------------------------------------5 E3 k2 t/ T  M! w. C3 r

3 z0 p& _6 f+ W5 \+ i9 k// 通过 WIN32_bios 获取 BIOS 信息:* V+ W, E9 }4 ^# V; a
void __fastcall TForm1::Button1Click(TObject *Sender)% b" Z5 V9 m, M2 }* d  k0 f
{0 Z: F% d4 ^8 q. k4 h( }$ V- E
   Memo1->Lines->Add("================== [WIN32_bios] =================");
( H0 q. A0 x  c% I    GetWmiInfo(Memo1->Lines, "WIN32_bios");
( C! }# {" D7 O    Memo1->Lines->Add("");# ]' C/ M% n# R+ b7 L2 J
}
' G" g, J/ k7 t) H  w" j
( j8 \$ V7 G& W4 r) c) t--------------------------------------------------------------------------------
2 b4 w7 N9 U, p1 l$ N7 f# H: Y* q3 W0 L) V
WMI 可以访问的信息类型有:0 v( s; P+ A$ x
  Win32_1394Controller
% C' _  j) Y. ]9 ]( j   Win32_BaseBoard6 v" X/ N8 P9 ]+ S* }/ l  i( P
  Win32_Battery
$ B' m9 r% E9 F" z, a7 Q   Win32_BIOS
1 I8 }1 d, y% @3 E( l% [4 t   Win32_Bus
  o3 ]6 J$ m! U) v   Win32_CacheMemory
* j! o  |- l9 g- I* Z; S, q   Win32_CDROMDrive4 }% [% X9 i+ j" K! R# ~) C
  Win32_CurrentProbe2 F0 x7 I# V: l7 d8 \& ?
  Win32_DesktopMonitor
+ \' \9 j% e% P; b. t+ J   Win32_DeviceMemoryAddress$ m% \6 a, }$ p8 E5 h
  Win32_DiskDrive7 A+ P. d" [  d. \. D
  Win32_DisplayConfiguration
' q$ {7 P0 V6 p2 a- b+ h+ d1 L0 r: @; `   Win32_DisplayControllerConfiguration2 w" W! x7 Z% a% W
  Win32_DMAChannel
5 E  |4 Y( R* t, D5 P   Win32_Fan  \( P# z% R6 j/ S$ \1 F% k' Z9 T
  Win32_FloppyController
( O+ Q; o7 P$ E0 T" N, x. f   Win32_FloppyDrive8 |$ W8 y; t. ?
  Win32_HeatPipe; S) l% C* W$ Y7 q6 m+ J/ J
  Win32_IDEController
- `9 p$ V' E! ^   Win32_InfraredDevice
& I$ \- |, s7 e  X+ A   Win32_IRQResource
6 ~7 J( W* m( K1 |- [) p   Win32_Keyboard- }; j' A5 j3 J$ B
  Win32_MemoryArray
: o7 v8 r( i2 `/ f" s. ?6 Q- C# F   Win32_MemoryDevice( b) T, ]: ~9 }3 |
  Win32_MotherboardDevice! q0 L( \- Z  E5 t. ?, @# ?5 b! L
  Win32_NetworkAdapter; o9 L& s6 b% h' \" c2 t8 z" b7 l
  Win32_NetworkAdapterConfiguration
8 ]# z! O/ g; |  v! M, R   Win32_OnBoardDevice
/ N9 |* u% ?0 K  K7 |$ v/ a8 g; e% \   Win32_ParallelPort
' L/ N# J# ]# ^, v8 g/ J   Win32_PCMCIAController) X" ~* F( N( F& X7 \. X
  Win32_PhysicalMemory
. s4 r! a3 S6 s   Win32_PhysicalMemoryArray* P  a) R5 a! w3 H0 t* d
  Win32_PnPEntity
1 \/ N1 M; @& W6 r' ?   Win32_PointingDevice
  e8 j- n- {3 p3 u6 ^: x   Win32_PortableBattery- F3 n8 a) ?2 Z* }2 A& P! m) B
  Win32_PortConnector& A# X" T7 R% ?1 [
  Win32_PortResource
! \8 E# o6 x2 i   Win32_POTSModem
, ]7 m/ |/ z8 h( H7 i  t, Z! t$ ?   Win32_PowerManagementEvent; A' w! D+ x/ Q
  Win32_Printer
3 U  y" v: H- Z5 k9 e* ]% S- n   Win32_PrinterConfiguration
7 t, F; y! o7 g: |1 [- q   Win32_PrintJob2 z  N' V3 e0 _
  Win32_Processor5 d2 v, n; k5 B+ _$ j1 y$ e
  Win32_Refrigeration
. J8 Z- o/ @! ]! ]( c1 b* ?   Win32_SerialPort
* _) T8 G( s- x! F  x   Win32_SerialPortConfiguration
, |- P( P* l9 v7 g  {+ e, }   Win32_SMBIOSMemory
: f5 d/ X9 Q( x+ R0 W- [6 J7 c   Win32_SoundDevice( D* K. A, W$ f+ A; a4 t+ x  B
  Win32_SystemEnclosure  A# Y0 }8 t" B# r- i3 p
  Win32_SystemMemoryResource
( f8 ~3 o. Q' Z1 y( s2 R   Win32_SystemSlot9 G, S# m* i, \, c6 G
  Win32_TapeDrive
' O/ q+ ~0 i  [) y# P& O   Win32_TemperatureProbe
+ {* L  l8 R' a7 W   Win32_UninterruptiblePowerSupply" d9 \" e, v4 Q/ c. X8 q6 ^# A
  Win32_USBController
  [: n3 h! N; {7 |6 C1 p   Win32_VideoConfiguration, ^; }, k) \- Y/ b! O/ W7 L, U
  Win32_VideoController
! B$ M& p* ]; `, h' @   Win32_VoltageProbe9 n% J; v2 q+ N; C2 I: G% ]! j
  S3 `8 I+ P+ g( n8 d( r2 Z. y
以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-1-26 16:09 , Processed in 0.017256 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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