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

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)
, |, T$ L( C$ ?
4 D$ e- d: W9 l2 q9 W$ @$ e; W. D$ T- g+ A8 ~
--------------------------------------------------------------------------------
8 J- z! z1 v2 M8 k& Y4 O3 iWMI: Windows Management Instrumentation (Windows 管理工具)
9 _/ b% d9 }9 F$ ?- i3 l1 j. M$ l   通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。 & _* k0 ~1 K+ E- t
  利用这个工具可以管理本地或客户端系统中几乎所有的信息。
. R8 a& N/ o  f& J: T   很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。
5 Y8 q% F: t0 h% d3 x5 l  A2 {1 l4 D1 q2 \, X3 x3 j: I/ L
--------------------------------------------------------------------------------
) L  q! }+ p( o  E( D. V& d. G. R8 @BCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面
* t8 }$ p) @; V; _6 o  S. p" t/ r, I- l' S- C
--------------------------------------------------------------------------------
* k* j! U& H* K& E8 f! ^) a9 K* D① 初始化 COM 接口:6 B# S& W/ t7 Q, K7 J6 D% ]
  访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。. w, _/ g7 b/ z# X# n
  这两个函数在 #include <comdef.h> 里面定义。6 n1 T" g0 N( E' m+ I( ]
$ |) _* D3 G! C# R7 g) [, u
② 获取访问 WMI 权限:
6 ~9 V5 B' `( n8 \   CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);
) X* l) X" l# D9 R9 m: u" X, J5 {; F   如果这个函数返回 S_OK 获取权限成功, 否则为失败。2 Q, H: G' \& @6 L( F
& W. d4 Q6 @9 N' ?! @' T
③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:- n4 _2 R* r- V  p
  这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。
/ ]3 f( @5 I0 g# D% v2 q
8 M2 T; t$ }; ^void GetWmiInfo(TStrings *lpList, WideString wsClass)5 U6 P3 E1 Q# V, c
{1 b. {' y3 C9 l) y+ E( X0 ~
  IWbemLocator *pWbemLocator = NULL;& _, ?$ B$ k4 t: C/ B
  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK). e7 K9 Z# C, J8 _: ~( \% c9 `
  {4 w. ^/ h4 p( }" V& P3 @
    IWbemServices *pWbemServices = NULL;
  O; r/ [9 u( M. m     WideString wsNamespace = (L"root\\cimv2");; w( S; k( [& X- L. |5 J. x
    if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)0 t( R" V9 b  ]& f1 ~( X
     {
% j6 K# M" l1 V$ L1 N        IEnumWbemClassObject *pEnumClassObject = NULL;( `) N9 f6 A; K; p2 n, `6 w4 B
       WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;
5 Z' a, z) z$ |& w2 L        if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)
3 g3 K" ?2 f2 n         {
, d) n1 O& D( f# W' ?           IWbemClassObject *pClassObject = NULL;
) K) t3 x/ o+ j0 m: `. w8 M# q# D1 j           ULONG uCount = 1, uReturned;
, k: H5 C1 Y( w4 _           if(pEnumClassObject->Reset() == S_OK)
8 ^" r( m. i4 ^! H- v            {
3 T, A9 s2 b  r. h9 W              int iEnumIdx = 0;
* l- q9 J2 b5 S* t! E              while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)
, ?* D0 a0 }: k& A( S0 P! c4 e               {
# A' L2 M* z' b                 lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");1 c" \" `5 l# z: C) y! F! c

% }# P% a& w- W                 SAFEARRAY *pvNames = NULL;
# m! I8 }3 H' F9 q                 if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)( O" X( J5 v1 A: l) x7 F0 H: A
                 {
/ d/ U7 v( ~+ S7 k2 ]- p' {                    long vbl, vbu;
: ^* S  o8 Z& h  T$ l$ N7 K0 S                    SafeArrayGetLBound(pvNames, 1, &vbl);
- ?, P" J5 ~& Z1 p; S+ }& H                    SafeArrayGetUBound(pvNames, 1, &vbu);% H2 [6 o7 \0 m* [
                   for(long idx=vbl; idx<=vbu; idx++)
5 ]+ x- d( L1 x# {+ p% Z& p" \                     {
3 g+ O5 b% @6 K8 |, Y5 ~  c                       long aidx = idx;' O2 _6 `  z+ n9 I
                      wchar_t *wsName = 0;
& ?! l/ X2 @( z% I4 T                       VARIANT vValue;. f( o9 n( t6 `8 H" ]0 d' B
                      VariantInit(&vValue);8 b" R' L! G+ o9 W5 H7 p7 w
                      SafeArrayGetElement(pvNames, &aidx, &wsName);1 E) j; f, q5 U7 F

# r+ F6 D0 m5 S, h                       BSTR bs = SysAllocString(wsName);/ O- T. _( V5 i1 S9 G" P, `: l
                      HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);/ }& Z8 P" W' T1 b) O) S9 [
                      SysFreeString(bs);! }) H7 ?! o# h. F

6 v' K/ w0 t! g$ i) ^# W5 c% W( C                       if(hRes == S_OK)
+ h: @2 z* a0 K0 S) p, b1 K                        {: o; J& G5 P+ D6 G
                         AnsiString s;
3 X6 Q9 A* E2 M# _1 ?                          Variant v = *(Variant*)&vValue;! z" d: z/ z9 h9 B+ ?
                         if(v.IsArray())6 g2 s: x* [! h' K, n8 N
                          {
! ^0 |4 ?# f/ `+ y                             for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)
" K5 L3 u: F& G6 d4 }                              {0 C5 U+ l  H5 J8 W) z0 M7 g9 p
                               Variant a = v.GetElement(i);) r0 m* }5 C3 E! f7 A
                               if(!s.IsEmpty())
3 d2 y0 X- w) T; Y" c- N# Q                                  s+=", ";
" I  T' }6 p2 W1 ^: w2 _6 v                                s+=VarToStr(a);
. J* q/ s' j- Y6 D0 a$ q" H' E                              }
5 I1 }( v% Z8 x- [8 t                           }5 N8 c3 Q: E3 e
                         else) b/ I* H: @. x/ J
                          {
* o/ k7 c4 X  q3 _2 ]# t                             s = VarToStr(v);5 @2 V6 Q' S* D3 B9 t+ O
                          }/ y+ C/ ], F) g
                         lpList->Add(AnsiString(wsName)+"="+s);
