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

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)& |4 L5 I" R( |1 \+ ^
8 U! b% l, `* L1 s- v$ q9 w" d
% _$ ~' w4 p3 L) g0 h
--------------------------------------------------------------------------------) B2 d0 n# X' p- z* @! S' l' D% k
WMI: Windows Management Instrumentation (Windows 管理工具)
1 E& l9 S" z7 r& y, _: l1 T9 k   通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。
; L6 j1 l0 g% D- z+ T   利用这个工具可以管理本地或客户端系统中几乎所有的信息。
( u9 X& c, l# N- {/ n   很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。
- P, A9 N1 P( x( Z6 z8 z! v# e3 ~% c) c
--------------------------------------------------------------------------------0 V/ S/ O  h% L& d) c0 E
BCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面
3 Z2 F2 q  I! C# ~- g5 Y# Y5 H& A0 @( A. }# Z# f! K8 g* z
--------------------------------------------------------------------------------
& J0 r! T7 T+ F9 D8 {① 初始化 COM 接口:$ ^9 t3 [$ k: l) T+ s8 ?/ U
  访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。$ O8 q7 u% G5 z0 j  N. A* O+ p
  这两个函数在 #include <comdef.h> 里面定义。7 o2 E; `) b+ _+ j5 B
0 h3 X2 W8 c/ ]4 b5 P
② 获取访问 WMI 权限:6 S) s& Z" `9 _3 g" Q" j
  CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);* g0 s, K+ K" ?4 W( k4 ]& Y2 D" U/ D* k
  如果这个函数返回 S_OK 获取权限成功, 否则为失败。$ W' |% n* F8 k" g
