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

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)- w1 _" c& k8 g" D
3 Q6 [* h" O6 d& k+ D9 r* b

5 N1 ?$ m0 E5 R1 K--------------------------------------------------------------------------------/ ~" G) j, A" J
WMI: Windows Management Instrumentation (Windows 管理工具)
8 D& ^9 D0 M; L$ Q* @. S& o8 n   通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。
5 p" a+ f7 P+ _, f% S   利用这个工具可以管理本地或客户端系统中几乎所有的信息。
9 d6 m: f- s5 v$ ^' m# W( K( w5 |   很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。
; Q6 m8 Y3 _# P# B  \( Y
  X, u* e0 w. S% l6 q6 s--------------------------------------------------------------------------------5 r" p% @& e$ H0 S" b
BCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面
2 D4 h  H) `7 O2 T, T, c/ u) J" {7 c3 ]8 m3 j$ B3 L
--------------------------------------------------------------------------------8 o6 \7 q! g- @: O! ^
① 初始化 COM 接口:
' x! |$ L/ u- S. y' n   访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。2 m* w$ k, s- k8 l( w
  这两个函数在 #include <comdef.h> 里面定义。
4 S+ x) T9 y, P: x
9 I2 P  s. D' o1 N4 {+ _7 ]② 获取访问 WMI 权限:, O2 e" @: C/ u$ w! r
  CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);
$ t; l/ n: s# I  ~   如果这个函数返回 S_OK 获取权限成功, 否则为失败。
! a" b, Y- P! O8 M+ v% r8 G3 V- Q9 q' W
③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:. |" C, T) j& F
  这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。. s  s! y' ^6 k+ |& |* F. R
/ ~! c7 x( G# t2 i/ H5 D
void GetWmiInfo(TStrings *lpList, WideString wsClass)$ Z) X1 l* R( N6 r
{$ W/ Z( C) |  I! g
  IWbemLocator *pWbemLocator = NULL;5 o2 L0 O/ R2 V2 Z
  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)4 F( L% f2 P+ i5 [2 w
  {
" P1 E3 Z# e! g7 E  Q0 `) Y     IWbemServices *pWbemServices = NULL;
# [9 q" n8 R$ t& @. c     WideString wsNamespace = (L"root\\cimv2");) s- Q' u1 y: S; I( j0 Q
    if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)