7 G4 x% \0 a& J3 C6 E! A( F                        }- C5 m0 |6 n0 f; w+ S: u

5 c# P5 s* |$ a$ A1 i                       VariantClear(&vValue);9 M* z7 ~3 u8 s, {" C2 k
                      SysFreeString(wsName);& }' a, x( Z* l
                    }5 m+ j' Z8 O7 S6 ?* y' c( T+ l
                 }
2 A  s! r1 M6 j) d9 h  B; K                 if(pvNames)SafeArrayDestroy(pvNames);
' Z) l9 N5 X( a: P. R4 t& [                 iEnumIdx++;
/ V! j$ m+ b& G               }! g% I+ W) Q$ G# K' i
           }. \$ X! h, x" m5 K" I
          if(pClassObject)pClassObject->Release();( r- K9 V1 O3 ]: N
        }0 h2 f5 e7 ~" j  @; K; Z
       if(pEnumClassObject)pEnumClassObject->Release();
! r" Y6 a4 E1 [9 }3 Z3 M      }
7 s6 S7 T! V' ^4 m6 d0 V     if(pWbemServices)pWbemServices->Release();" c2 d. j- s* h; h) b
  }
$ T* g6 f) E/ p  if(pWbemLocator)pWbemLocator->Release();
: J  S1 q, _, j}
8 J/ F% o  N( B1 T) e9 y//---------------------------------------------------------------------------
; O( x5 `" i, ]6 n" K$ Q! D8 r4 t( T& d- M7 o# y4 N2 e
// 通过 WIN32_bios 获取 BIOS 信息:
" Z1 ^5 _+ f% m8 Zvoid __fastcall TForm1::Button1Click(TObject *Sender)4 R$ J( G3 U! b! {% \
{5 X  c& C- ?* N0 N
   Memo1->Lines->Add("================== [WIN32_bios] =================");
' V6 e9 Q6 a' ]8 ~0 `    GetWmiInfo(Memo1->Lines, "WIN32_bios");
- L* q  b" Y* K5 s6 X' I    Memo1->Lines->Add("");
  P7 J! Z+ j4 s) A}
- T! C0 s% x/ b2 m. h4 ^5 @4 y2 q+ _8 ^7 e* z
--------------------------------------------------------------------------------0 z' x5 V5 n; s. X. X

7 G: X- D9 T% N/ g7 L4 IWMI 可以访问的信息类型有:
8 f+ a5 T, r: Z- I   Win32_1394Controller
1 R' o. o) c# s, M6 C" k) E   Win32_BaseBoard+ H2 P2 n$ b# u' Z  l* i0 Q
  Win32_Battery
3 q- ^( d9 L% \) B; I1 O   Win32_BIOS( Y0 g* s3 l* U2 c0 f! v
  Win32_Bus& ]3 e  W$ x3 N% _& L
  Win32_CacheMemory