& b! W4 k' M* [8 V  m8 u
③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:  ]. t" b: H4 N% H2 i2 l* X
  这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。
  ]( g& Q# `5 v2 k, @3 o. h( m( {) N: D
void GetWmiInfo(TStrings *lpList, WideString wsClass)7 s% V" W- e) E+ m8 k
{
4 k0 ?5 h" |9 [& b) T) T8 e  IWbemLocator *pWbemLocator = NULL;) v8 a' z' I6 k9 J
  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)
6 l3 E( }% v  a/ d0 j   {1 B, h6 y4 l$ G8 ^3 E7 R
    IWbemServices *pWbemServices = NULL;
- A3 {% `$ ~( k- Y     WideString wsNamespace = (L"root\\cimv2");0 b8 P  ~2 k7 [
    if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)6 Y. c7 n3 {. b9 r+ `* J2 v" {
     {
* Y/ L8 l0 g# I1 w6 S! L' p        IEnumWbemClassObject *pEnumClassObject = NULL;
& y4 ~3 O- D9 A0 R; N        WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;
0 |0 f" r6 Q7 l: F" B6 t        if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)& \/ z) J# B/ G/ l& S6 X2 g
        {, K8 s8 n% }' j" M
          IWbemClassObject *pClassObject = NULL;
5 q8 g! E6 g' P           ULONG uCount = 1, uReturned;+ [9 M" `3 }: y) V4 j$ B
          if(pEnumClassObject->Reset() == S_OK)
4 a# l% A: Q3 ~' K1 |9 j            {
! [) |  \% @. @& ]$ V              int iEnumIdx = 0;
* G; [0 p3 c5 g( ^              while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)5 Z( p7 B& d) d) r4 g
              {, `; e" X8 n4 ?* p# ?
                lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");7 x* X2 R% J( T" Q( [1 s, p& @
0 l1 ~; o" a9 U& Q
                SAFEARRAY *pvNames = NULL;
; |  W) _: [7 [0 q2 ?* Z# }                 if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)
! `; K" H, d1 U% {' _' D& L1 h                  {
; [9 U6 y. D; z0 V. r6 l                    long vbl, vbu;
' O+ l. G2 Q3 P                    SafeArrayGetLBound(pvNames, 1, &vbl);3 o0 E; V+ ^1 s" v6 A
                   SafeArrayGetUBound(pvNames, 1, &vbu);, i1 r# V* G4 f/ p  \
                   for(long idx=vbl; idx<=vbu; idx++)
* p- {2 O. `3 Z1 W3 c                     {7 y. w3 n! w2 G6 {" k, {  E( X
                      long aidx = idx;1 C" a  `- T8 G! U
                      wchar_t *wsName = 0;
4 Y" c" L0 U( {4 C4 u                       VARIANT vValue;
3 g3 i8 w6 j* P% e' v! W- ~# g! H                       VariantInit(&vValue);: r! j2 Z2 ?5 Q1 z' N% v
                      SafeArrayGetElement(pvNames, &aidx, &wsName);$ v' W8 q- e! s. h1 p) r* f  P2 G
0 M, }+ k' E/ @5 [
                      BSTR bs = SysAllocString(wsName);2 q6 }5 d% q' L; g
                      HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);
( P; q% Q8 r9 k! M8 Y8 Z  E                       SysFreeString(bs);$ q+ w( C8 I' S' B' w

# n& e# E( L; M+ @- y7 _                       if(hRes == S_OK)
1 t# _5 c4 e: i: f6 _5 a                        {
% a, g  I* R+ G/ ^                          AnsiString s;' ]1 D8 d) T( r- _& c) e. I
                         Variant v = *(Variant*)&vValue;( o8 ~2 z, ]3 q, ?* m9 ^
                         if(v.IsArray())" c0 M7 l, {: [$ ~
                          {9 z( Z; U- `# Z
                            for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)
$ R  R* ~# v* ]. J; q1 g                              {' Z# S4 N, g$ b9 }' R+ r
                               Variant a = v.GetElement(i);5 \; M: f6 k8 R' H) R, k0 f
                               if(!s.IsEmpty())
1 a: i: b* n/ J: W; |+ [                                  s+=", ";3 }3 w% S5 a$ b4 H: b$ K3 c
                               s+=VarToStr(a);  H3 k6 J0 {% f, G" E
                             }( w- r. c5 o2 u9 z: b
                          }1 J4 Z" w: F' @! Y- k  c0 P
                         else
* h& m# }  h8 ]) r9 _4 d! n/ f                           {$ T: y# M) q4 D' R: r9 }
                            s = VarToStr(v);
: W3 T" D$ N1 N1 Z                           }! E8 n4 C: V1 Y0 I3 S
                         lpList->Add(AnsiString(wsName)+"="+s);! ~9 E" X5 f; l% K1 |
                       }
) b( T: i. R4 p) g" {! u+ M) ~* J# b$ ^! d# j% b9 t& L
                      VariantClear(&vValue);
0 J. g4 S) e9 }/ Q                       SysFreeString(wsName);; R) h' F, T7 B! o( \1 Q$ }: ?
                    }# w4 m4 h9 d+ a  z' [* W
                 }
9 x+ h1 y3 R/ ]& K5 K3 i                 if(pvNames)SafeArrayDestroy(pvNames);9 q# J" \# O: ]7 a1 y- D- z
                iEnumIdx++;1 B$ J$ {0 z) U
              }! ]1 a. f& ~* m# D2 e6 o
           }
& X- i- T; h6 ~$ |& h           if(pClassObject)pClassObject->Release();
9 |! f5 X5 o  D2 u% Q" A1 E8 r         }7 M; f7 g4 _( q; D, c
       if(pEnumClassObject)pEnumClassObject->Release();9 D7 ~: _- y; Q+ @5 K! x
     }
& }8 ]) n  d! l' `9 t9 ]' H- k# B: P     if(pWbemServices)pWbemServices->Release();# j0 L9 K6 o9 v5 f' p
  }
! k& l1 K6 }+ i9 \  if(pWbemLocator)pWbemLocator->Release();
7 V2 b9 M+ y5 L6 r}5 H4 O4 a( M$ C6 x0 _
//---------------------------------------------------------------------------' g1 r; V8 y4 ]/ T3 d

4 y8 R, ?' H2 P: L0 n// 通过 WIN32_bios 获取 BIOS 信息:( G4 d- i) f8 j. ?9 @
void __fastcall TForm1::Button1Click(TObject *Sender)
2 |* w. S/ n1 }: T/ x# I3 T/ |{
, a4 j; O, q. o/ ?' m1 ?) V    Memo1->Lines->Add("================== [WIN32_bios] =================");
" Y- P8 T' \* W% F3 F    GetWmiInfo(Memo1->Lines, "WIN32_bios");" P1 m$ w7 m6 |: A" J- R) h  K" h
   Memo1->Lines->Add("");# P7 t9 ~0 s2 i, T$ V5 k
}& I: Z1 O3 c" U
: {# a; m6 h9 W0 q- }
--------------------------------------------------------------------------------
; g& z8 E- D. R" x; D* N
" Z" ]: W) U% l: i- }8 l6 X$ PWMI 可以访问的信息类型有:1 t8 e, u$ e+ `$ x3 q9 g6 r$ r: y
  Win32_1394Controller
0 s! O3 w& j1 x1 k   Win32_BaseBoard
. Y" v, ]4 Z/ U' w   Win32_Battery( h# S8 b/ Q# S5 n# q
  Win32_BIOS  \5 X( @4 H# W, W
  Win32_Bus
1 g! X  l8 V! W" ~$ x) x! {$ l( [   Win32_CacheMemory* L$ o( x! f" Z! R) b: u
  Win32_CDROMDrive
# p0 H: h3 O8 H0 y: @: Z! u   Win32_CurrentProbe
* q9 [2 ]. h4 p   Win32_DesktopMonitor
6 o/ W& s9 I2 M1 X5 q& W" p0 x& h3 N   Win32_DeviceMemoryAddress, U4 S: u" [% e) j5 O% M5 c
  Win32_DiskDrive- l0 f/ I% U5 G) z$ y5 L2 q4 \
  Win32_DisplayConfiguration  e9 E  N7 u) k
  Win32_DisplayControllerConfiguration% ~) {# M: Q6 o
  Win32_DMAChannel
( G' P# V* \  a2 `! Y8 ^   Win32_Fan+ n; }% S0 k9 A: ~2 |3 X
  Win32_FloppyController$ }7 p& Z  l& N3 h$ H
  Win32_FloppyDrive1 N; O$ n; W2 {( n
  Win32_HeatPipe
4 H, K5 Y; N# r   Win32_IDEController  C4 a$ R. q9 `
  Win32_InfraredDevice: A' h+ S' R% E
  Win32_IRQResource
9 f1 j7 n" ?( k+ _- |. u   Win32_Keyboard: L+ K+ }8 a4 m3 @# L! X( Z
  Win32_MemoryArray
% U; X% S: I5 G   Win32_MemoryDevice/ P; Y8 R4 D6 u, `
  Win32_MotherboardDevice
* _8 M$ X* V. r! |- Q" \   Win32_NetworkAdapter$ M: e: n6 F/ o) Z# \
  Win32_NetworkAdapterConfiguration/ i8 z0 B, C7 o4 T) [. Q9 t1 ]: D
  Win32_OnBoardDevice/ ^0 V8 V2 X5 ^7 O: n3 J# i' i- f
  Win32_ParallelPort9 |7 l: `: a& f& I6 y
  Win32_PCMCIAController; r" s3 |+ I% [: [) X; j+ k% o
  Win32_PhysicalMemory- j  J0 W* S+ H1 @; m( @' d
  Win32_PhysicalMemoryArray
8 K2 d7 v1 |" u8 H   Win32_PnPEntity
% j$ Y7 {% ]7 }2 v5 E5 @& H2 e0 w   Win32_PointingDevice
8 z5 y3 L) z  O4 o/ K   Win32_PortableBattery
; Y/ ?: T- M; s   Win32_PortConnector
, E3 U& q' B& p5 V* ?$ V   Win32_PortResource+ ]9 j' i. n; {, n( l9 x
  Win32_POTSModem: g" _$ k- o3 h, l' O+ B
  Win32_PowerManagementEvent' M, ?9 h: n" h7 {
  Win32_Printer
3 f6 ~& ^9 \' S9 @9 g! Z   Win32_PrinterConfiguration
( p  L, @* y+ F  N  |: A6 D   Win32_PrintJob) ?' T( m" X, H: i$ B: |- z9 `! L
  Win32_Processor
7 l. e: u& B" `1 h6 t# i' @' Q' N   Win32_Refrigeration2 X* `8 G( `8 B/ z4 N: G5 a
  Win32_SerialPort
3 e5 Y( c. M: ~# ~: R) o) Z- [   Win32_SerialPortConfiguration. y5 l* }3 r. B) h/ `6 [
  Win32_SMBIOSMemory/ b9 \) R2 z+ E! |  l! G! \
  Win32_SoundDevice3 Z& v! Z, R5 u$ r/ ?
  Win32_SystemEnclosure
& ]+ _5 t# `( k$ C   Win32_SystemMemoryResource
/ V; x4 e& i' w( s% _5 |) T( J) {   Win32_SystemSlot
  _5 ]' d& A7 k   Win32_TapeDrive
8 E1 n* ?9 E, E3 h% d& t$ ?   Win32_TemperatureProbe6 ~5 V7 N% h$ b. n4 H; H, ?
  Win32_UninterruptiblePowerSupply7 ~! J, v6 J# }7 g) V; @4 w* [
  Win32_USBController
' i: m9 o+ W. L! ^   Win32_VideoConfiguration9 l% b$ Y0 I, N& t" N/ c' R
  Win32_VideoController
: B$ d! m4 t7 L8 n5 H% @   Win32_VoltageProbe5 }/ x$ A8 y) w, s7 p! d6 L

  W8 J' y' D* ]1 o9 V; L: A7 c9 ^以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-1-25 21:44 , Processed in 0.023808 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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