; l5 S$ t7 _, O- U- j      {& y6 ~2 r, T" z3 n2 ^/ v! l
       IEnumWbemClassObject *pEnumClassObject = NULL;; {6 I% P1 m3 Y* K' V+ p# z
       WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;
! {0 {# J- d) Q7 f" {- @0 y        if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)( d$ x4 q* H( O
        {: R0 v3 a6 F' D+ H  f& g
          IWbemClassObject *pClassObject = NULL;
* r$ T( P1 D: W+ p+ q4 T5 s           ULONG uCount = 1, uReturned;
7 m. ^5 d( D5 y: Z4 A/ J           if(pEnumClassObject->Reset() == S_OK)6 {/ K; {# V# [6 v0 ^9 D
           {4 u" [4 ^/ D: t" i$ C
             int iEnumIdx = 0;
" B: _- y. n7 E& q1 G# ~              while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)9 |: u4 k& F* y. l, w
              {% @- s1 G: K: c1 @
                lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");
4 q) ]0 Q! W; Y0 ?/ M3 J" b) N( Z8 w6 `# B4 k" m/ U+ |4 e8 X
                SAFEARRAY *pvNames = NULL;
% W4 q( G; j$ W: J                 if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)% O- V" }* f8 C: M' {5 a2 t' H: T
                 {
9 a: ?- n7 _* h& e, D) t1 p' r1 V                    long vbl, vbu;' H$ y3 r0 t; ]9 v4 [3 \+ w$ R5 K
                   SafeArrayGetLBound(pvNames, 1, &vbl);$ O0 P: i: k' ^, y5 v$ }
                   SafeArrayGetUBound(pvNames, 1, &vbu);
7 t- Y6 v- P7 C( p, o) M                    for(long idx=vbl; idx<=vbu; idx++)
3 W7 X1 H- ~6 r2 t                     {
6 B2 z/ n/ H2 ~% `! k2 C                       long aidx = idx;
5 e7 |% E9 c; T$ V& K% W                       wchar_t *wsName = 0;* m: U% M/ k, @
                      VARIANT vValue;
2 A! u! b$ I, h, }: @                       VariantInit(&vValue);9 ^% A* R5 {* }+ d+ i( O
                      SafeArrayGetElement(pvNames, &aidx, &wsName);6 w5 s! h. u) M! y9 {6 r
6 n& W5 n0 b+ f) m$ h
                      BSTR bs = SysAllocString(wsName);
/ [# ~/ I# r. a9 F4 S, L7 q0 N                       HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);
% ?$ b, J7 n$ E& I                       SysFreeString(bs);. a' t7 _" |0 q& B
' w2 @: G4 i. U) C& A
                      if(hRes == S_OK)1 K5 b5 x9 N* ~( i. ^" `! ^1 X
                       {
, W" x4 [( G& f3 L) [                          AnsiString s;: B$ I. n# m! T8 T" B+ R; Z7 R% p" V
                         Variant v = *(Variant*)&vValue;
' N+ k+ w& u9 d5 d/ Y& t                          if(v.IsArray())5 r4 w+ Q# ]# A7 x5 H( X
                          {1 H5 r0 V" i: I' L5 f
                            for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)* }7 h3 t# {) O
                             {
6 `5 X; G# y  t8 U4 H# d9 f                                Variant a = v.GetElement(i);
0 Y' m8 M: i& P4 |2 M                                if(!s.IsEmpty())
! r2 U2 V3 j* ]% I- \                                  s+=", ";6 w/ I  W1 w1 @$ l4 l5 p' s. R
                               s+=VarToStr(a);9 ^6 N) a0 W; n* V" t- k+ I
                             }
0 q: C1 C0 y9 g                           }7 r' S0 |# V7 E! @) C6 P
                         else
. i1 q+ M: _0 p# W                           {2 y! U) k* f  E4 d. Z+ U; V
                            s = VarToStr(v);
* b& a- u  Y6 G+ x                           }* O; w# M; L0 S# @  f0 O
                         lpList->Add(AnsiString(wsName)+"="+s);
7 g+ L5 F2 }6 v, _1 c$ b, c                        }0 i( @0 b& q) ~6 _0 {& v
9 V! i& f8 K* e; b' h+ \3 f  U
                      VariantClear(&vValue);
* P' t* A7 R( o' }; @0 ?( O7 x                       SysFreeString(wsName);! M5 P/ U& G) a1 G0 g
                    }/ e; g* p2 D; v/ A4 N& ?5 m2 \- H
                 }
$ Y& p8 L' u8 }" |& R6 d                 if(pvNames)SafeArrayDestroy(pvNames);
. S9 p7 D; l1 J' f$ {  G) v" A& f                 iEnumIdx++;0 ~4 d# C& H2 {; X
              }
' P4 h% @/ T1 @            }+ r" @! ~8 z4 z7 m0 ~
          if(pClassObject)pClassObject->Release();  T6 a" k# G/ }6 k( M- @. D9 G
        }) \5 |0 W6 K' v1 Q: G& G7 Y$ p
       if(pEnumClassObject)pEnumClassObject->Release();
( g+ K* m' P/ a$ f1 _% W      }
, r7 ]& q) `$ M: c5 l4 V     if(pWbemServices)pWbemServices->Release();
  d9 g& _' C! ]% v: K   }
9 z% N) k# L+ s. v" g  if(pWbemLocator)pWbemLocator->Release();
0 S3 X5 q- o3 \* J! i$ z4 Y}1 z/ E' d0 D, R  a. D
//---------------------------------------------------------------------------
: B4 K# {" U% G$ O2 ?1 X  l# C- Z& o  @# L* p
// 通过 WIN32_bios 获取 BIOS 信息:
, H+ F6 [! A  l* cvoid __fastcall TForm1::Button1Click(TObject *Sender)" U4 J- O% S  M5 O; Q8 U5 z
{: g: S6 Q4 n8 e& d: s3 X# A
   Memo1->Lines->Add("================== [WIN32_bios] =================");
' z+ v4 a8 y* f+ a: O0 t    GetWmiInfo(Memo1->Lines, "WIN32_bios");# L$ s5 [3 [% U
   Memo1->Lines->Add("");
9 E$ v6 `2 Q3 |2 q}  B6 g, }: `- [. N* C' @
& r, o; _+ x6 A2 r1 @% N
--------------------------------------------------------------------------------
8 m/ |0 U( \5 M. a% S7 M1 O$ [; H9 }; ~7 `7 O
WMI 可以访问的信息类型有:
. L2 G0 P1 F* `" h) p   Win32_1394Controller8 [  [3 {$ [; {9 F3 C
  Win32_BaseBoard" |* x4 K3 @1 o- ?  b2 D: u
  Win32_Battery, S7 K) E9 ^* V  j7 |. K
  Win32_BIOS
6 f# ]; y  I- G: c; f$ {  |2 K   Win32_Bus
; S+ v, C5 ^2 f8 [7 |4 _   Win32_CacheMemory
* p# H; l7 `5 a* p! {( D- S   Win32_CDROMDrive
( D% E& M) F4 Z2 U/ I7 G& [/ O   Win32_CurrentProbe
1 e# {: Z1 y2 D, y# r   Win32_DesktopMonitor: v/ N; ~+ r5 w( q
  Win32_DeviceMemoryAddress
$ m7 }, h6 `; ?   Win32_DiskDrive. A7 x7 O8 f  g/ e9 b9 s
  Win32_DisplayConfiguration. a* a! ]- F4 e2 [
  Win32_DisplayControllerConfiguration7 D4 O. Y" G" K. T
  Win32_DMAChannel
# U  S0 |9 }; Q   Win32_Fan3 a) C. _& {3 {# M9 ]
  Win32_FloppyController! M( e5 w- ?" o( m1 z
  Win32_FloppyDrive
% w/ l7 O" P' a7 C9 x5 }1 M  c6 R   Win32_HeatPipe: C$ n# {" v; D) o: c4 [/ T
  Win32_IDEController; U( w' ~3 i* f% Z3 P
  Win32_InfraredDevice, w6 G5 A, d! }! s+ O/ f
  Win32_IRQResource
+ v1 N) V. i0 ]. y8 @6 Y0 U/ E/ |( k   Win32_Keyboard
: \: l" i9 K8 [+ K; @/ _# r   Win32_MemoryArray8 a7 q0 m' B; Q9 T' I/ J+ H% K* ]
  Win32_MemoryDevice* k; A/ x2 C0 ^3 G# w
  Win32_MotherboardDevice. N" t) F* @, x: W- l( e) }
  Win32_NetworkAdapter0 K3 }! ~3 X1 o# M2 r% r
  Win32_NetworkAdapterConfiguration) a  K% C0 |; U8 ?. v7 }# ]1 X, G
  Win32_OnBoardDevice5 O- w% k- Z4 _2 C6 m
  Win32_ParallelPort* U5 z8 i0 s$ X5 U6 b! y
  Win32_PCMCIAController
0 F7 v' Q( d9 h! d! q' H! c& P4 A   Win32_PhysicalMemory5 _' _; t% ]  A6 |& G* |
  Win32_PhysicalMemoryArray
$ x9 t- T* T8 J2 O- W   Win32_PnPEntity& h7 D% G* B$ g# o) }" V
  Win32_PointingDevice
, n8 W! a* ]- W. \   Win32_PortableBattery  R, Y+ A6 u! V1 v6 V9 p( r6 ~6 g+ G
  Win32_PortConnector
" |  l" m5 q7 O/ d, t, B   Win32_PortResource2 m- C' f- w: F+ m# e: S: F7 B( A" b
  Win32_POTSModem
" l& z8 }0 k+ r* M/ o- M   Win32_PowerManagementEvent
: B' h& _# s( e. L   Win32_Printer: h+ E3 V# k# Q3 i/ k
  Win32_PrinterConfiguration% k2 a1 Q8 M5 j, u( ?
  Win32_PrintJob1 Y  F1 |, D7 D; W9 U- u
  Win32_Processor6 c- v/ h! @/ l6 [9 ~3 C
  Win32_Refrigeration
2 n6 j# N" A9 H" s" }4 N8 h   Win32_SerialPort
5 K- k. F, g2 b3 i/ W! h0 f; l   Win32_SerialPortConfiguration
6 o' J: f  e; Z   Win32_SMBIOSMemory
7 e4 t, b  I/ {' p2 r  i- O6 Z   Win32_SoundDevice6 s, d) `, O$ l! ~9 J6 X
  Win32_SystemEnclosure
3 E0 o' _) V5 F+ c7 Y   Win32_SystemMemoryResource8 |! {- V9 W: o) q- w% Z  \7 u
  Win32_SystemSlot' g' m! R) B! W+ H# P
  Win32_TapeDrive3 f/ T) M0 i% F: |/ Z8 \7 {- u6 S: q
  Win32_TemperatureProbe2 i. z' m5 g7 M: A
  Win32_UninterruptiblePowerSupply# E( S8 h+ S  u3 m# f9 @
  Win32_USBController6 S6 q, d( ?! V9 i; B
  Win32_VideoConfiguration6 s) d. U% m/ o0 |+ N3 d7 A/ T
  Win32_VideoController. [+ P4 T$ ?) s; y" B3 U# H
  Win32_VoltageProbe
/ I8 n# ^( [& j  p. \4 ^
7 J# I4 J1 q& T7 h( l以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-12-18 16:07 , Processed in 0.021522 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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