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

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)/ X4 S8 j) a4 L: U. V

- ^6 q0 C7 K9 ~- j* O3 ^3 z8 n: E
; p* U) P. A* h! ?& h, |--------------------------------------------------------------------------------
6 L9 Z0 P& j' @0 G+ a$ ?. L+ tWMI: Windows Management Instrumentation (Windows 管理工具)
% _1 a/ B$ T' Z# }. F   通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。   e5 R( D0 F1 h
  利用这个工具可以管理本地或客户端系统中几乎所有的信息。5 z: u# o7 @! N* m7 r
  很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。
: q/ F9 y9 H/ A- I. g
$ Q  a1 K4 }6 t+ n9 \' o+ X, L4 j--------------------------------------------------------------------------------( G& _; W0 P* x
BCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面
* M1 Q% u3 a( w% W
6 p# H+ d/ R) @; `5 n! I1 x& z--------------------------------------------------------------------------------# ]( F( z4 O9 n
① 初始化 COM 接口:7 S% g" J* h' R' `/ K
  访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。" I% T# o- u3 y, i" c8 J
  这两个函数在 #include <comdef.h> 里面定义。
! P' r% x3 `9 }% M1 n, j/ V2 J1 \# I0 m- S7 c+ n
② 获取访问 WMI 权限:
& O2 f1 a9 t  C   CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);% s4 `9 g: n6 ]9 ?
  如果这个函数返回 S_OK 获取权限成功, 否则为失败。1 q1 x; q$ W6 ]' K$ x) t' k4 P
) a1 @8 E( W& i6 j
③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:
: `! H" l1 r% v) o% i   这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。
; W2 b& G- _4 ]" s5 V6 e! v- \3 l/ J1 S- r" j& j/ s5 \; m3 c8 r
void GetWmiInfo(TStrings *lpList, WideString wsClass)  ^7 c; x7 ?" R- l4 A4 j
{* D8 k  p  o; ?# |
  IWbemLocator *pWbemLocator = NULL;
' M6 {- l: T+ ]0 @+ }+ v  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)+ R5 ?, P3 @. K! H  Z( m* q
  {
  Z5 k/ F# ^* U& T+ [0 s     IWbemServices *pWbemServices = NULL;  c5 i( {% x( j% G! Q% I
    WideString wsNamespace = (L"root\\cimv2");2 }4 {7 @  @/ j4 h0 \
    if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)
# o' J- _! m5 \( y7 }2 S, }$ e7 S      {
% I$ i- B2 f& X! R6 U" a; |& W' d5 J# |        IEnumWbemClassObject *pEnumClassObject = NULL;; x. e* d0 i& W" S' t
       WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;  M) [( J. b- a1 H) M* z
       if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)4 Y( J8 N* F$ N' s; [1 ^0 X
        {
' B' d# w& B+ h6 b1 q- L0 J0 r           IWbemClassObject *pClassObject = NULL;; f# N- k$ P7 P. B
          ULONG uCount = 1, uReturned;7 o3 w) S+ k# @: ~1 ?
          if(pEnumClassObject->Reset() == S_OK)" U4 P* \# }/ d! M
           {2 ?0 f: e% w- Z1 T" b2 K
             int iEnumIdx = 0;) C3 Z9 T/ `3 p: s* e# f
             while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)) Y7 ~$ ^) d! Y1 q0 ~7 \
              {8 P+ S& K/ q' H9 [: ~
                lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");
" @# @- a8 B1 _6 D2 j3 t$ @) F& g( K, u" t2 j
                SAFEARRAY *pvNames = NULL;
5 h1 M2 ]8 v+ \                 if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)
5 \, @$ C/ b) @; X                  {
& D" I. p2 J1 ^0 f                    long vbl, vbu;, G; l6 ]5 n3 l5 K( H; Z' J
                   SafeArrayGetLBound(pvNames, 1, &vbl);
; k# ^( m5 k7 P; c" X                    SafeArrayGetUBound(pvNames, 1, &vbu);
" x7 L. }1 K7 W" m, x  m1 ~                    for(long idx=vbl; idx<=vbu; idx++)
( x( a. @: |) ~1 @                     {0 _/ p0 B7 D5 o8 d* t9 h; o- a! J3 T
                      long aidx = idx;8 w4 G, b# o6 m" v% ], o
                      wchar_t *wsName = 0;
( v! ?( m, b' {- I, z( r$ W                       VARIANT vValue;" F$ z! \( h$ u
                      VariantInit(&vValue);( n7 @, d6 A6 `, J9 p0 c
                      SafeArrayGetElement(pvNames, &aidx, &wsName);
9 u% P+ o, b1 v# A3 D1 y2 h
3 K: z- ^0 [# p) f4 |) ^7 {0 s                       BSTR bs = SysAllocString(wsName);
1 x# J2 h& F) r: L" d; }: Z                       HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);) e9 O" {$ G- c
                      SysFreeString(bs);/ u* x. w9 D0 \( p; i5 U: u3 E
' \2 x' z# G5 j4 p
                      if(hRes == S_OK)% S' s+ ]0 h; Y$ E
                       {
2 x7 t2 E* O+ z( t: c                          AnsiString s;& h% N/ n- v  k& `9 U) e3 |
                         Variant v = *(Variant*)&vValue;
$ v# Y4 h) T8 T/ c# d! _. B                          if(v.IsArray())2 x/ a1 \9 y2 x) Q
                          {) [9 z0 O4 e& q, ~' a
                            for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++). Y: h0 V7 d5 B' ?+ @7 k
                             {6 ^# r# a/ R% G4 S  H- _
                               Variant a = v.GetElement(i);9 y" y* R: E/ E2 [& t) B
                               if(!s.IsEmpty())  D. V7 o& _1 n3 ~# v
                                 s+=", ";
4 `5 @( M& a2 O                                s+=VarToStr(a);
5 M; v1 K5 n4 ?2 _) Q% ~4 i5 ]0 z                              }
7 ~- a1 Y) F5 V& Z* F                           }
. c9 P4 ~/ ^9 b* H& N                          else
" j" L, K8 @* W+ i( |; s                           {
8 O' }' a# }3 a( M2 E" L- M                             s = VarToStr(v);
7 G: l, G7 T1 l; z                           }
' w, k! A6 [" e" M                          lpList->Add(AnsiString(wsName)+"="+s);5 s# N* M- ?" A" o, f1 ]8 E
                       }1 l* Z) I5 ?5 Q9 I) U* z' `

