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

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者); A3 U; c4 b, q$ A! s. t1 h$ ~/ N

2 [- c4 s6 p% Q" \; |- e' o; y! S& x# Z
--------------------------------------------------------------------------------) Z4 V/ M1 D) Q  E) }) o* V
WMI: Windows Management Instrumentation (Windows 管理工具). M. n4 y2 B6 @# M( [6 l: n) c
  通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。
$ A  M5 q" X+ H  `   利用这个工具可以管理本地或客户端系统中几乎所有的信息。# R# V% T/ D  F
  很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。 : F0 @/ k  L( P' a
8 v( m: L; b1 P+ M# m$ e9 |
--------------------------------------------------------------------------------
9 i! P! o) L  J" _BCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面
- K' ~+ l! ^- d4 }2 Y" `0 \' x0 C. f
! v7 U9 Z4 V, |7 Z7 p--------------------------------------------------------------------------------
) V. {3 A6 R% ^: p3 w3 D% r① 初始化 COM 接口:
+ }' r: d/ V' T: e) t   访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。- T' e" T$ }/ n8 _7 l
  这两个函数在 #include <comdef.h> 里面定义。
# r6 }: k$ E, i; F9 U( q# i8 I7 g
' I$ C: _) P1 f+ j+ M3 G. N② 获取访问 WMI 权限:
2 g7 O: U$ ?& \3 _& N0 M   CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);! {  U$ g, Y) a& z
  如果这个函数返回 S_OK 获取权限成功, 否则为失败。( c. n' Z+ _8 d8 q

; x% [4 l$ k# E" a" P" v( r③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:
( t* }- }! {1 J' J: ^' u1 M1 `1 Y   这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。
: T& E5 a( @( D+ \7 o* v4 t
. ]: Z! ^; K( T# K4 yvoid GetWmiInfo(TStrings *lpList, WideString wsClass)( g& d9 Y/ x+ L4 w9 ]; N
{
. t# t( o- U" X# O& `# J  IWbemLocator *pWbemLocator = NULL;! _1 |5 ^$ x: ?/ s& k% T# Q# D
  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)* M# c" a+ Q) I& \
  {5 ^& x& G  O* K0 B! }
    IWbemServices *pWbemServices = NULL;  G) A1 f! w; B( X6 E; p5 j
    WideString wsNamespace = (L"root\\cimv2");
" N) c% @: k& L2 j6 h1 }( T  w; E0 |     if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)
8 |! d% S. o: I1 y      {) x9 _2 Y2 E' G0 H8 l8 D% N
       IEnumWbemClassObject *pEnumClassObject = NULL;# D5 |( c0 |7 \( Y) Y
       WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;' Z: O$ g2 n/ }3 n0 J
       if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)! y# v7 u9 c/ e- P" ^7 g( u
        {2 j& `6 n' h7 X  ^/ L5 j
          IWbemClassObject *pClassObject = NULL;
, ?0 R. \$ F1 ~/ U, C0 C           ULONG uCount = 1, uReturned;
  t" l  y1 _% M  v1 K6 A' l; d           if(pEnumClassObject->Reset() == S_OK). e3 Z0 @% ?+ L
           {) D' ^4 I" r0 ]1 Z$ _
             int iEnumIdx = 0;
* C/ u, H6 r: G. w              while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)
% c" v* W3 M! Y! X! Y' O- l% r( o               {
, t7 ?* g5 c, Y                 lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");
% k% g' w; s8 Y( G! g' s( H
" L( L' d! f. P5 X6 @' P4 l, q; n                 SAFEARRAY *pvNames = NULL;
! R0 L" |$ K( ]4 o* o& ^! [                 if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)% m* t' L( W, R) `3 p. c& M% x
                 {2 c4 S) |/ P3 o# u- d
                   long vbl, vbu;4 ?9 O! z0 t. U7 |' A1 n
                   SafeArrayGetLBound(pvNames, 1, &vbl);' w6 N7 t. E( f4 {. P* \; V. t
                   SafeArrayGetUBound(pvNames, 1, &vbu);
; p; L; {* j  n/ Z/ f0 L                    for(long idx=vbl; idx<=vbu; idx++)
6 @$ z0 T. X& _# T5 w# y, a. V2 D                     {
; W# J1 [1 b: I: p0 \. V  M3 S/ p                       long aidx = idx;" q) Z3 ^/ v1 V
                      wchar_t *wsName = 0;, x: \# c! c4 L
                      VARIANT vValue;6 Y% K$ R: V, b
                      VariantInit(&vValue);8 j* g1 y* E/ ?( L$ J
                      SafeArrayGetElement(pvNames, &aidx, &wsName);& R0 r; h+ }$ o9 N+ {; i
8 }$ @* q  i$ k- z
                      BSTR bs = SysAllocString(wsName);1 y* w- Z. |$ U$ d( N
                      HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);
5 I/ {* O; e, w  I                       SysFreeString(bs);1 M( A3 _) R& F$ |. D; r- J, [

9 l( P7 r. w/ U2 b5 H                       if(hRes == S_OK)- D/ o0 Z6 G) [: D, P# @
                       {# p# x- h% R! [, l
                         AnsiString s;
) i+ ]) Z' ^7 P7 J# P8 f                          Variant v = *(Variant*)&vValue;
7 |; t& D( S6 O+ g                          if(v.IsArray())6 p, {$ W( N9 \) N
                          {! d" B5 x' I# A0 s2 x4 f7 x. z
                            for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)! D$ D( X; o; S
                             {/ u4 k% T; b' ?& p5 A1 e
                               Variant a = v.GetElement(i);
( c& ]0 f/ W& U! m0 c                                if(!s.IsEmpty())
; D+ J4 ]/ P( ~  |' n+ R                                  s+=", ";2 }1 Y7 B2 |6 ^8 Z5 {; V+ {/ r
                               s+=VarToStr(a);
* |4 A+ O; n  w2 W                              }
( M" O5 h, O! f# \3 ~+ d/ o0 [; E$ L                           }+ g, M1 Y, P9 c1 @! v- N7 u$ M* G
                         else
, i8 Y* o# }3 ?3 f7 w, C                           {: ^: e. D' V+ b& M, ?
                            s = VarToStr(v);
1 g% v, T  g% o                           }
2 O4 Q  A% K9 @" m1 s                          lpList->Add(AnsiString(wsName)+"="+s);
9 [, ^  b) U4 M) J/ ^5 c# H' J8 G                        }
1 h) s- l. }. f, `4 y1 K: |& V8 I* Y1 u4 C: r9 T9 H
                      VariantClear(&vValue);5 ~. F5 j5 L0 G4 X7 C
                      SysFreeString(wsName);* x& W7 Q( y1 i' E
                    }( f1 q& A8 F, D3 o! a! O
                 }/ e' W' u5 j% m" \+ k' H5 Y8 C
                if(pvNames)SafeArrayDestroy(pvNames);
" i+ y$ \) c6 u0 C                 iEnumIdx++;$ O7 w! |; K/ u( G: U
              }' {( O4 k  r0 a( x
           }
9 T2 v! b' I$ n8 M) n6 S- d           if(pClassObject)pClassObject->Release();
# U4 n+ b  W2 E5 O  S) C8 J9 W         }
5 w/ s, C! P! ?' ]! t        if(pEnumClassObject)pEnumClassObject->Release();
+ I* d5 A! X8 g8 p  B- C$ H      }! `9 K2 D$ a8 L/ H
    if(pWbemServices)pWbemServices->Release();
3 R1 O/ _+ `- x: b; C1 [% E/ A   }% u! }0 S6 }6 B' D$ N+ z, q% g
  if(pWbemLocator)pWbemLocator->Release();2 d* B/ a0 O. b' j# Z
}2 h4 ~- J' L% P2 x
//---------------------------------------------------------------------------) V$ l4 y" c6 U+ ~% _5 \0 `( P
/ t. X; N/ V7 \1 }# Z
// 通过 WIN32_bios 获取 BIOS 信息:$ c2 [: `. B- J- R- n9 f
void __fastcall TForm1::Button1Click(TObject *Sender)
* a6 [2 U. v& w{! ~" {/ ]2 C; Q; o9 O/ L; U
   Memo1->Lines->Add("================== [WIN32_bios] =================");9 i! v$ f3 I5 w0 n  m, ]$ i0 N$ ]. G9 w
   GetWmiInfo(Memo1->Lines, "WIN32_bios");5 E/ }4 i* C+ @# h
   Memo1->Lines->Add("");7 V4 M8 E2 a+ u
}
, v  _! g, M& V' V- j5 [
6 z: j3 C: }  X: J: I--------------------------------------------------------------------------------
0 G1 p7 g. @' ?/ P
  f% T6 C6 k0 x* }6 R' c; c- g" jWMI 可以访问的信息类型有:
