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

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)
( M; H) W9 J# R( t' q8 f/ O) H( k7 Q# _$ i! ~

) Q; g: @7 }; \' p6 U7 N0 k' d7 {--------------------------------------------------------------------------------
/ V% k1 c0 a. a4 tWMI: Windows Management Instrumentation (Windows 管理工具)4 R& I- Z. c7 O4 s6 c
  通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。 * D& s- I# i  d4 f: u! B; v9 `
  利用这个工具可以管理本地或客户端系统中几乎所有的信息。
8 |& \3 Q8 E6 m" U   很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。 ! d- H+ [/ k: U- |

4 I* V& u1 p3 x9 b9 @--------------------------------------------------------------------------------% W/ c7 R+ w& Z' T
BCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面" f; v, Q: _, D4 C# V) y6 ^
2 O2 f0 _* o5 v  ?1 t( }3 Y3 M+ P
--------------------------------------------------------------------------------
0 }3 t7 X* G/ W. E① 初始化 COM 接口:  K( }& P: |& i
  访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。' @' V7 v' F6 M; v/ E) Z' H
  这两个函数在 #include <comdef.h> 里面定义。
/ P3 J: ^* A; z! Z- r3 F! j9 s, S& m2 P
② 获取访问 WMI 权限:3 e4 t1 V& j1 D/ J% ]5 M* C
  CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);& m  z& [+ Y& L
  如果这个函数返回 S_OK 获取权限成功, 否则为失败。1 H! i4 D" Y* J+ R- B3 l2 c9 F

: I7 N) ?  e2 j$ R2 Q# L; H③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:  c: ?9 m  C3 s1 c' a; I0 s
  这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。+ I8 n- F6 o# E# v# L% f. y
) g3 S4 P+ O! N  i
void GetWmiInfo(TStrings *lpList, WideString wsClass)
* n; s- m" N; e5 L' m5 Z" s$ i{
6 Q3 h& n2 l% v+ E  IWbemLocator *pWbemLocator = NULL;
  p: e" f4 j/ o. O' N; C  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)
0 D6 j5 M: K  `2 V! s- y, M   {
% n) C* I8 S; S0 ?     IWbemServices *pWbemServices = NULL;4 {, Z( J- F/ l2 p  [
    WideString wsNamespace = (L"root\\cimv2");. M6 F  H( E. B3 E7 [% ?9 a
    if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)
. l6 A3 w. I* D1 Q: S) _' _      {
' Y, ^9 W4 x  ~# `  E- Y        IEnumWbemClassObject *pEnumClassObject = NULL;
( Z# I7 l$ E/ H) n        WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;; Q" k( H& C/ m6 z# `3 ~
       if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)
# f* b6 u9 o) D/ I2 m! z         {
! o% k  P$ B$ A  Z           IWbemClassObject *pClassObject = NULL;2 E. Q  O1 U( l! f
          ULONG uCount = 1, uReturned;
( |, {" ~% z; x" o6 A           if(pEnumClassObject->Reset() == S_OK)
& I3 h, G3 h! a: P! D            {
. R7 U6 j' H7 f              int iEnumIdx = 0;
$ Q* R" D9 i4 }              while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)
; u4 B+ ~9 S+ u, B, f: O1 h4 w8 D               {% b$ h& C' `/ b+ N
                lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");
1 m8 T# E" F8 h- B
6 Z& y# c! d5 U8 Y4 |# Y* Z) O! _                 SAFEARRAY *pvNames = NULL;
$ g6 P! q8 ^# v4 Z                 if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)9 r" C+ |0 @$ ]2 m) }5 {
                 {
2 P+ z* z& t6 O                    long vbl, vbu;
  {# r) @6 A' X4 t/ o                    SafeArrayGetLBound(pvNames, 1, &vbl);: v% \  g7 _. D$ ]! _  r7 p3 N- F
                   SafeArrayGetUBound(pvNames, 1, &vbu);
2 U4 z0 O1 G5 }3 a6 y                    for(long idx=vbl; idx<=vbu; idx++)
' B! E/ M- p2 H6 I2 I7 L2 t                     {% c' m4 `* h$ m* S
                      long aidx = idx;
! X) V/ H) F5 @' R/ ]  }! w                       wchar_t *wsName = 0;
" {% M9 K4 u/ I4 c4 ~& u; f' g* Z                       VARIANT vValue;
& x/ `7 o8 Y. ~6 W7 ?                       VariantInit(&vValue);- {. i0 _# p4 ~5 w, z: e$ k9 F
                      SafeArrayGetElement(pvNames, &aidx, &wsName);7 O' D( y: ^; R8 i: t- {5 W

9 p2 B( @% j& @7 Q                       BSTR bs = SysAllocString(wsName);& I' Y- A- ~. M4 b9 \# t( I
                      HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);
8 u) E! x' h' O$ n                       SysFreeString(bs);7 b1 g( F" ^6 f* c

, Q9 G* N- a6 v: n% i                       if(hRes == S_OK)) H- Q( W' w$ y+ b3 ~$ |
                       {; a, j6 E0 B& P  t2 a
                         AnsiString s;+ z/ K, ^$ t( J, ?7 c4 b9 R. j
                         Variant v = *(Variant*)&vValue;" q/ ~. ]( B/ z7 f. ^
                         if(v.IsArray())8 |' ^) t; j( Z( D
                          {
, x3 M- E; M6 A7 V                             for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)
+ B: W9 ?, w; I+ z( M) w                              {
0 Z2 j5 u3 |5 V. d                                Variant a = v.GetElement(i);
" l! F6 \* H) [: x                                if(!s.IsEmpty())
8 i& R- ~4 B' R* D! D' z                                  s+=", ";
. D& g  P6 @% l                                s+=VarToStr(a);+ T9 F6 E2 A3 u1 [4 ]9 u/ G8 s
                             }# h' k& [' O$ ~  }; q4 q, F
                          }& _; l5 U3 x* B/ ?; ^/ }+ A
                         else2 P6 w$ ~$ m2 |, X0 d" G
                          {
, Y/ I# g7 u8 D7 b$ d                             s = VarToStr(v);
5 P9 ~) @6 t7 |: y+ b4 U                           }
4 [, s+ E  Y' x5 T% ?8 N* S& D0 z                          lpList->Add(AnsiString(wsName)+"="+s);2 V; C6 G9 i( D" r; b1 c! l' K
                       }
) l# i. U& _$ L! L- L. H+ Z% z. v" W0 L6 N4 P) I
                      VariantClear(&vValue);# T) d+ E1 ?8 Y+ D) G; v% ]+ e7 x
                      SysFreeString(wsName);
5 c$ w8 Y9 m9 }                     }
; x* ?) V) I/ l9 D  V  X                  }
" F# k7 h" Q$ `8 i                 if(pvNames)SafeArrayDestroy(pvNames);9 e) p# b* v. `. I* j2 a, W
                iEnumIdx++;4 [& z8 T/ _" A+ u/ s
              }
6 E& `: O2 X. E% }9 ]. Q/ v7 }            }
% O8 l8 S' u9 z8 Q' _3 l+ _           if(pClassObject)pClassObject->Release();7 j) g& A9 Y: y: P' B
        }0 ?, }$ `2 r+ a1 V  T% Y, z8 y3 H4 b
       if(pEnumClassObject)pEnumClassObject->Release();3 z1 a" i3 [$ n0 x* K' h
     }