9 }( O+ z7 y: U3 K' f5 z                       VariantClear(&vValue);
& t% }, D$ f  M% z3 T% F! Y$ f5 a                       SysFreeString(wsName);6 B) r9 a& Y: P7 y
                    }5 E1 U6 z8 `8 F
                 }
1 N! C( K4 C9 `  ~( i' q                 if(pvNames)SafeArrayDestroy(pvNames);
) d" V. f# K7 p2 M                 iEnumIdx++;* g4 A/ C/ j9 x! J/ R! S, h
              }' y8 p: s  V$ B! k
           }
! e9 s& i) x7 i+ o# j5 L6 }           if(pClassObject)pClassObject->Release();
% [& s: T: Q% {6 H( Z         }6 G* m0 e8 W! i2 T8 o* o% Y
       if(pEnumClassObject)pEnumClassObject->Release();
  S+ O7 Y# E- ]8 ^- T      }
5 I# D& |$ B; G2 I  l     if(pWbemServices)pWbemServices->Release();7 Z  [9 r/ ?7 i4 L6 r
  }- ~" t0 T. R: C1 B& c2 W+ L& W
  if(pWbemLocator)pWbemLocator->Release();. Q4 [3 i2 s8 V6 `8 U7 I) m
}
% b( J6 I$ T; I* a+ z//---------------------------------------------------------------------------
. ?% j/ m$ h' @8 \) R( S9 ]! B+ _0 [0 |: [7 B4 _7 X7 c) s! j9 H5 d4 I
// 通过 WIN32_bios 获取 BIOS 信息:5 f( y# u% Z& |: c1 z0 j
void __fastcall TForm1::Button1Click(TObject *Sender)
2 p/ j4 |' h) @{) a; W' q7 \& O  y
   Memo1->Lines->Add("================== [WIN32_bios] =================");
& I! y/ e- a' k) w  ]    GetWmiInfo(Memo1->Lines, "WIN32_bios");
/ z5 [6 P9 h- g; W    Memo1->Lines->Add("");
; Q% s% Y/ f) t}
# x- X. h% p  n) O- y% A7 B. w3 G
. D$ R+ B, x0 H! K--------------------------------------------------------------------------------
$ i$ k) F( y+ z5 G/ h5 x  {' Y' h* o
: S7 G  k9 s( AWMI 可以访问的信息类型有:  l, X* o+ F2 H" g" V
  Win32_1394Controller
( J, G: y8 X  s% @   Win32_BaseBoard
: [- [4 s. ?! n: l  I3 H   Win32_Battery# J# W: P. Y7 c9 g: E0 ^4 i
  Win32_BIOS
0 }: B: v- E$ D; _   Win32_Bus" Y0 _% ?: P) m( K
  Win32_CacheMemory
* q3 ?$ V) r+ N* m/ B% h8 J0 A( i3 X   Win32_CDROMDrive+ \1 s- s8 B* _* `+ ~
  Win32_CurrentProbe
$ v; T3 v) C" J0 J& _   Win32_DesktopMonitor( {6 v6 V8 b) o( H$ o- [4 v! E5 u- x# N
  Win32_DeviceMemoryAddress( I6 I* _; R+ J/ e" {
  Win32_DiskDrive4 c  J; b# ?. z( }" ^& l; h, \2 o
  Win32_DisplayConfiguration
. C  t. w/ r  y1 t   Win32_DisplayControllerConfiguration
7 l# I2 V& w9 u% Q" k   Win32_DMAChannel1 U6 V& a4 z* h3 B
  Win32_Fan8 r" \1 }! b4 l/ s. ]' s3 [
  Win32_FloppyController* H3 m* x' J( A" E1 e/ A1 p
  Win32_FloppyDrive8 K' M! \4 w2 b& B
  Win32_HeatPipe
7 X1 v/ Z9 m$ ?& s4 g   Win32_IDEController
/ {! m, k3 D* W0 M! O( C; ?( w6 W  P   Win32_InfraredDevice- j" b. E6 `6 @0 A5 u
  Win32_IRQResource. F9 x# A( z3 e" x; M' \
  Win32_Keyboard  X' Z9 P/ T" Q7 G" R
  Win32_MemoryArray
5 S5 r/ _$ Z8 X: N7 F/ S   Win32_MemoryDevice
* T2 a: i7 q, w. W   Win32_MotherboardDevice+ h( `7 M  z3 R7 D1 F- ~
  Win32_NetworkAdapter
8 T* G6 E$ w1 S9 j$ c   Win32_NetworkAdapterConfiguration
9 _9 q5 a1 }+ @2 j" {5 n4 H   Win32_OnBoardDevice
5 Y6 O+ S/ \$ q" [. F( e# `! y( i1 h   Win32_ParallelPort; }3 C/ \4 B) `, N4 A! I
  Win32_PCMCIAController* j! s& Y  a3 t7 r
  Win32_PhysicalMemory, Y$ C9 F! M/ t6 ^# f& N5 u# u. g
  Win32_PhysicalMemoryArray
- F/ t7 C9 y( H" |2 ~   Win32_PnPEntity$ U2 Y% s. d# V  g; k
  Win32_PointingDevice
- d4 v7 w! _$ B% A   Win32_PortableBattery& N8 Y3 l* u$ O, M' E4 P
  Win32_PortConnector
" h0 ^; ]1 v) ?   Win32_PortResource
# h& Y! p. ?7 f, D  o1 L; h   Win32_POTSModem; S0 p3 ?% G; {6 n" V
  Win32_PowerManagementEvent. Y5 b" }% q7 G
  Win32_Printer
4 j. ~! q% p2 A# l: i   Win32_PrinterConfiguration
& V; y  ~$ ~  }: h' n& \+ [   Win32_PrintJob
7 V1 m4 P2 n5 Y( O- o   Win32_Processor
  r* _$ i: L# w% l5 c9 H   Win32_Refrigeration
5 Z1 r& i7 L3 W- b0 g# {   Win32_SerialPort
" ]4 s* A# D3 G' n; a8 K   Win32_SerialPortConfiguration. K! W! F7 _8 I0 O3 H# M
  Win32_SMBIOSMemory* G2 ]) C$ t, K- T' q" l7 w! b: y; F
  Win32_SoundDevice
8 n  K, \, E! }; a3 D" X8 ^/ K4 Z   Win32_SystemEnclosure% z4 t7 |; I( l9 K5 w4 h! ~  d% Z
  Win32_SystemMemoryResource
* M" K+ c+ A. x" D   Win32_SystemSlot
. {" u: i/ f0 L) g2 x# S+ I. Q   Win32_TapeDrive. B! }; F' ?; _
  Win32_TemperatureProbe
3 W5 h) z/ G' m' m! ^" ?& [0 b   Win32_UninterruptiblePowerSupply
2 K* f/ X) Y4 `9 v   Win32_USBController
6 B$ j3 G& W' {  ]) u( z   Win32_VideoConfiguration2 ^7 _1 x4 j" M; L- O# h. a. S
  Win32_VideoController- g( {* U( b* M" w
  Win32_VoltageProbe
6 J3 d& v/ F! ]* t
: m9 `% X7 I! e- x& D) m6 g9 N4 ?以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-1-14 19:54 , Processed in 0.021102 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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