5 v0 ?# }* b- J4 [" \/ d" T- k! e, B   Win32_1394Controller
$ `8 v+ I0 p+ z: r   Win32_BaseBoard
$ O! e, s3 U: [; j9 U$ {! D   Win32_Battery
  U" X. r/ `8 n# j3 G# s   Win32_BIOS  X. }/ Z7 }& @3 C- R
  Win32_Bus
' C( q  Y6 Q5 n+ O9 g5 V% z   Win32_CacheMemory
- s( y9 L. i1 e& P, b   Win32_CDROMDrive! }, }6 x# A/ Q* `: e# g, J% h
  Win32_CurrentProbe
) C: c/ k  s/ ~4 x   Win32_DesktopMonitor
4 h' j( y8 |8 C/ H2 M   Win32_DeviceMemoryAddress1 _1 c; P5 K8 J3 G9 ~
  Win32_DiskDrive0 f- J* l9 R2 D
  Win32_DisplayConfiguration
- E2 @& g1 V% ^6 [2 v- `   Win32_DisplayControllerConfiguration1 J8 n; g& s0 M
  Win32_DMAChannel. v9 T- @/ F+ V% W: C
  Win32_Fan
# g' D1 i% Q! N1 r* a9 C   Win32_FloppyController% [+ `) u% s8 ^% q/ }$ g
  Win32_FloppyDrive2 U2 v" D$ s3 L3 W* q4 U4 A9 |/ I: l
  Win32_HeatPipe
