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

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)
+ ~) i& k! d3 h8 ^: `6 Y; [& `. {: B+ a
& U1 E6 L0 s! u3 f
--------------------------------------------------------------------------------
" {7 l* N) u4 ~WMI: Windows Management Instrumentation (Windows 管理工具)0 g7 \" R% m, A
  通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。
, Z: \9 o: Q) |, F   利用这个工具可以管理本地或客户端系统中几乎所有的信息。
% n, M! D: x: L! ^/ e! w   很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。
( W* c3 h# t9 A4 B
4 }' n  l$ I% [7 o- G! V& P--------------------------------------------------------------------------------) a; |* r& J' V8 s& W
BCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面6 N& ]' c2 p  n( [* A- f
2 E: F6 g, K0 O1 @8 c
--------------------------------------------------------------------------------5 f) f) ?4 U3 ^
① 初始化 COM 接口:7 D& N5 L' Z8 u, L* E
  访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。. _4 o5 t/ H+ N/ ?7 ?% Q
  这两个函数在 #include <comdef.h> 里面定义。
" s3 U6 T+ T' ]$ k* r
' F6 l8 X& B' V+ ?② 获取访问 WMI 权限:$ T6 t  X8 f# }
  CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);' [; i5 k1 [0 q2 \& S
  如果这个函数返回 S_OK 获取权限成功, 否则为失败。
