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

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)7 ?4 I6 i3 s; f3 g
" `4 v7 R8 X9 d6 o7 D

' C- a6 _2 G+ T4 J% W9 j* P--------------------------------------------------------------------------------1 R. ^3 f7 l, u2 c* x
WMI: Windows Management Instrumentation (Windows 管理工具)6 `; C5 o" @' S* d) c
  通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。
# V$ P; m  e( r6 |6 i5 ?   利用这个工具可以管理本地或客户端系统中几乎所有的信息。
2 ~, U5 z% c. A- ?) L' y   很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。
. B/ j) n1 n+ |# s  k3 L" u9 b- U& T9 r( M3 C
--------------------------------------------------------------------------------* u. v! {: N- s  b0 {" \8 H
BCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面
3 d6 N- X  L1 @- p+ H4 f) F1 b1 ?& d2 ]3 v0 ], f, V
--------------------------------------------------------------------------------
) c1 T8 p: B$ s3 l2 `① 初始化 COM 接口:
$ w& K7 q/ _8 I0 \# P3 n9 }   访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。
2 d0 i4 M. @/ E8 J   这两个函数在 #include <comdef.h> 里面定义。
( U: _( ]/ P' c( x0 U4 L, m% K/ u9 z
② 获取访问 WMI 权限:* S3 h' d1 v& x0 }
  CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);
) b; `6 B( i$ V6 B( L3 ]( Y+ N! a1 S   如果这个函数返回 S_OK 获取权限成功, 否则为失败。
- R, |: f* e* K8 C+ m9 i7 L, S0 o% _& Z
③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:: S8 F% q1 M! w* i
  这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。
. I1 J8 C: }. w& G/ |- A3 j1 X' }1 @1 G) A* Z9 g+ e! R
void GetWmiInfo(TStrings *lpList, WideString wsClass)' t$ D/ z0 e3 q
{, n) _- M3 H# A
  IWbemLocator *pWbemLocator = NULL;
* M) s3 ^/ I5 `: g3 A  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)
' F/ ^" ]" j: N6 O! r   {1 S) P8 F# L" n1 W) b- ?2 k
    IWbemServices *pWbemServices = NULL;6 n$ O6 L# w3 g' W, s) \/ S
    WideString wsNamespace = (L"root\\cimv2");1 v4 F5 ^/ |; a' I6 e3 ~6 L% R
    if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)0 a, r, ]/ t4 [! m0 j9 A
     {
$ s7 F& U& s$ x- R        IEnumWbemClassObject *pEnumClassObject = NULL;
/ H0 H9 n8 p8 R7 b# P) c' G        WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;& m0 e* u" b. I# p, p/ s! E
       if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)4 s8 t+ x, ]! d) L+ J
        {
7 W$ S: R) `& q- w0 r           IWbemClassObject *pClassObject = NULL;/ Y! ]5 ~( s4 w8 G" [0 }9 Z
          ULONG uCount = 1, uReturned;
' P; V3 \+ l# P0 s' K% |- ], r           if(pEnumClassObject->Reset() == S_OK)
$ i! ^4 i1 g0 K" w5 J7 t5 A            {
* @- }- ~7 ^! u3 S% \0 W$ K              int iEnumIdx = 0;
7 L9 @& Q3 p( \: M* @              while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)
) k6 m/ c: ?' h& p, D# j               {
' H  z. j( O! `& U  P, ]8 V" [                 lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");: n9 V* q5 O& r
8 y7 c$ }  w& O' O% g
                SAFEARRAY *pvNames = NULL;$ Y5 f* v7 c$ i
                if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)  ~) ~5 _& q8 f! B1 W* b$ z
                 {
* j( J( g  r+ ?* L* P3 X* z9 y% f1 N( d                    long vbl, vbu;
4 H8 ~0 t6 ]4 A6 X' {" g$ {                    SafeArrayGetLBound(pvNames, 1, &vbl);
0 ?4 S2 I! O" W. T% f- e                    SafeArrayGetUBound(pvNames, 1, &vbu);5 E+ V; l9 y$ L& \- M: ]! J$ W
                   for(long idx=vbl; idx<=vbu; idx++)" Q  G, m# P% c9 h* \8 H" A* g
                    {' |5 u! V$ Y$ A5 ~* i0 e
                      long aidx = idx;
, [! W+ I& Y8 p                       wchar_t *wsName = 0;
( ~  F. {0 `/ i8 }' e& t4 m6 B                       VARIANT vValue;9 d7 R; P$ r1 F4 ?# o3 Z
                      VariantInit(&vValue);& y( q* p* ?- x/ X7 I( i
                      SafeArrayGetElement(pvNames, &aidx, &wsName);: ]0 x. G$ w. U  i6 U/ I2 K9 c

/ f8 P: a+ ~7 L) }) C                       BSTR bs = SysAllocString(wsName);
: K: f, P2 O+ `  E                       HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);+ u3 Y7 i( C9 D8 y# g: a
                      SysFreeString(bs);
- `' B/ k* x3 J1 Y9 p# K! T  z* x2 S6 [1 ~6 G. a0 ~9 `7 B: R  M
                      if(hRes == S_OK)/ G1 E6 G0 @' [
                       {4 p/ G6 d3 e( k" `
                         AnsiString s;0 q# Z3 B% l! ^! ]: \- J; h( q
                         Variant v = *(Variant*)&vValue;
5 Y, s  A  x& U& A/ E+ M- T                          if(v.IsArray())
/ K# d0 s0 h) b* m- n                           {
5 W3 m3 n  x3 D+ U! J, g+ q- {+ x+ H                             for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)
9 O0 y7 H. J+ V/ o# p1 T& L                              {
' [& W) i; E4 U                                Variant a = v.GetElement(i);
% d$ F. o  ]! y  m* |' |$ i                                if(!s.IsEmpty())5 I) ^( W  \% _
                                 s+=", ";5 ?$ _1 n( n3 `" _) h& O
                               s+=VarToStr(a);
% @* q: {1 t6 Y. O$ N5 I                              }; `/ h  G6 y" t9 B4 K
                          }, x( J' O9 f0 K& s& Q
                         else
9 X# ?$ k% W; X. [                           {
1 M: A3 V0 {2 c4 R, J" x                             s = VarToStr(v);  a0 Z8 x9 F3 {4 S, [2 P3 i' N
                          }
4 l8 M( L( B- T: ^$ H                          lpList->Add(AnsiString(wsName)+"="+s);
3 C. [# W9 ~1 A/ K* c                        }
' z3 {" H) A; K+ A& C2 [' \- G5 ?  O
                      VariantClear(&vValue);; V8 w- z8 y- E4 V0 |! O$ R
                      SysFreeString(wsName);
3 W8 G7 }; ?2 y) J- {                     }1 r9 W3 ?! U3 X
                 }) Z  e( Z0 \0 i# B, B# A4 ]0 b
                if(pvNames)SafeArrayDestroy(pvNames);9 R  N. }( [& U2 v; p" [* b1 ?
                iEnumIdx++;  F  N, a6 x4 H& S+ s! c
              }% O- f5 q4 f: u' @/ y9 M0 P
           }( e$ I2 K$ p  ^5 {: R0 j" G1 m0 s* k
          if(pClassObject)pClassObject->Release();
1 K, T' S3 j, J8 _6 |/ z1 ]: Z3 o         }; i- ]" K; R0 ?9 @3 t
       if(pEnumClassObject)pEnumClassObject->Release();
% D9 h* k" k0 b& D+ a" _      }
+ c! u( j: x1 s& C. v1 \     if(pWbemServices)pWbemServices->Release();
6 W. V  O( b$ D! v- }5 p   }
' V9 b! C$ O. S# H  if(pWbemLocator)pWbemLocator->Release();9 H3 |5 @" @6 C; R/ e. I+ R* s
}2 u& e- X" k! y5 |4 v
//---------------------------------------------------------------------------1 p6 ]( L/ f3 y: \/ m

, }# n% s4 p; _: d  x// 通过 WIN32_bios 获取 BIOS 信息:- r$ M6 d7 r/ E) F% [; S* \) i( u
void __fastcall TForm1::Button1Click(TObject *Sender)
' M; ]4 ?3 w, B2 L- k% w! @, ?{
# m! S3 ~# e+ z* S    Memo1->Lines->Add("================== [WIN32_bios] =================");
1 `/ ]' x) E# W$ \+ q9 g    GetWmiInfo(Memo1->Lines, "WIN32_bios");
& z8 Z3 ?% e6 V! c$ w( `* \; [    Memo1->Lines->Add("");  x  o9 Y# w6 o1 y+ v. x) X
}
! V: v4 j3 ?+ S; s' e; f
( s: |8 i8 Y. C# s0 Y--------------------------------------------------------------------------------( u' }0 f6 O4 E* A0 ^! R3 V
$ m6 O+ ?- f6 \+ h7 r$ X
WMI 可以访问的信息类型有:
9 H0 Y+ d9 N* D) K) Z* ~   Win32_1394Controller
. d3 f1 ?  u( T5 N7 V+ a  _9 r5 J   Win32_BaseBoard
! M' N, X$ W6 U% G0 ~   Win32_Battery
1 ~1 t; M/ B* {7 u; F; w. p4 p   Win32_BIOS/ G! h' g. O* x4 c" l$ J! m5 g: @
  Win32_Bus. ~/ l0 c+ Y: L9 d" ]) f& f) Z. ?# K
  Win32_CacheMemory
+ A( f7 U( M$ [7 R/ ~& I( o3 {/ H   Win32_CDROMDrive% }0 m! T. Q. Y$ L& j! @+ m  o
  Win32_CurrentProbe
( [" @  ]0 G) {, }2 r' Y- v- T   Win32_DesktopMonitor# s) {: H  j  m7 I9 s: Q! r
  Win32_DeviceMemoryAddress5 j! s- ?1 X. X+ t) j5 R
  Win32_DiskDrive
' N9 q! J; V  L! A; y- `% z   Win32_DisplayConfiguration
- G1 K2 w, m3 Q7 A& \   Win32_DisplayControllerConfiguration% H; `4 g: l! }5 D
  Win32_DMAChannel
- [# k1 ?5 a0 _0 |% o! [   Win32_Fan
$ H2 ~2 \; T. X: |0 R. O5 T+ a   Win32_FloppyController, e# ^6 |' u4 g, J6 P
  Win32_FloppyDrive
5 `( y& p0 G! F8 j5 K3 q   Win32_HeatPipe
! n: I! ]" V( z, }   Win32_IDEController/ `: I5 o. [& g5 Q3 q$ p
  Win32_InfraredDevice
0 x& M( B& K, S0 d4 }5 ~1 q9 b- z) z   Win32_IRQResource5 S% l2 W+ |/ i
  Win32_Keyboard5 D0 A6 n. n. h1 E
  Win32_MemoryArray
+ k; `* s7 z2 \4 x- a! y% y; w   Win32_MemoryDevice. B* E8 }- g$ v; ^2 U: _
  Win32_MotherboardDevice& H: d$ {! b' ]; I; G6 p
  Win32_NetworkAdapter
1 X6 P% @. n7 @7 V2 P   Win32_NetworkAdapterConfiguration4 @7 f: i, p8 O% \! E
  Win32_OnBoardDevice* {+ W6 i) ~: H6 ]3 z3 x
  Win32_ParallelPort
, u6 p+ b6 D3 |  W. Y% M" z1 V  b3 f   Win32_PCMCIAController9 `  c) f/ v, u
  Win32_PhysicalMemory: u5 u8 d& A% [# c
  Win32_PhysicalMemoryArray0 W2 b. I6 {, _: p- m) ?
  Win32_PnPEntity; C0 y$ g; N; ^) U6 q
  Win32_PointingDevice
, r6 e$ \2 y* T4 h- u8 X  V' h   Win32_PortableBattery4 u, U  h; b9 @( o$ {
  Win32_PortConnector, |/ a' V* e" K& C  }, ^9 O* }
  Win32_PortResource
9 S7 |$ h8 T) A   Win32_POTSModem
$ V& V8 @+ q( W2 r0 o% p0 f9 {   Win32_PowerManagementEvent' a4 d- C" {9 Q" J  ]! a7 K
  Win32_Printer
2 m! _4 B' G2 V# p2 e+ a* {" V   Win32_PrinterConfiguration+ ~& O9 p" F- y$ u( E
  Win32_PrintJob$ M% u. @! C0 U1 c5 V6 j- H7 L
  Win32_Processor/ |( A0 `& j# `1 X% p7 {% [
  Win32_Refrigeration
9 T4 a# ^- l% |# ]: W4 I7 [   Win32_SerialPort3 e" \3 ^7 ^! t
  Win32_SerialPortConfiguration
- m  I* h  Q" x; q; V8 G   Win32_SMBIOSMemory
, X/ |3 w8 ^, U   Win32_SoundDevice
1 H; s0 t2 t, Q   Win32_SystemEnclosure
$ `% m% D, \. C: r8 v0 z6 e   Win32_SystemMemoryResource
& k; P& i( ~1 {0 u) s& e6 q6 f   Win32_SystemSlot: \; Z, T& j! E$ ]
  Win32_TapeDrive
. C( g  J6 I* b/ \3 |   Win32_TemperatureProbe4 F" z7 a/ k6 D3 f. o8 j
  Win32_UninterruptiblePowerSupply
2 |, o+ S  c# D; z- |5 H' @   Win32_USBController
  W" }$ Q2 b. R4 p   Win32_VideoConfiguration1 T  x0 k4 q; M
  Win32_VideoController0 |; A3 Z$ s# o4 J$ E
  Win32_VoltageProbe
+ I8 N" n: L& c/ T% U9 \& K- Y$ S6 L( Q, w& t
以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-1-12 08:03 , Processed in 0.111956 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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