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

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)
) o7 L# Y, y7 X& A& Q' m! f/ j: m
* F' M. d* x1 @8 L! X7 K8 p
( R2 \9 a+ Y# Q, D  b--------------------------------------------------------------------------------' b+ P" G2 l) P8 G" K0 O8 \3 c# o
WMI: Windows Management Instrumentation (Windows 管理工具)
! `2 p! r7 h" u; P' N   通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。
- Q+ V+ `) ~* A- E# ]' J: A   利用这个工具可以管理本地或客户端系统中几乎所有的信息。9 P; A( n% w* {/ U$ x) G$ x4 h( Y
  很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。 ' c9 Q% Y( A7 v  M$ w
+ ^* P! {! d. q+ B2 H* l
--------------------------------------------------------------------------------
! p6 k$ i/ h/ [' I; Y$ e% x: GBCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面" H* e& O, V* T
' J& }9 V- X: E  Z- n
--------------------------------------------------------------------------------
+ F& f8 Q; V' ~. s" g% ]① 初始化 COM 接口:
) ^$ `2 A+ I* r/ Y$ N7 X8 B1 A   访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。; X( M' n4 Z. E" s2 U: S
  这两个函数在 #include <comdef.h> 里面定义。2 |) T; S- a: _0 m' c

5 @6 Z, y' e4 Y② 获取访问 WMI 权限:% |3 r7 u" h# ~; ~+ h
  CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);' t( R$ h/ H/ s2 {3 U
  如果这个函数返回 S_OK 获取权限成功, 否则为失败。3 Z9 w1 z# h3 G
/ P) R0 D3 x! p. S0 I, A
③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:, ?2 J5 f. p" q! r8 W0 Y
  这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。
. c1 d: G: Z7 Y; z6 ?% V- _; [9 `% h  ]) ~: j
void GetWmiInfo(TStrings *lpList, WideString wsClass)
8 c2 q8 `$ J3 Y1 f1 {$ E{
7 x) N1 b- k+ A( f  IWbemLocator *pWbemLocator = NULL;4 u7 k6 d  R$ n4 W" e
  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)4 r* \0 V7 N8 K/ i, z
  {
* P, {/ X' F  ]+ w     IWbemServices *pWbemServices = NULL;9 h3 P* I& n/ W0 r1 ?5 _9 `
    WideString wsNamespace = (L"root\\cimv2");