, b. T5 G7 v: W+ ]( l! l' n# g$ c' U, C, w' a, B3 J2 q
③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:
% P- z. T; r) ~% I% A   这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。
+ i: z# i2 N# i1 B9 x0 J3 u6 J5 Y" w; P: d4 b
void GetWmiInfo(TStrings *lpList, WideString wsClass). P7 S$ R5 r' v1 O
{" w+ v3 q5 G0 Y0 |3 N* O) Q
  IWbemLocator *pWbemLocator = NULL;( |- e0 a  |' I6 ^& h& F4 V
  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)
3 A+ P" d1 O& Y& K1 @* N% Z5 A   {  C: [0 L2 b8 V2 c, v
    IWbemServices *pWbemServices = NULL;
, T4 P9 e9 s. G0 Q: n+ u- L% p; [     WideString wsNamespace = (L"root\\cimv2");; J5 L, y0 R" Z' O7 }- \. y4 b
    if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)
" U1 K- e( M7 |+ C( R      {
" V6 J' G( r) f        IEnumWbemClassObject *pEnumClassObject = NULL;1 l/ \- x/ w0 E% S( Q1 a: t" [
       WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;
6 ]) O7 r% @4 @; Y. H3 I$ v- X        if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)
  |* T# l5 U8 V1 ?! U4 `  q7 _         {3 Z/ c7 P/ {# [3 U  G3 \) Y+ h: {
          IWbemClassObject *pClassObject = NULL;  o& j$ o  ]. B% ]5 G
          ULONG uCount = 1, uReturned;+ D& \, j2 i5 b8 B
          if(pEnumClassObject->Reset() == S_OK)8 P& U: ?7 d3 H  @4 o9 u& t
           {
% S2 w% {( Y8 ?; p" c6 j* n              int iEnumIdx = 0;7 p$ m& Z& D/ C: U
             while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)
, \; _: a! L. U3 H0 [4 p, q               {4 p( D" Z# V+ g8 z
                lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");
& }/ d, n1 g* d: j& W  \, O6 t* L! w. G: R
                SAFEARRAY *pvNames = NULL;7 J3 x, N) d5 M, U0 v! ]7 H1 L
                if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)
1 {8 O" ^: {4 I                  {. C1 \' d5 O& X" `' b
                   long vbl, vbu;
/ ~) @9 i- ?- n1 t% o: _                    SafeArrayGetLBound(pvNames, 1, &vbl);
/ B% z8 p4 G; m9 b- c5 f: Y+ S3 P                    SafeArrayGetUBound(pvNames, 1, &vbu);4 R& W' o) R1 K5 V9 N+ h/ x
                   for(long idx=vbl; idx<=vbu; idx++)
% k  x" q. I- k/ _7 t) w5 x1 T                     {9 l8 U) Y2 n, h. J7 ]% x/ {
                      long aidx = idx;
/ P. ~' N3 d. P' b7 W                       wchar_t *wsName = 0;, R4 c7 o( Z1 _7 d
                      VARIANT vValue;
4 f; S* s; l& f$ w# w: I                       VariantInit(&vValue);( c" B; L- b8 h$ w1 B
                      SafeArrayGetElement(pvNames, &aidx, &wsName);7 C: r4 a/ ?# L6 V3 }# g

% e- P8 D0 W  ^3 r/ J; ^$ c5 j# K                       BSTR bs = SysAllocString(wsName);5 ]. O4 J: g. G: e
                      HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);
; e. M+ j( O" a( ^; A. v/ d                       SysFreeString(bs);* P  |6 L9 i% U+ h" K

: C. n& D7 h& K# ~4 ^$ Q. y                       if(hRes == S_OK)
; t5 U8 {* y# m4 O                        {+ X" e3 g% N0 Z& ?
                         AnsiString s;4 @8 e' f/ b4 O" k- e
                         Variant v = *(Variant*)&vValue;
  V; S7 E; I9 B1 n; l& M2 i                          if(v.IsArray())* b( B* V5 P4 V( F' S4 P% B4 G
                          {
/ X0 e0 m: }6 I$ h; r                             for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)! Y) u$ P  ^/ p7 _0 h4 A0 t( U5 x' i
                             {% t, a5 ]+ ^0 c$ b
                               Variant a = v.GetElement(i);' ^' \- j0 D7 o, P. y+ s+ \
                               if(!s.IsEmpty()); ?9 M0 o& Z/ I+ _
                                 s+=", ";7 I7 u% P9 X0 P" H- l  `  k
                               s+=VarToStr(a);
+ M2 {5 {/ O& `8 a; i" P                              }  F- s+ `" i. b- F2 h* p( w: t
                          }: H( c4 V7 {% i; U
                         else
7 H$ V: U7 F, O) b                           {
3 j* A! Q' d, p2 Q                             s = VarToStr(v);1 X5 V- ]" Q& @6 V, }( E0 {
                          }
/ S9 N/ ?3 Q; c4 I6 L, c                          lpList->Add(AnsiString(wsName)+"="+s);
8 E; k7 ]; r+ w; j" @                        }
4 h' ^1 I) v. P
& E- t& p# ?6 b. I* ?                       VariantClear(&vValue);
2 I4 r1 B% p- a5 V. }                       SysFreeString(wsName);
4 {/ T" ^0 A. K: G, H                     }
/ |! s9 N4 o! @                  }
. R1 Q$ t$ ~! x1 R. G0 }                 if(pvNames)SafeArrayDestroy(pvNames);: ~7 z+ E7 f( U
                iEnumIdx++;# H7 r6 Y  w0 G" U3 ^; k
              }* N  {( i; d& D- u* h5 W
           }4 t# _' _. d3 }- v
          if(pClassObject)pClassObject->Release();/ H5 k2 Y* E# k5 ^8 e# E
        }
7 _+ K3 x3 C) M$ ?: a/ F( c# t        if(pEnumClassObject)pEnumClassObject->Release();
. y$ m1 r; n/ R* E  m' W! ]      }
. C( V- |- n% _+ B     if(pWbemServices)pWbemServices->Release();
" H  ^, q% S+ A4 f% f  d% T+ v6 D   }
' q2 s9 ^4 o; c/ p% o& V1 _; U2 q  if(pWbemLocator)pWbemLocator->Release();
5 k: \! Z& p& J' B5 u) g5 _}, K. o, Y2 {1 W% h  a8 n
//---------------------------------------------------------------------------
, v7 o: K! K. n3 I3 D0 D' s& x- U
7 I! u4 r" t1 `" n// 通过 WIN32_bios 获取 BIOS 信息:
* n" y# m2 L- n3 N( ~1 s% cvoid __fastcall TForm1::Button1Click(TObject *Sender)9 O  ~3 U) Q# H8 t% R
{
8 O5 N+ K% o& ~, ^6 G7 L- a    Memo1->Lines->Add("================== [WIN32_bios] =================");- _  f1 Y8 y4 t+ x6 {
   GetWmiInfo(Memo1->Lines, "WIN32_bios");. q1 z7 u6 w; H' y8 R
   Memo1->Lines->Add("");
6 I6 g3 h% e: n# p}# g; U5 q5 ]* ^8 v. h/ H
" C: ^+ q; n8 E/ U8 M# V5 I. ?* R1 k
--------------------------------------------------------------------------------# w) P* Z1 C0 _5 c5 d

) s& Y' D2 ~+ L) N8 \' XWMI 可以访问的信息类型有:
) h/ G7 }/ ~) l$ _% F1 X' F- N   Win32_1394Controller
9 F' S5 q' C6 R$ B   Win32_BaseBoard! T! G' B. u# T0 v
  Win32_Battery) S% T$ J( ?- B# Q* w/ S! u
  Win32_BIOS
0 J8 I0 ]5 ]3 A0 N! G4 s& b4 l" N   Win32_Bus
/ }- ?; w( [) q7 f7 f3 m   Win32_CacheMemory
& A+ |0 U# `2 d1 `+ Q' m   Win32_CDROMDrive4 Y2 ~# W' B! H; P
  Win32_CurrentProbe
4 U- o& ?7 ~2 ~& q   Win32_DesktopMonitor
  \( j7 K) \+ X% ]8 V   Win32_DeviceMemoryAddress% @6 r: Q: x9 z& Q
  Win32_DiskDrive* e+ w5 h# F! T# B+ p/ T/ h% B
  Win32_DisplayConfiguration
