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

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)
% O4 K  W. p4 P# v: p& E& @( f& Z/ x. i0 C) z
# Y" S$ h; k+ J7 w8 x5 O. t
--------------------------------------------------------------------------------
( R9 R$ q, q4 A  O9 v( FWMI: Windows Management Instrumentation (Windows 管理工具)9 K+ L( L) d5 ^) U
  通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。 & h/ S# }7 }' w2 e; `0 Q1 m
  利用这个工具可以管理本地或客户端系统中几乎所有的信息。* C* {* o3 I( J6 Z  |; a
  很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。 1 I5 Y- I) w- F, r

; s: V4 Z" L8 m6 _& Y--------------------------------------------------------------------------------
1 R7 [) H; ]+ I6 ?2 }3 PBCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面
, L8 u# ~( F+ J
; J5 D9 R2 }  B$ W' e--------------------------------------------------------------------------------
; I$ p) g  w; M$ ]) l① 初始化 COM 接口:
+ n0 q% n' M1 [" G1 X: w1 N) j   访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。& _* Q8 ~; w) l& {1 {0 C
  这两个函数在 #include <comdef.h> 里面定义。" Q& C1 t* y3 R. E) f5 ?$ h8 C
' F/ u9 o. V8 ?6 B$ E
② 获取访问 WMI 权限:
: Z% N; P. Q2 c( b& {3 `& G: F   CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);: v  X0 g' m5 r5 e3 ^0 F
  如果这个函数返回 S_OK 获取权限成功, 否则为失败。
2 b% c% h: l% ]5 y# V- r
; B# s; B* U  e* s③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:0 L8 h; X( f4 h! U2 C5 y
  这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。# X" z: G3 K: m5 k

* P) B" ^& {2 p( i& N5 J9 Y- Xvoid GetWmiInfo(TStrings *lpList, WideString wsClass)
: r. k- c% \4 \. s3 ]{3 O8 C. u  X7 z" ~8 |
  IWbemLocator *pWbemLocator = NULL;
8 l3 T5 V, ?) r% U2 P  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)5 G2 n8 j" {7 y, P
  {
# J" E! b* s' x, z; ^7 V     IWbemServices *pWbemServices = NULL;
; P0 b( g3 c* f% O- l+ @+ ^6 \     WideString wsNamespace = (L"root\\cimv2");- ~1 Q& `3 y/ K$ y
    if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK); F7 b% |; Q) Y* ]8 R' _+ y% u. s
     {
% K8 a; P7 r- Z' Y) v3 Y        IEnumWbemClassObject *pEnumClassObject = NULL;
" n, ~& a" ^3 d        WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;
  s- y5 r8 f: G" O5 J# o        if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)/ x9 e, m7 i3 x, s
        {  a5 ^. z% p8 l! b; c7 H! T8 J8 I
          IWbemClassObject *pClassObject = NULL;, n$ N* u/ Q3 H" H0 \
          ULONG uCount = 1, uReturned;+ R: _2 {2 t/ y, m0 e& _
          if(pEnumClassObject->Reset() == S_OK)% B3 u$ E+ \& e9 ~. R
           {/ k0 A$ B3 x; Z3 @/ ~! e
             int iEnumIdx = 0;1 z: D" t+ L+ ~6 @1 m* q
             while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)
+ r  m9 V& f! Q               {
9 o1 H! D' t. Q6 o6 T8 F                 lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");$ N# g& R& Z7 b3 I! ~. M3 x

1 p" p2 o+ ]+ h% A% p0 h# p                 SAFEARRAY *pvNames = NULL;
7 q/ N+ N5 x7 y$ Y  ]* x                 if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)
% @" |8 }4 m# D2 E, H7 q1 Q+ ]                  {
& S& f6 O9 O& \5 {) e$ K                    long vbl, vbu;
( b, b  c) M# [1 {+ M+ V( f                    SafeArrayGetLBound(pvNames, 1, &vbl);; ]; y% Q# c; u. B4 j  S# I7 _
                   SafeArrayGetUBound(pvNames, 1, &vbu);& h# g4 _7 q3 U* a5 Q
                   for(long idx=vbl; idx<=vbu; idx++)
  h' D. H4 G4 Z2 j0 s- n  k                     {1 _) j: {9 A* J" \- w) p
                      long aidx = idx;9 \+ Q' k4 M; t9 f. c4 T
                      wchar_t *wsName = 0;+ Y0 @# d4 a) k6 U
                      VARIANT vValue;
1 W5 A8 O% {6 T2 _% S1 `                       VariantInit(&vValue);
) @+ F) d) v& ^8 M/ s! V6 f                       SafeArrayGetElement(pvNames, &aidx, &wsName);
. Y+ H  z/ ]8 z, h3 G
3 H7 n7 `7 }# f/ x                       BSTR bs = SysAllocString(wsName);8 t, y; P+ s( [% ^2 t  @
                      HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);' R3 Q! M3 S: u' r" H/ j- `
                      SysFreeString(bs);3 r& {! k! x  q. A
8 R3 d+ ^8 n6 r
                      if(hRes == S_OK)* Q3 u% t- u! m8 ]) ?8 P
                       {
, r; |/ Q( {% G3 i3 I9 C                          AnsiString s;
! D6 j8 l5 G7 h! A( J$ A/ \8 G                          Variant v = *(Variant*)&vValue;; Q+ e- M: x: t
                         if(v.IsArray())0 ~0 M+ {. }9 u4 q" \
                          {6 V4 s' q( f5 f( g, ^* T
                            for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)
' z8 ~2 I/ a7 E( x& @) T9 A                              {9 I8 g- a/ ^2 E: M
                               Variant a = v.GetElement(i);, n5 n$ s% [: j3 q0 T! `
                               if(!s.IsEmpty())
4 {* [+ d  g$ J4 A( R8 r                                  s+=", ";" ], X1 t) m7 U3 S1 j
                               s+=VarToStr(a);; }  P3 {; h: c. |% A8 r7 K
                             }  p" V% d; h* r0 b
                          }2 [, X0 e( w( w' u# Z  n
                         else3 G1 Z2 |4 V' J3 y3 i* {% w7 H
                          {2 O( s4 J, k/ b: s
                            s = VarToStr(v);
4 N  h+ [, E' c" v+ r                           }- \5 G2 O( e5 x. V3 l
                         lpList->Add(AnsiString(wsName)+"="+s);/ A2 ^( v; b) V9 I# O% c+ s
                       }1 }) _8 ~! t, a4 d4 `: t" ~, D
7 c; y, ]! o. L' \" ~) I
                      VariantClear(&vValue);/ }# O+ N! f1 t4 T: y4 x! X
                      SysFreeString(wsName);
( u. A, o- M$ g# ~% j                     }' n1 W+ _' {, ]/ P5 h5 r
                 }5 ^0 K+ C8 s; m/ z# ?/ c# c
                if(pvNames)SafeArrayDestroy(pvNames);
+ b& I6 v- Y5 i' p6 R) ?( R                 iEnumIdx++;
' a: D  D; H/ F% W               }2 g, B5 G0 W: H3 g& D0 m* o  h- j
           }
+ H4 \4 m: p. m6 t. i+ j) Z           if(pClassObject)pClassObject->Release();
" \% ]- w' D3 _4 d( l7 z         }
3 o- c+ S- i5 m, A5 Q& g        if(pEnumClassObject)pEnumClassObject->Release();
- X! s# ]& {# X- J: y" R- L4 d5 Y" a      }8 }7 H; w5 q# p% H$ _, F) }
    if(pWbemServices)pWbemServices->Release();8 o# U+ t/ t; p7 c  T: }
  }  ]8 W8 h- o. K3 g
  if(pWbemLocator)pWbemLocator->Release();6 Q0 t8 p+ b2 u) O
}1 J7 l6 c# l( _' a$ j
//---------------------------------------------------------------------------7 @9 h3 [* H, P3 M
! g6 z' r2 `% ~
// 通过 WIN32_bios 获取 BIOS 信息:* l4 y+ p- l8 @' `" a
void __fastcall TForm1::Button1Click(TObject *Sender)7 u8 t7 Y9 F3 ~" |/ _3 N6 I( S  r
{
7 X4 ~% A* e" h9 \9 a) M    Memo1->Lines->Add("================== [WIN32_bios] =================");7 b- B" u5 a1 j! n7 Y8 q
   GetWmiInfo(Memo1->Lines, "WIN32_bios");2 Q2 a2 E/ @* F
   Memo1->Lines->Add("");' Y+ a) g: {8 v4 B: e$ k/ W. {
}
2 I' q) h; T9 }
# e8 }- d8 i3 j: W5 z--------------------------------------------------------------------------------" Y+ Y# M0 F4 H+ E* h% {% l; C
1 `/ N! v! @/ s1 B# e% S
WMI 可以访问的信息类型有:- O( }3 z+ N( {$ B
  Win32_1394Controller
( ^! I$ T. l  N2 ?# r4 t* S   Win32_BaseBoard* J3 ^8 d/ c' m! `% |. v+ u5 M
  Win32_Battery0 ?( e8 L6 ^# }; g
  Win32_BIOS& h% J+ m$ p# C2 g5 }+ e
  Win32_Bus8 n3 V% _& v$ r- c7 G
  Win32_CacheMemory
9 ]7 c& N# ?: v   Win32_CDROMDrive
9 Q# I. Z( X( r9 i7 x3 T7 B   Win32_CurrentProbe
# T4 b0 C4 N  L# x   Win32_DesktopMonitor+ T2 {. X; e, J6 {. u; s5 A; X
  Win32_DeviceMemoryAddress: t5 i! u/ i& K1 i
  Win32_DiskDrive4 K: y+ r9 |; P6 v* Q/ p
  Win32_DisplayConfiguration; t8 ^* D/ ]6 P! E& ^* e
  Win32_DisplayControllerConfiguration" B6 I: }0 b1 G# C4 H) Z
  Win32_DMAChannel, l$ K8 h8 s0 B7 q
  Win32_Fan" Z' y& S8 H, @
  Win32_FloppyController