# h/ i& w. F3 [, Q: m     if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)4 c. m) k9 b% E- M
     {- m6 Z& F1 L% l
       IEnumWbemClassObject *pEnumClassObject = NULL;  b: a2 j8 w# R' P- O
       WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;
: _  U' V# |) q; `) w# b        if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)$ m+ Z" r- K& X) S: s: Z4 u
        {
1 R5 R! [" v0 ~4 ?5 k           IWbemClassObject *pClassObject = NULL;
! i; D; D$ O8 A' m8 U           ULONG uCount = 1, uReturned;, v. I2 o( M2 G7 {6 G  q# Y+ y
          if(pEnumClassObject->Reset() == S_OK)4 B/ W! p  Y( {! j  {: Z! z# P. i
           {
( k$ x- k' V3 C! ?5 Y              int iEnumIdx = 0;8 j( [; x9 i- F- c6 f+ Q
             while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)
2 m3 b  q) |1 l+ O: G& Y+ n               {5 Z) ~( r# t, X6 p2 a5 X' `, _
                lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");
# A1 K( ^5 C+ t: y, ^  ^
, N' B) A8 W2 _( N' k0 W0 @5 f                 SAFEARRAY *pvNames = NULL;
8 Y1 R% s. K: r. m                 if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)
9 N; H7 y8 ?6 y3 c* T6 v& ?                  {0 X9 W# ]# H# h! Y$ v4 k
                   long vbl, vbu;
/ \5 k; J! @' b6 ]; k- b2 l0 _                    SafeArrayGetLBound(pvNames, 1, &vbl);) A4 u* s! P  J& p3 g% ~
                   SafeArrayGetUBound(pvNames, 1, &vbu);5 W1 L0 L+ M% g- g3 U( @8 I
                   for(long idx=vbl; idx<=vbu; idx++)- E4 z  V6 `1 h) L# S: C, Z$ N
                    {
( [5 Y0 ?8 p1 {3 P) u, B5 X                       long aidx = idx;
2 v) Y/ [4 O8 L; s$ @- D3 g) `                       wchar_t *wsName = 0;
  m5 _  u5 ?6 f, B                       VARIANT vValue;
3 T: y9 L  H& p* R3 ^                       VariantInit(&vValue);
6 U/ S* w* F' p/ {                       SafeArrayGetElement(pvNames, &aidx, &wsName);
) `. E4 s4 ?8 |5 e+ u
2 R  c2 N0 H! g2 f9 B. c                       BSTR bs = SysAllocString(wsName);
$ q( k6 C1 M( N* M) S                       HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);
9 ]' c+ L" ?. K4 W' f2 @7 \& ]                       SysFreeString(bs);
4 V) F; S* V, y2 G$ y7 a# j7 w$ f3 M, Y
                      if(hRes == S_OK)+ P) p: b! [) a" @% |1 e
                       {, k! o. d$ c+ |$ i5 e$ t
                         AnsiString s;
' z* l/ z7 i. A7 {- H                          Variant v = *(Variant*)&vValue;
$ S& u, k) g. k9 @                          if(v.IsArray())& v: I7 j8 K' M: A
                          {5 ~/ _; }8 I* j- P9 C6 J
                            for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)
% S. _& b' t, l2 F5 l+ ^                              {
0 C. G1 _* B2 Q& F3 [8 A4 o2 J                                Variant a = v.GetElement(i);1 X: k% T' Z3 w5 o! @
                               if(!s.IsEmpty())' y9 c9 H9 O) _
                                 s+=", ";* r. ?* _. s% V( R" |& V  o/ M
                               s+=VarToStr(a);
* y# t% u* y- B! v                              }5 v6 f" a( ]+ D8 Z  v
                          }3 R2 C9 w4 y) n
                         else
& D$ |) y( B- P5 s5 s- O7 T                           {
" k" ^7 ~: W4 J' ?( j                             s = VarToStr(v);. }5 K9 v+ H& o
                          }5 p0 i1 _) H% Y
                         lpList->Add(AnsiString(wsName)+"="+s);
6 A9 o3 X% x7 y& ]' K7 @                        }
' a3 g1 u$ `( r7 }3 j. E! ^2 c* t5 K, J
                      VariantClear(&vValue);* E- k3 ^- C. d$ ~$ f4 Z: i
                      SysFreeString(wsName);
- A5 U+ V* U+ d0 A; |4 B1 W* y                     }
, M7 |5 p0 J# ?- |- Z& m                  }
* J) M% Z# i% B8 T                 if(pvNames)SafeArrayDestroy(pvNames);
% i% F  ~* |1 ^$ _: ~" T2 Y                 iEnumIdx++;
  l! V7 Q6 u0 u& N3 K: O               }
9 i$ a  Z% @8 @            }  D1 B8 k! s, ^/ l
          if(pClassObject)pClassObject->Release();0 s& s1 _: J1 {1 o3 a. u/ R/ K
        }0 P5 f+ J1 m5 e' S
       if(pEnumClassObject)pEnumClassObject->Release();
) O7 o7 k' z$ b5 Z- S      }5 w: I9 V, o/ N; C" N) _7 v5 t
    if(pWbemServices)pWbemServices->Release();! _# a1 F6 o0 Z7 c6 ~" |' v
  }5 w7 `( m  T* I4 Y  w* l- `
  if(pWbemLocator)pWbemLocator->Release();( R1 d# K. M: T. \# k4 j
}
* n. y, j+ L# {//---------------------------------------------------------------------------1 L6 m; ^6 K- ?
  e% o5 a. T9 x
// 通过 WIN32_bios 获取 BIOS 信息:1 d  d9 p7 C* `4 @) e+ b% X
void __fastcall TForm1::Button1Click(TObject *Sender)7 w! D  N1 |# w* O" f2 X0 f3 t2 _  ]
{
! O+ f# A+ X0 o    Memo1->Lines->Add("================== [WIN32_bios] =================");, e3 Z0 Y2 k8 `1 S: Z( l1 c# J# n1 N
   GetWmiInfo(Memo1->Lines, "WIN32_bios");
, w9 Y4 g( I3 j5 T    Memo1->Lines->Add("");% P* i6 a  w  S% a! m: M- X
}
$ U7 f( z# a9 A& g, r6 w( ^' h/ j* Q5 c4 E4 v. a
--------------------------------------------------------------------------------
5 ~3 n7 L# H" S7 C  i% F! l* v8 B' N- l7 h
WMI 可以访问的信息类型有:
# f$ p: U) ^% z) P   Win32_1394Controller( v$ A$ i) A* l% l  Z- l2 X5 I
  Win32_BaseBoard
" Y1 t: T6 T1 N$ i5 a) G* }   Win32_Battery/ i8 {# ?" C* S' e
  Win32_BIOS( i4 i1 j6 I$ Q2 s2 ?
  Win32_Bus
* {& f" n$ s9 {2 p9 F9 Y   Win32_CacheMemory
# w' |) x0 \( h% B/ j. E1 a& O   Win32_CDROMDrive
7 Y: v2 Z3 T# l% y6 ]9 ?  c9 ]* q   Win32_CurrentProbe+ k% u6 I+ i- U, e' i. ]
  Win32_DesktopMonitor1 B4 C. S0 Q' n7 e7 N$ Z7 Z7 `  L
  Win32_DeviceMemoryAddress
1 j# B! r8 I- `% Q# [   Win32_DiskDrive
- N  ~7 M* X. o) |   Win32_DisplayConfiguration7 B( K" ]( F' g1 o
  Win32_DisplayControllerConfiguration
8 F4 g+ e0 P% k' r   Win32_DMAChannel4 m- ~6 G" `8 ~+ V0 f$ A! z
  Win32_Fan
) ]' |0 E: g  p; i   Win32_FloppyController' C1 K4 k/ q: ~5 h( s
  Win32_FloppyDrive6 ~8 D; F6 v3 r
  Win32_HeatPipe
, ?0 T9 p8 g) Y$ X0 _   Win32_IDEController
+ k" K6 ~$ }. p' `   Win32_InfraredDevice& r% b: |' P( k
  Win32_IRQResource
* z0 ?3 w7 |# B' }# U+ T% r   Win32_Keyboard) l9 ?: j# h& y/ u
  Win32_MemoryArray( O* A) S: u1 X) G% o) B
  Win32_MemoryDevice# i$ D; M, J- d1 s& Y
  Win32_MotherboardDevice+ Z1 }% q; l: U# _. `
  Win32_NetworkAdapter! k$ m- D/ A- d) f
  Win32_NetworkAdapterConfiguration* X' Q! o! |* M+ a7 V5 I& c8 _$ I
  Win32_OnBoardDevice
