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

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)4 m, g( i# u3 v0 N. ^1 G
+ P# e( A' I* ?6 A& G
5 m9 f" I6 i' u6 {( l
--------------------------------------------------------------------------------- q$ c2 D2 }+ d" i5 m
WMI: Windows Management Instrumentation (Windows 管理工具)( x, G( \+ l0 h& |) m
  通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。 3 `& S2 J6 S* E; b
  利用这个工具可以管理本地或客户端系统中几乎所有的信息。9 l3 P7 b& E, L+ B! p( ]) Y
  很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。 5 \9 C: X6 f9 W8 E
. ^4 g! b6 R: c( r; I# O& q
--------------------------------------------------------------------------------7 T/ q6 s. j) i  P
BCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面) P. H" Z+ c# s/ l+ p% q7 G
+ E! ~' _( b" e! |
--------------------------------------------------------------------------------( S& }' B1 i7 A0 a
① 初始化 COM 接口:
# K- X' _  Z  F% ~0 R   访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。1 w" {) v3 h4 p" E$ F
  这两个函数在 #include <comdef.h> 里面定义。+ G% @: C' b* Z- k; m1 F
$ \0 V2 |9 w7 d7 P. g4 Z; R/ }
② 获取访问 WMI 权限:
6 _0 [. v0 g5 y6 W8 p   CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);
' r+ M, Q6 Q; l* ]   如果这个函数返回 S_OK 获取权限成功, 否则为失败。* I3 C' r. A) r3 ^' a! w- ~  P

% f- X7 z7 ~/ S) c③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:8 U9 i! E5 G; L, N. W" t
  这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。/ }' m/ t2 k7 a% B# _5 F$ k3 O& K

+ J! B! u- u, n# M! yvoid GetWmiInfo(TStrings *lpList, WideString wsClass): ^! S4 n/ `3 r& S
{4 S4 k3 |. H/ R8 z6 f
  IWbemLocator *pWbemLocator = NULL;
6 b0 Q5 q& F9 b1 j  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)" m* X, z. B, a: F4 e% ?' v
  {
  c. h3 y8 [- y* v4 O1 t     IWbemServices *pWbemServices = NULL;
: j6 D4 E6 I9 }: i1 A- I; O6 `     WideString wsNamespace = (L"root\\cimv2");: f) X* |/ {/ J# n0 p7 A
    if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)1 `& m; ~# c2 A8 G9 M  y
     {9 ^9 a2 U) D; @
       IEnumWbemClassObject *pEnumClassObject = NULL;
" [9 D8 w% N3 ]        WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;4 _) x' [' {3 G  ^6 j$ Y
       if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)" y6 o4 ^" h, u0 [$ z' a1 ?
        {
7 G7 w" v- c; H" \           IWbemClassObject *pClassObject = NULL;
" D3 I' W4 i5 H/ }3 j* q           ULONG uCount = 1, uReturned;
' e% f% ~1 S5 r4 e# _           if(pEnumClassObject->Reset() == S_OK)
4 G8 W2 Z. U$ w7 ?3 n            {
( q# o3 _2 B+ u( q4 \2 C' Q              int iEnumIdx = 0;( u) o6 y7 f" I8 D
             while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)
9 P/ ^/ D  J+ m* q               {
  _* d$ h  @2 j* r                 lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");( }2 ]" t  N& R2 q. [! o
. W' D/ R5 _' D1 r9 U$ }9 j
                SAFEARRAY *pvNames = NULL;/ E3 D, [$ @) F$ x6 [0 }
                if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)
1 l- x7 {' X2 n, v4 q                  {
4 k, ?- \, u( w" z                    long vbl, vbu;2 Y6 U' Z* \5 \7 Y8 W" p. Z. E& J7 \
                   SafeArrayGetLBound(pvNames, 1, &vbl);
0 X! j& L2 q5 C! M( s                    SafeArrayGetUBound(pvNames, 1, &vbu);
$ I% V$ f" t3 P2 e4 x* ~                    for(long idx=vbl; idx<=vbu; idx++)
4 P7 g) p, \! i7 V% Z' U1 W                     {1 h/ i7 U5 g0 g. D+ i' w
                      long aidx = idx;8 T  |9 f( [1 {0 O( ?8 a' E
                      wchar_t *wsName = 0;
  w" }  a/ }  y- ?+ s- Z% t                       VARIANT vValue;
( N+ d+ w, Q7 p6 @* u8 V$ P                       VariantInit(&vValue);
  E: ~$ Q, a* Y. }) \( \3 [4 c                       SafeArrayGetElement(pvNames, &aidx, &wsName);/ j1 h: B( ]( J9 o

. h  [9 F1 O; n! x                       BSTR bs = SysAllocString(wsName);
( Z" W1 a. O' W7 h+ s' s                       HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);- G( |8 E8 U( [
                      SysFreeString(bs);6 }7 B4 Y. H. B8 R. C2 t; I
3 f" [! ^$ ~" r, J3 {7 G6 J# \3 E* s
                      if(hRes == S_OK)
% M( u2 C# ?6 z6 {0 {                        {
0 e6 V5 k8 q; Z6 C  b9 @' l                          AnsiString s;, E9 q9 }9 d; R8 y: s7 o
                         Variant v = *(Variant*)&vValue;# B; A5 y5 p7 s! h6 A2 s; C! m
                         if(v.IsArray())" Z7 H( O8 K3 a3 Z! B1 p
                          {
! W4 Q% {4 e. G9 S# v* }- x" B                             for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)7 C( V4 K2 e3 ?, v& T7 ^
                             {# |- d5 K: @5 _- Q+ G' y1 n4 r
                               Variant a = v.GetElement(i);
) v9 k2 x9 D0 D; o$ T                                if(!s.IsEmpty())0 h) I/ x/ q& a0 E: o% _& L
                                 s+=", ";7 K! v7 d8 w- H& {6 F/ d
                               s+=VarToStr(a);
2 U; A& P# L3 r% q, i7 M! J                              }
& C1 {9 V6 {% e$ b- h9 r' h. V                           }
- i9 V+ g; n4 D4 O- ~                          else
! K9 n0 W2 F/ y( `5 l                           {* E4 E' z& A) k# [8 E
                            s = VarToStr(v);9 J# N. N4 m/ B; s9 \8 S7 t% x- _
                          }
9 S2 s: C6 R8 V" l) F                          lpList->Add(AnsiString(wsName)+"="+s);- W; v# T8 Z$ a9 N! Y! B: {, T
                       }
& ^& W$ Z" ^+ R2 x7 V
; j7 V4 F0 n$ H                       VariantClear(&vValue);
9 W" I7 M; [5 i/ |4 N                       SysFreeString(wsName);
& F0 @$ @' Q- z: _6 C! r: }* \                     }
6 \* n: q) {/ s) F6 A4 N                  }
4 f% G1 R  `: }4 b' F& ?, p% C                 if(pvNames)SafeArrayDestroy(pvNames);6 C; |! R$ g7 R* n- t+ n$ W% e
                iEnumIdx++;
: D2 s, _  ?7 L& i, _               }* ]1 A7 W- Y; D
           }1 N' R$ j! b; j6 V1 O
          if(pClassObject)pClassObject->Release();( n) Z6 t; _; k  t6 a4 J
        }
9 b& P! T2 s: N0 z3 G- F        if(pEnumClassObject)pEnumClassObject->Release();0 u/ I+ b% h- ^* |3 x9 `: L
     }+ T3 P" N1 f* h
    if(pWbemServices)pWbemServices->Release();8 _' \6 _; F# D6 c
  }; l1 r" F. u7 G2 L2 Y
  if(pWbemLocator)pWbemLocator->Release();
; P3 N" D2 Z# M9 ^}
& E; f7 _) t4 B//---------------------------------------------------------------------------
8 N  K, ~- ^  o0 [0 g6 a6 s$ o2 Z1 u
# o( L4 A7 d' {, U: r  }7 R// 通过 WIN32_bios 获取 BIOS 信息:: B7 L' [6 S, F+ I
void __fastcall TForm1::Button1Click(TObject *Sender): l. l- J; [$ g1 A* h4 W
{
  T4 r& V! Y# Y3 d5 I# [; Z    Memo1->Lines->Add("================== [WIN32_bios] =================");
* I, F/ x7 |% z& y9 G) `    GetWmiInfo(Memo1->Lines, "WIN32_bios");  M7 L, I" Y2 \
   Memo1->Lines->Add("");1 {/ g: M6 D, g& b2 c
}
1 k0 S* i9 x  ^0 r4 F2 S, y. e) n3 o4 f0 h/ |6 b' j
--------------------------------------------------------------------------------
. x, L$ k9 }3 D) X# n
# o4 _8 Y& A4 |. ]0 [, w& GWMI 可以访问的信息类型有:: k* i% b+ G' B! J( W
  Win32_1394Controller
0 E. s  I3 p- c! X8 ]   Win32_BaseBoard5 K7 d' `/ [$ u+ F- h  A
  Win32_Battery
, o5 _( k$ ~  `) Q% h9 _   Win32_BIOS
' G* \- ?9 a# Q   Win32_Bus
- r* N6 g4 N  O7 W5 v   Win32_CacheMemory
3 E% q) {* `* h+ P   Win32_CDROMDrive
+ g1 i% z5 X2 P$ G! M6 I  j8 B  A   Win32_CurrentProbe5 c# |0 |1 ^  e* c/ k  U. d
  Win32_DesktopMonitor% [: V* A/ q  ^% |9 w5 J5 f& y! L
  Win32_DeviceMemoryAddress
( Q4 Q( i: I3 P* T% m2 l7 e2 R   Win32_DiskDrive( k! J4 m4 j3 C1 N+ u& f5 u/ [2 }
  Win32_DisplayConfiguration
  C& ^+ x- q$ I( }1 c3 A/ Y   Win32_DisplayControllerConfiguration
0 f* P; z4 n# @   Win32_DMAChannel
$ G! n4 \+ P/ }& Z" x1 U   Win32_Fan
9 L/ ?& M# a+ Y% w( b   Win32_FloppyController
4 U5 B; B. x/ O) N" z   Win32_FloppyDrive1 r4 A& W! Z0 h% Z+ Z8 S
  Win32_HeatPipe( |5 w+ w. {& l* K6 f7 O
  Win32_IDEController5 ?2 y8 S: T- e2 D9 Q) l) R! A( a
  Win32_InfraredDevice( f& a- F5 h0 y1 [6 n( d
  Win32_IRQResource$ X& O4 Z  |0 i8 o9 A9 t
  Win32_Keyboard; E  K6 D2 O& P  Z! o
  Win32_MemoryArray
" w& t' j6 x$ u% [7 t  K   Win32_MemoryDevice
& I# K' g. A( Q4 w+ F; q( P   Win32_MotherboardDevice
8 r4 G) P3 B" f& f, p5 K   Win32_NetworkAdapter9 `' k: H0 u, h$ b' d
  Win32_NetworkAdapterConfiguration' y, P0 q6 L# Z4 W: N& c" y( g
  Win32_OnBoardDevice
; x( [: z7 v$ w1 f   Win32_ParallelPort. d4 Q& ^& h/ R
  Win32_PCMCIAController
6 y6 d) j8 v' h8 \, K   Win32_PhysicalMemory
$ w$ y7 c5 V; M- }3 Y+ z   Win32_PhysicalMemoryArray
, |. \) }$ n8 X" ~   Win32_PnPEntity$ f1 B: L7 D6 y! L+ H
  Win32_PointingDevice) |( G' a5 X1 \7 |7 g% p
  Win32_PortableBattery
+ E  a7 E7 [* I   Win32_PortConnector
- Z* _4 C' n2 G( C4 @   Win32_PortResource
' `: i" [& @7 L7 I# k4 f# H, b   Win32_POTSModem
9 S! F8 V0 g' V4 ]   Win32_PowerManagementEvent, [) h: X/ f$ R/ D" {$ i
  Win32_Printer6 f0 c8 ~( a7 g1 X. Z
  Win32_PrinterConfiguration
) V: i; Q* J' ^   Win32_PrintJob
2 |5 A/ ?+ C0 X1 u   Win32_Processor3 I) J/ [3 q' Y; {/ {
  Win32_Refrigeration
! n' I$ U8 `+ ~: R   Win32_SerialPort! D) [3 D7 N  W9 D
  Win32_SerialPortConfiguration
( K# m$ V$ C- z5 M8 n   Win32_SMBIOSMemory
6 T8 v1 l# ^6 p   Win32_SoundDevice+ h% d5 c& ~3 Y" N( X9 J5 V
  Win32_SystemEnclosure
' ^- V; V8 ~& w8 d7 }6 o  T   Win32_SystemMemoryResource
, m& \4 i- X) C   Win32_SystemSlot
) k1 T0 Q5 g. V& [( d' ~   Win32_TapeDrive; |' Y9 P9 }& B  {
  Win32_TemperatureProbe/ l0 F) w& p0 N. n
  Win32_UninterruptiblePowerSupply
& d7 l4 Y, Y9 P- A0 i   Win32_USBController6 o$ N9 e  Q! D5 c% C
  Win32_VideoConfiguration; f. y# P0 J! n: j6 q7 f+ L; `
  Win32_VideoController3 D" F6 \- D+ x9 p' Z8 A, f
  Win32_VoltageProbe: O  H! a+ C8 L4 D- a) j2 d/ }
5 Y  @; B6 |6 I) g- |) s
以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-11-19 19:16 , Processed in 0.018225 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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