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

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)4 t7 O% d1 ~9 t/ j

. ?# ]! [" A$ P% z: v  R
/ ~, s  T6 m% ^( M- q/ l" `( H2 Q--------------------------------------------------------------------------------
' \1 o$ y2 i! C( hWMI: Windows Management Instrumentation (Windows 管理工具)
7 }3 ~0 o) h7 m$ N0 b* X: `   通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。 6 K1 {5 ?# a8 y) m
  利用这个工具可以管理本地或客户端系统中几乎所有的信息。6 i( A/ d' y5 p. t  `
  很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。
3 ~8 L' _. R' h, _5 B3 f' W: _- I  [* ~3 ?
--------------------------------------------------------------------------------4 f0 k: S, t' ?. L; D5 _7 d
BCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面+ C+ [% U9 T- u" V, P% ?
2 @* c% Z4 ~4 g5 R4 i
--------------------------------------------------------------------------------
' ?. t& x5 J% Z0 q① 初始化 COM 接口:
5 h9 M+ Q* L7 H6 N7 i: R# L% B   访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。  {0 k" C5 @6 o" a3 o0 W
  这两个函数在 #include <comdef.h> 里面定义。
$ v$ h: e( g: Q, q3 D7 O; Q4 l5 h! d  p+ u4 K! J
② 获取访问 WMI 权限:$ ?8 e. A: s! l5 S0 T7 ~* L' z
  CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);
" E2 j$ r& k, d8 y   如果这个函数返回 S_OK 获取权限成功, 否则为失败。
; {- V) X. S; L- N
; \- x, Z: t$ @0 i' p③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:
* W- W7 [1 f3 {. G5 I   这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。
7 t, M5 c. ^& \5 o  ~; L" Z; x$ C2 D( e4 ~2 k
void GetWmiInfo(TStrings *lpList, WideString wsClass)
5 p: m$ X0 Q/ g( b0 Y- I$ G{
. G3 }0 ^, H, o0 K3 x  IWbemLocator *pWbemLocator = NULL;8 _" m4 z! K' A7 ?1 M  Q- N* X% y# f( c
  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK), i5 x, h4 Z$ D* O6 B
  {. P3 r4 }' R4 T/ z4 X; J& [
    IWbemServices *pWbemServices = NULL;4 N7 r( q2 m6 |9 R
    WideString wsNamespace = (L"root\\cimv2");* V) {7 }( ~/ e5 v& i) S
    if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)/ b& _$ F/ ?4 }
     {2 d, z, L9 s$ R# c
       IEnumWbemClassObject *pEnumClassObject = NULL;6 |1 x9 b: [0 H
       WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;% P/ t- U: I$ v- v
       if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK), v8 k1 s8 H1 M* ?
        {
3 @7 ^" Y5 G- C' Z( H           IWbemClassObject *pClassObject = NULL;/ C) D# H0 I( J/ E7 j3 c+ v0 H
          ULONG uCount = 1, uReturned;
2 U+ m# @0 p1 D1 ~; |( D: t. P& Y           if(pEnumClassObject->Reset() == S_OK)
+ v! A; P, z5 N0 r8 t- o( o; ?            {3 D( I9 \$ q- P6 a8 I; M$ H
             int iEnumIdx = 0;( n0 L* Q: j) m4 t: h1 j  }6 C! d
             while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)
( e% E& [4 P- Y" G. ~4 P2 |               {) D( x; Z$ _% q
                lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");; D1 |8 O7 r$ p- |+ ]1 X
- k: E+ Y# z; ^' a% b1 u
                SAFEARRAY *pvNames = NULL;$ q$ @2 n9 W. A
                if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)
, D% \" i# B8 {                  {
% `! \2 N" o* R& A' Q& D                    long vbl, vbu;$ S( p6 N/ N, H& C' {" |
                   SafeArrayGetLBound(pvNames, 1, &vbl);
$ \. K. Z" ?: j0 }* Q9 t                    SafeArrayGetUBound(pvNames, 1, &vbu);
" [% X( D& d' X6 m& X7 v9 ^                    for(long idx=vbl; idx<=vbu; idx++). e$ b; h0 G( V" N- d/ \2 Y
                    {
8 H0 g8 F# a& W2 L9 D5 E/ K0 r3 X                       long aidx = idx;
) z8 q. t$ t" S( T3 i                       wchar_t *wsName = 0;6 y* I& K+ L  h6 s+ O/ T
                      VARIANT vValue;* S4 T% E" B5 [/ t
                      VariantInit(&vValue);. D- s2 C  n7 D: t! A: y  m0 B
                      SafeArrayGetElement(pvNames, &aidx, &wsName);6 ]9 u+ r+ y! K+ Z! x5 y4 }; y

. [+ J, I) a, Q$ j+ U                       BSTR bs = SysAllocString(wsName);
0 a+ j0 D. P- ?4 h; m. D                       HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);8 Q, a, p1 D# u: y6 n1 B
                      SysFreeString(bs);( s7 g* ]6 @% F9 i+ i

" x" w1 J3 L& J! Q1 \9 h                       if(hRes == S_OK)
' u  C* L1 `- q2 u* d/ \: }                        {* ~4 q5 I; n# r' I- x4 R9 S7 t
                         AnsiString s;
2 s" S" H& m+ n' ?4 O                          Variant v = *(Variant*)&vValue;
/ s/ L7 U- c" j" B+ _& a' u                          if(v.IsArray())8 v  A8 t5 V7 X4 c* ]
                          {
8 \. c" ]+ J  G) O# h* T* e( d! y' P                             for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)1 E# F& q7 d, _
                             {, q. V0 A: c; l& e. G% l# F
                               Variant a = v.GetElement(i);
- Y- R3 ?' J+ ?  M! E# w5 q                                if(!s.IsEmpty())5 b: Q8 c0 y+ x' W6 d8 {8 f* e
                                 s+=", ";
& X; I( o" }' B  L# K9 u                                s+=VarToStr(a);
+ h( C) M5 x/ ^+ ^  I6 M1 B                              }- h- [. C0 X% c4 j- p! `0 _0 R+ ?+ l
                          }5 x1 J% L7 J1 M9 t
                         else
- y' o5 h6 C2 m$ N& P* ]8 q                           {
2 C' h2 `. M6 b+ j$ [) S                             s = VarToStr(v);  I0 `5 Z6 f. c& h5 c" F% d. r
                          }1 i! P/ L8 I! L0 T5 V+ R
                         lpList->Add(AnsiString(wsName)+"="+s);
3 h0 F( V9 w5 G' A% j                        }
; {6 J% O$ f" \9 |3 O' E( ?  Q) t- s' S, J2 B; T
                      VariantClear(&vValue);
: f: ]" ~+ D) N  P) y                       SysFreeString(wsName);: Y( r: ^4 J# I* F& ]! Y
                    }
- I. F* N  D* I. v                  }) ^. C6 r* v6 g( P/ C
                if(pvNames)SafeArrayDestroy(pvNames);9 ~) P8 w( q1 G3 Z+ M% U1 _
                iEnumIdx++;
; N: q9 s( P3 r& c. b1 f               }
5 J0 H& E1 m! a5 v/ |, J            }5 p) y+ V3 l# [
          if(pClassObject)pClassObject->Release();
/ Z, V! o" B! i: n. l# A         }! M! ^) c! e& v4 O. w
       if(pEnumClassObject)pEnumClassObject->Release();! }( O6 Z- t% R# u5 n% p9 ?* ^4 W$ Y) {
     }6 k5 s4 x, n# Z) ^: c9 R
    if(pWbemServices)pWbemServices->Release();- d9 e8 ?6 A4 k6 B; r0 v1 y5 j8 J0 ^3 w
  }$ h6 Q, |9 b$ v3 r! E
  if(pWbemLocator)pWbemLocator->Release();
4 ]" g! g) K9 U5 Y, ^& T}8 `" F& s7 F. h; X, _- m6 B
//---------------------------------------------------------------------------2 F. I# Z6 L1 P. a0 h. P# I# y
( I# Z0 H$ X3 x" S
// 通过 WIN32_bios 获取 BIOS 信息:5 u9 |$ t8 V/ g" R  n3 H3 |
void __fastcall TForm1::Button1Click(TObject *Sender)
4 N, p; Q$ s3 I7 c) c{
% _$ ]9 _  @2 b    Memo1->Lines->Add("================== [WIN32_bios] =================");
4 u/ T8 l% k0 R) d5 k: |    GetWmiInfo(Memo1->Lines, "WIN32_bios");& ^- v0 j0 X: V) _4 I
   Memo1->Lines->Add("");
% Y& ~  B' v1 [}! @+ r9 W3 T8 j, ]- B
% E6 l/ U% a- b" A1 Q' d
--------------------------------------------------------------------------------
3 }, b; C. [9 [' ~6 N% I7 P( a# u0 A, Q. L
WMI 可以访问的信息类型有:
' _' f/ f! D  t8 e+ v% O9 W3 X   Win32_1394Controller
4 r1 H4 P2 s4 s+ f& E: [7 Z- @   Win32_BaseBoard% j4 S% G( {: |  B1 J- q
  Win32_Battery
5 X7 {; N2 h, c( G1 T6 |   Win32_BIOS
6 b2 i3 l: T& U! X   Win32_Bus" _9 a) g8 d; s& w
  Win32_CacheMemory
6 d2 z& Q7 F$ b- {& ^, [6 n, \   Win32_CDROMDrive4 P4 p/ w1 l3 ?
  Win32_CurrentProbe, w4 s3 s8 J; ?0 \; n
  Win32_DesktopMonitor
; V. p  }8 j0 f& ~   Win32_DeviceMemoryAddress  {3 F- T) z% w5 m
  Win32_DiskDrive
  t% m  z6 J) f+ h9 }- Q   Win32_DisplayConfiguration
( `: d6 R8 N) J& ]1 w$ P* F   Win32_DisplayControllerConfiguration
. R7 v2 d) e* r4 C   Win32_DMAChannel3 M/ H7 ]" @8 `$ b# ^: k+ D. A8 t7 `
  Win32_Fan
6 d8 `% P! {9 {   Win32_FloppyController. f. ]$ I! W1 A+ B# ?
  Win32_FloppyDrive
* ]3 h3 s$ h* `, q' ^2 R1 f   Win32_HeatPipe! U- Y! a7 k1 I! T, k
  Win32_IDEController7 {5 J* ~. ~3 P4 @/ _
  Win32_InfraredDevice/ n7 [6 [6 V3 ~; Y* f$ X4 N
  Win32_IRQResource6 x6 d+ Q7 b) {" j$ C8 c
  Win32_Keyboard. H3 L# C% W" o& A4 {
  Win32_MemoryArray
& f3 o; l5 Y6 F& e  j9 w! f* p   Win32_MemoryDevice' s( k8 j! F- p8 q" ?2 g
  Win32_MotherboardDevice
: C! A& S9 p) b/ \7 U. O2 Q( x   Win32_NetworkAdapter" d* w$ U( l3 c8 v
  Win32_NetworkAdapterConfiguration
2 Y+ R, H0 B3 ?' O! Z   Win32_OnBoardDevice
) h0 E9 \8 K7 G' ~' @   Win32_ParallelPort8 D% {" F" \0 _4 l; D# U0 [, m7 w
  Win32_PCMCIAController
/ K0 J  \0 H( n   Win32_PhysicalMemory
5 i5 h6 ?$ ]/ B1 d* c8 V   Win32_PhysicalMemoryArray
" C# X( \, V! z' m0 u- r' _# f; l   Win32_PnPEntity) @' i( F( V/ R4 D* t1 ?; H) r
  Win32_PointingDevice0 U2 Z* g" N$ h2 ]9 i9 E6 C* \
  Win32_PortableBattery. h8 u' U/ i- I* g7 \: L$ u& i
  Win32_PortConnector
* J# w: e2 ^/ f; Q* b' X/ [   Win32_PortResource5 ]0 E/ E: w3 T8 V2 ?4 {6 Y
  Win32_POTSModem
& @! T* ^; S* Z" Z( |( K   Win32_PowerManagementEvent
3 ]1 n/ c8 w' J/ j9 w   Win32_Printer
* N9 r( P6 j8 p   Win32_PrinterConfiguration
  P0 j4 A" Y% w$ P8 N& F8 s   Win32_PrintJob
3 d3 s* ^* u0 k7 |+ Z: t; v   Win32_Processor
! I7 Y, ]7 @& M9 J3 W+ \( H   Win32_Refrigeration
! C7 v8 Q# k1 ^6 |& q$ E% g9 j   Win32_SerialPort' d) ?3 q/ w8 \& e2 F  w  O6 J' s
  Win32_SerialPortConfiguration
/ s2 @! C0 W# N  [7 f2 b   Win32_SMBIOSMemory: I, |& t: c/ a: v% G
  Win32_SoundDevice, h4 u9 X  _4 x0 c1 T( n+ G: F
  Win32_SystemEnclosure  |$ O5 ]* p$ F0 R, {$ K
  Win32_SystemMemoryResource3 b# W" x' i+ V# ]% ]
  Win32_SystemSlot" |4 @  T3 O) [
  Win32_TapeDrive
, y  {( m; u2 w4 C+ N' H; e   Win32_TemperatureProbe& N+ ]- ]' I/ \8 ]. k
  Win32_UninterruptiblePowerSupply
( h" b- G2 {# J8 i% b# ~   Win32_USBController
/ c1 k8 L0 I) X- Z' e: G8 g# M   Win32_VideoConfiguration
# p1 R2 A6 Q7 U. J: X   Win32_VideoController
6 q2 Y7 q  n. g# v2 W, r* y3 J   Win32_VoltageProbe
* L* @! k7 h/ B5 }$ i5 r4 w# l& F2 b3 |4 t6 c# H
以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-2-1 12:31 , Processed in 0.018813 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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