$ P7 i" U- P+ _6 m3 M! C. j   Win32_ParallelPort
, ?$ w: n5 A: \% G0 `5 y   Win32_PCMCIAController
) a& U( `0 D/ o' n  ]2 \   Win32_PhysicalMemory
) G; j5 V1 V# a   Win32_PhysicalMemoryArray; {" n8 u0 M; ~% a' a
  Win32_PnPEntity
$ Y+ Z) l' v" B0 s   Win32_PointingDevice
5 i, \' l3 Y# I" Z   Win32_PortableBattery& k0 a" p  |) d4 L9 c
  Win32_PortConnector
. G  d. a( A2 j5 q) r6 o. e2 I, G# z   Win32_PortResource3 \( G9 N3 ^5 C
  Win32_POTSModem
5 i6 a+ y' {; D9 {# q3 E4 I   Win32_PowerManagementEvent
( D6 X" Z; z5 A" ^   Win32_Printer
% V+ _, y% w+ G3 A* h   Win32_PrinterConfiguration- x/ M' X; p% _) a! S: B# _* G
  Win32_PrintJob0 m. d, t( P/ Q
  Win32_Processor
5 X& p. q- ]/ Q/ `   Win32_Refrigeration, T8 a( _2 j4 O; J& R2 W3 r* d- y
  Win32_SerialPort
9 M$ j+ n0 W/ n8 t; u: w1 F   Win32_SerialPortConfiguration
! d1 i& M% Z, d7 w. g   Win32_SMBIOSMemory6 {6 ]  p5 _7 @* Y3 k, {
  Win32_SoundDevice
& a. ?# y( X$ B% i3 ~/ ^# Q   Win32_SystemEnclosure/ Y# ]6 h& q* D, I; {! n
  Win32_SystemMemoryResource
  v: D! \7 B3 n& w' c   Win32_SystemSlot
$ H3 n! a+ `% U, Q" d   Win32_TapeDrive/ d9 C0 m- P6 K5 E( M+ [
  Win32_TemperatureProbe6 u3 Y$ f& @% {4 N. D
  Win32_UninterruptiblePowerSupply
' T2 \" N$ b( R4 T' u   Win32_USBController) p" r( V( F4 u+ R5 u0 b
  Win32_VideoConfiguration
. c& C/ B+ ~/ o6 `9 o7 K* B# ^2 P7 H   Win32_VideoController9 g; c0 ~, F) E6 f. |  `
  Win32_VoltageProbe
4 J6 J9 ?2 S* N9 s
7 z8 i5 ~# A* D7 l以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-1-31 17:00 , Processed in 0.021764 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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