% x6 a9 F4 A9 r. ^4 f1 s   Win32_FloppyDrive# D) N9 j4 u# p3 `1 _2 K- P+ z* u
  Win32_HeatPipe7 E7 S7 w  u9 [3 r( [
  Win32_IDEController
. p& T+ ~. A( \6 M5 |. k4 `& f   Win32_InfraredDevice
  V/ b# y' j0 `& X$ K1 r9 A   Win32_IRQResource2 C/ a/ |+ @$ O  f- C
  Win32_Keyboard% y/ f7 T! w$ {$ _
  Win32_MemoryArray+ u1 x3 A, S1 E. n
  Win32_MemoryDevice
. {) p  D- l: o$ @   Win32_MotherboardDevice
& \7 s- l, O' n   Win32_NetworkAdapter
$ u4 A% ?- q  X, u   Win32_NetworkAdapterConfiguration# A2 m1 ~4 f. y/ Y
  Win32_OnBoardDevice& d7 u) f; J+ X; e: }/ F5 o
  Win32_ParallelPort
$ H, @% M$ Q: o4 e$ A/ y- N. n   Win32_PCMCIAController
! ]$ D) v. `! @) D! D$ H   Win32_PhysicalMemory
  k3 Y5 M6 [) o* N   Win32_PhysicalMemoryArray
* [# y3 f* P$ j* e   Win32_PnPEntity# q5 Z% d2 W5 x5 N6 n$ C3 g, k8 _
  Win32_PointingDevice5 l% M8 r; s! f
  Win32_PortableBattery
$ Q; H% I! i% B0 |   Win32_PortConnector) [/ R& s. X! O+ L0 ~8 i0 ~1 w
  Win32_PortResource
4 |7 d2 H  o  i5 S, x& m/ l   Win32_POTSModem
, j( O/ U, h# I" P   Win32_PowerManagementEvent, K( r3 D/ U- U% f
  Win32_Printer
; c! F/ ~  h3 y% W5 h* s. ]- a   Win32_PrinterConfiguration
+ l( v) c7 ~& Y+ D: x) l  A: W   Win32_PrintJob8 u6 i+ i% N4 _6 n% M
  Win32_Processor
! j9 J/ R" {- p3 {7 c8 _+ k   Win32_Refrigeration
# a: a. k; D3 [" J4 d! p   Win32_SerialPort
% \6 }3 |! e3 m) T! a# J1 O   Win32_SerialPortConfiguration
" E( m5 u8 E6 p. Q, x/ O. N/ \   Win32_SMBIOSMemory9 q; i3 J6 s  U$ y
  Win32_SoundDevice( a$ n' o6 q9 U& g/ t" C/ {
  Win32_SystemEnclosure
5 P& A5 }7 `$ u   Win32_SystemMemoryResource
* Q  B% y' {! t8 @: u- D   Win32_SystemSlot
* v4 q+ H- G( p* E( [. p   Win32_TapeDrive
9 ^$ ^7 P% }  j  ?0 b' q   Win32_TemperatureProbe
* w7 \: }+ H! B5 T- ]/ X( H# B6 l   Win32_UninterruptiblePowerSupply
7 d' N( F, Q4 o% m4 ]/ r3 R1 C  N   Win32_USBController. [& t+ q: w- ~" d. q( K7 ]
  Win32_VideoConfiguration
1 `! m6 Q; [/ {  o/ ~3 h. Z   Win32_VideoController
/ }+ e" j, [3 U' W   Win32_VoltageProbe5 g0 T+ X7 c. U. t6 g) O' l

& m% e& g+ Z: d8 g$ Q, r以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-6-20 01:53 , Processed in 0.035162 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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