+ [3 T6 P+ j' s& J# y) {2 n. T   Win32_IDEController
9 [! z( V; F! O- t; B; ^   Win32_InfraredDevice
: r% A5 ^8 D+ |4 o   Win32_IRQResource
( l0 j. A1 l( g  x/ O8 p+ ]! @: R   Win32_Keyboard" \+ V& t8 l" j
  Win32_MemoryArray
0 \$ `7 w  H% q/ C   Win32_MemoryDevice, K) I2 E3 ]! d- `/ ?( F
  Win32_MotherboardDevice
) v- t/ X" [% |# t- U   Win32_NetworkAdapter4 H: S  v9 K' y2 {
  Win32_NetworkAdapterConfiguration& g* v* F4 l: u/ L1 h, U
  Win32_OnBoardDevice
9 r" w% j2 @6 i, P; A+ `   Win32_ParallelPort
- `$ O( @6 E4 \& [* i# `, d   Win32_PCMCIAController
) r: r( j8 t; s" }6 u, m( l+ U+ W   Win32_PhysicalMemory
. \, S2 ~* _1 |, I- K# Q   Win32_PhysicalMemoryArray& W& P' \6 C. ^6 B: t7 {) `, T
  Win32_PnPEntity) [' I; ]6 Q4 |4 N8 Q- i3 q
  Win32_PointingDevice7 v& [  n- d6 q" r! G, q
  Win32_PortableBattery
# I5 ?- b, R' t( ?   Win32_PortConnector0 |$ M0 ?( c6 U9 p5 l6 @
  Win32_PortResource
: c8 W9 J- Y8 y9 v5 L- P  F% y   Win32_POTSModem
9 I  g; {! X4 z# ]: l   Win32_PowerManagementEvent
7 g8 ]  d6 ^5 @   Win32_Printer# I* o" V, S" [6 @7 K% B: K
  Win32_PrinterConfiguration1 x8 a3 u0 E$ _* @+ t$ r, R
  Win32_PrintJob
6 W( t; V" `5 n1 a& |# D   Win32_Processor) P2 J. s- J0 K  h  ^
  Win32_Refrigeration
: u* y# r- Y8 C3 V/ e( F3 U: a  B   Win32_SerialPort4 ^' y; y( f  X' U
  Win32_SerialPortConfiguration/ v& @$ n" p; t) @* o- O  R
  Win32_SMBIOSMemory+ n' s4 v) h. Y+ E2 C! W( K
  Win32_SoundDevice
4 ]. Z( l3 u# I$ @' M1 {   Win32_SystemEnclosure) U/ U+ e& S5 F
  Win32_SystemMemoryResource
; ^; T' l8 ]5 D% U' b   Win32_SystemSlot
& x' o- M7 Q( V0 {   Win32_TapeDrive! T( Z0 N/ ?6 g; g6 C8 c, Y+ v
  Win32_TemperatureProbe2 v4 }. ?) {. w" s* d7 T
  Win32_UninterruptiblePowerSupply
4 u8 G. C3 I& M$ z   Win32_USBController
9 q1 E3 _& ~6 W- L# N# Y   Win32_VideoConfiguration. V' n. d: h( p5 V" s
  Win32_VideoController
* i2 Y. C- l' S: s% i   Win32_VoltageProbe; W/ ~0 L. ~9 m/ H6 d( c/ S: r
4 F# ?' o, \- v$ R0 s4 B( B
以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-1-3 08:42 , Processed in 0.019302 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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