# y) F1 f% w, I$ u0 F' A   Win32_DisplayControllerConfiguration
# C( I5 L1 ~1 k. ~% d   Win32_DMAChannel
: t: a6 z4 K  c; {   Win32_Fan! z8 k! R5 ?/ p
  Win32_FloppyController
+ j, A7 ?$ R+ v3 x5 N7 L' {   Win32_FloppyDrive
! }8 h5 W) w+ }1 j   Win32_HeatPipe$ u& b& n3 @/ ]* \5 P1 ~
  Win32_IDEController
3 T. e; ~! x8 @% G, j) J& l  q5 _   Win32_InfraredDevice
7 l, K5 F1 u. K( h9 K% t   Win32_IRQResource
5 r2 K8 ^8 t2 z7 r   Win32_Keyboard
- u% P, y8 Y8 Z  \2 ~   Win32_MemoryArray
3 I9 X' ^4 n% |/ w. K   Win32_MemoryDevice
  R' k& R- e1 A" D" L0 W! }, O   Win32_MotherboardDevice: W( C/ j% h6 L* B
  Win32_NetworkAdapter
0 A2 X8 v" S0 `/ t: D5 @   Win32_NetworkAdapterConfiguration$ F! N3 B" w* M: ^# U
  Win32_OnBoardDevice
8 n4 e& k/ H# u7 @4 I! v+ X, N   Win32_ParallelPort
% A& W9 I4 g% }+ O% K4 Z   Win32_PCMCIAController. g; C9 J  a2 k3 k0 e
  Win32_PhysicalMemory
5 \& s" y8 D) c3 H   Win32_PhysicalMemoryArray
/ M$ S) @( g7 v6 J0 y. P   Win32_PnPEntity: V: F+ N% J" v) g& }
  Win32_PointingDevice; }9 b* }+ A/ s. Y- S
  Win32_PortableBattery+ }+ E# E8 s6 [4 q- p! @$ x
  Win32_PortConnector
5 \' {/ y- \0 V7 a* u  x   Win32_PortResource
0 Q4 o# V( M+ Z" o; @& _   Win32_POTSModem
1 ?% r4 ~2 @$ {" z. `! C, X* {! k   Win32_PowerManagementEvent
3 j6 J# f5 Y5 O3 y" B   Win32_Printer" R3 ^' @, c0 L6 x5 H0 g3 @( z; ^
  Win32_PrinterConfiguration
- S# G" {* Y/ Q  u   Win32_PrintJob/ p1 _- K2 y: j$ T& u
  Win32_Processor; R/ M, @( M, \4 J: d1 k
  Win32_Refrigeration  l* X" F1 v, l# U8 {
  Win32_SerialPort5 C, x- d5 s4 P+ N
  Win32_SerialPortConfiguration6 e. W& s4 ~; [- q! p( A* [0 L
  Win32_SMBIOSMemory
6 D( `0 X8 e2 X0 P+ I   Win32_SoundDevice
" E8 p0 D( ?* I" |/ w  }+ @   Win32_SystemEnclosure" @! s5 i, v4 B  D0 R1 y
  Win32_SystemMemoryResource* e# N) a. c" s! Z! L/ x, l5 f) ~
  Win32_SystemSlot$ n+ U1 W% w- y( o1 w5 D
  Win32_TapeDrive7 _8 `  ]/ C9 b2 j& D$ G) T
  Win32_TemperatureProbe( o$ x! P% m/ x; W$ Z5 g7 [5 a
  Win32_UninterruptiblePowerSupply
4 F& r9 z' V) a( ?9 r   Win32_USBController/ ?1 Q8 e5 x: D0 `: x
  Win32_VideoConfiguration
8 w  X! T3 f/ d$ `% Q9 ^1 k   Win32_VideoController
: a2 j2 F; |, ?5 I; K. B4 x3 M) W; O   Win32_VoltageProbe# t5 }. _# S/ E$ V5 Z

* v7 p$ m5 V8 i2 S  ^/ w以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-2-9 06:45 , Processed in 0.018671 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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