0 D0 N: r. C- ]! c; P* B9 B   Win32_CDROMDrive! T0 z3 V" i7 N  m  [. p
  Win32_CurrentProbe  H, I" T' @% Q1 W; f' y
  Win32_DesktopMonitor: ]" l& v# f- M+ X9 c& y$ r
  Win32_DeviceMemoryAddress
5 {; x! ^# M4 f( {2 l- y) ^3 ~   Win32_DiskDrive* E0 z8 e7 l# P5 k5 ^
  Win32_DisplayConfiguration
' B. h/ S( U$ u" j3 \" S4 m   Win32_DisplayControllerConfiguration
5 f! j" T: }1 b1 w& G5 ?/ ~   Win32_DMAChannel. i. S* j" ^# W. s* a; o
  Win32_Fan' i; i) _4 ^5 n6 |3 M
  Win32_FloppyController
7 j0 N$ d8 f; Y% B   Win32_FloppyDrive- ~/ ^1 ~# d1 R2 V
  Win32_HeatPipe# C( f  P0 {2 s8 e# M' t2 F" V
  Win32_IDEController0 j* p3 h; E. o! }& x
  Win32_InfraredDevice
4 O% y- A: T. A: v# c  S/ g4 l   Win32_IRQResource
1 G8 Q- J8 a1 w+ K* @+ v& c5 p   Win32_Keyboard
2 _& E  |, o7 O) L   Win32_MemoryArray  a7 k0 M$ t. e! C- n
  Win32_MemoryDevice- N$ j6 M" Z" W  @
  Win32_MotherboardDevice# y0 c: P0 [$ s5 X
  Win32_NetworkAdapter1 n$ u) k7 P: y9 A$ Q+ n
  Win32_NetworkAdapterConfiguration
0 T9 |9 b* w; n1 c$ e/ A8 e   Win32_OnBoardDevice
3 {/ g2 L( c3 V9 t) a4 ?0 L   Win32_ParallelPort
5 L; P' `3 m! F# r. E: e   Win32_PCMCIAController9 i5 F! t( c) h, I. G' j
  Win32_PhysicalMemory
- u; n- U0 q: u: ?3 ^9 K- \   Win32_PhysicalMemoryArray9 V: }2 A6 [# O: |  ~5 `
  Win32_PnPEntity; T. i) B: C) n0 D
  Win32_PointingDevice
4 M: j  F/ s$ H( p! l   Win32_PortableBattery
4 |5 [) m5 E: b  A   Win32_PortConnector: z7 q, {* `' s/ h. i& ^$ Z
  Win32_PortResource
0 h6 B" n" e( G7 F% D. Y   Win32_POTSModem
- {$ W$ J$ \7 p0 g; ?1 }   Win32_PowerManagementEvent
% v2 E( T4 r' K   Win32_Printer' K6 `# K, c! {2 u/ c5 ?
  Win32_PrinterConfiguration
& q/ K8 [; ~2 |2 m% m   Win32_PrintJob
' q7 Y! V$ b0 F! Z( c/ x& |   Win32_Processor
# v3 W' L- w) r* I   Win32_Refrigeration
8 ]4 l# [* g# ]( x2 e   Win32_SerialPort) ]% k* M% |- L/ t! p6 @  T
  Win32_SerialPortConfiguration# c/ K! t4 P( E2 E4 p9 Q. ?
  Win32_SMBIOSMemory, a4 p9 M8 ~" `0 k0 ?
  Win32_SoundDevice8 N5 S$ m. f; U  B" Q* L& o
  Win32_SystemEnclosure5 k/ S/ h2 t4 T1 p- D$ r0 l6 W$ g
  Win32_SystemMemoryResource& R9 ]1 x9 `2 |6 X
  Win32_SystemSlot
8 X- i& e1 F; h   Win32_TapeDrive
$ V, O8 M! x( z( i# \   Win32_TemperatureProbe( K3 \  u9 C( o  k8 @+ J2 j0 G1 s
  Win32_UninterruptiblePowerSupply
. Y# J, _- k  H! d" M   Win32_USBController: Z& }& A7 W, Z4 I+ E) `6 `9 a
  Win32_VideoConfiguration2 ~" l7 |4 p: W: J4 J
  Win32_VideoController2 t7 s  j" T* r
  Win32_VoltageProbe, h3 c/ X9 r' j9 r3 {& c# w

% b. [: ~9 }8 Y4 F4 p5 U: m' p* c以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-1-18 02:36 , Processed in 0.019312 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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