- e$ u9 n' M* [     if(pWbemServices)pWbemServices->Release();6 g* p- f* r9 T$ Q2 [* ^! h+ W
  }
8 j- K1 q; Y) `0 _+ V: u  if(pWbemLocator)pWbemLocator->Release();
+ o3 e+ S+ X* b; B}
5 v/ ?5 v+ r9 `" N- @" T. C//---------------------------------------------------------------------------( H: W3 O$ O( z" b7 d
9 [9 u- d+ g1 Z% g5 F
// 通过 WIN32_bios 获取 BIOS 信息:5 E( e& {. \" j# ?- p5 _
void __fastcall TForm1::Button1Click(TObject *Sender), ?: ~, Y" |4 Y) z$ n
{  X( E' q5 K0 I# n! H
   Memo1->Lines->Add("================== [WIN32_bios] =================");' K9 K5 w7 ?! l9 X! R
   GetWmiInfo(Memo1->Lines, "WIN32_bios");0 @# b' z* Z5 Y8 `5 T. @
   Memo1->Lines->Add("");* m  X9 j0 Y/ ?9 E- x
}; _% k7 Y+ U% f2 Z9 Q9 ^2 H1 v

8 Y: u# O# I5 J# M* K( {* }--------------------------------------------------------------------------------
+ @8 C: Z, `4 P8 i4 Y. o! |/ O! ]( @4 p7 K. a' J
WMI 可以访问的信息类型有:
5 \6 x# R5 Q- U2 k( F; n   Win32_1394Controller1 C0 b; n- h/ d5 T4 ?
  Win32_BaseBoard
3 A7 w# a7 f' l, }. ^% J/ O   Win32_Battery
4 E: l# A, Q  ^8 P4 k, {3 W1 J/ F7 S   Win32_BIOS
8 c6 K4 t+ g0 ~   Win32_Bus
1 j4 K  q" a! n! ~8 p; V( f   Win32_CacheMemory
7 L/ p3 d- [; {0 M' Z9 h8 K, x0 n   Win32_CDROMDrive$ `3 {3 P9 y, X5 o8 X, K
  Win32_CurrentProbe9 p6 U, K; Y" }1 G2 J
  Win32_DesktopMonitor9 A; \# C$ w  G# n+ B# k2 @6 J: X
  Win32_DeviceMemoryAddress! t4 x9 w' c/ O1 x6 |6 a$ K
  Win32_DiskDrive$ Z( k: l6 x7 S2 F0 r5 z7 C% S* C
  Win32_DisplayConfiguration+ R, o. a& t$ d' i
  Win32_DisplayControllerConfiguration, `2 ]7 _$ Z7 Y* h
  Win32_DMAChannel
  Q* p- p- m, X- E   Win32_Fan6 A# H* w/ r5 Y$ K
  Win32_FloppyController
6 O: H& V9 C: h5 ~  @) C   Win32_FloppyDrive
7 }9 w0 q3 T. V, p3 E" |% m   Win32_HeatPipe0 |- M& ^' V. ~2 v0 a. P
  Win32_IDEController
: F# ?; x' H5 \  i   Win32_InfraredDevice" w. b4 @- i) ?2 H% c. I! Z6 V
  Win32_IRQResource8 f" K$ c, n$ X
  Win32_Keyboard0 x' f/ Y/ l2 Z
  Win32_MemoryArray
( i* O, n- w5 q4 V9 P   Win32_MemoryDevice& U' ^" ]# }9 z( J2 U7 `
  Win32_MotherboardDevice
$ y6 d: A8 N! p; y- p1 S! t* d2 o' v   Win32_NetworkAdapter) _8 t  s6 d1 |
  Win32_NetworkAdapterConfiguration  o6 q% U6 p; C  r' B# R8 D7 y
  Win32_OnBoardDevice/ V2 m' @' ^) u$ S
  Win32_ParallelPort
' g0 u* H6 r, p$ _+ T   Win32_PCMCIAController0 @$ `0 F4 D: H# v* y
  Win32_PhysicalMemory
/ u9 O. }- f6 W/ c   Win32_PhysicalMemoryArray, B; r" z9 [) U+ |; Y) |& m+ g1 c' C
  Win32_PnPEntity
) G, ^& [1 b7 m$ b, z" w& R& o   Win32_PointingDevice
2 f: N; v! }4 b; p+ }  O4 r+ B   Win32_PortableBattery
# t+ T. s* m2 K9 }   Win32_PortConnector
7 e. ]) u3 e( u: T   Win32_PortResource
; C$ p! J. r; t% X/ Z3 ~& T1 x   Win32_POTSModem7 k6 |8 q2 w6 K- n' M5 {
  Win32_PowerManagementEvent. Z3 O& k8 V3 e* g
  Win32_Printer* d, T/ C- I3 p8 F2 w
  Win32_PrinterConfiguration
& D7 g+ B8 }+ K, c   Win32_PrintJob
  u# E2 N6 K" U/ U6 v   Win32_Processor0 E3 p2 ^% K7 O, f
  Win32_Refrigeration& |4 ~  E! M/ x! X, ?; B
  Win32_SerialPort* h: ?# m/ ~6 C4 t$ Q- f7 g
  Win32_SerialPortConfiguration
; G( s" d4 {! ]6 _; x   Win32_SMBIOSMemory
/ {' c1 e: s5 s" {/ j; t' E   Win32_SoundDevice
$ S2 w, e) P8 r- j   Win32_SystemEnclosure
4 ]! ^7 j) o2 {9 d9 u   Win32_SystemMemoryResource* K7 z  p$ U* Q
  Win32_SystemSlot
6 y. m+ z$ ~( C% ]5 X   Win32_TapeDrive) E4 \; M  T, F; s9 I* U; E5 w
  Win32_TemperatureProbe
* ~5 Q# w5 [9 @" {) O6 r: a   Win32_UninterruptiblePowerSupply
' S6 b# I2 S6 B* ~. ^0 I" y   Win32_USBController
/ \# X- L& ^! u9 d( M   Win32_VideoConfiguration
6 b7 d2 Q- i% ?   Win32_VideoController% }6 L, z0 l3 G) D
  Win32_VoltageProbe& [& F( H% K3 J

% z1 H5 z. q, u! v以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-12-26 08:49 , Processed in 0.019433 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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