求VC版本如何利用WMI獲得磁碟資訊 VC/MFC / 程式/執行緒/DLL - 社群 community.csdn.net
WMI使用技巧集
很多的朋友對WMI可能見過但理解不深,我也是十分想了解關於WMI的知識,可一直找不對太合適的資料,在網上的一些資料不是有很多錯誤,就是講解不清,我有空的時候將關於WMI的知識集中一下,放在這裡便於大家學習。本貼會不斷增加。
1、 什麼是WMI
WMI是英文Windows Management Instrumentation的簡寫,它的功能主要是:訪問本地主機的一些資訊和服務,可以管理遠端計算機(當然你必須要擁有足夠的許可權),比如:重啟,關機,關閉程式,建立程式等。
2、 如何用WMI獲得本地磁碟的資訊?
首先要在VS.NET中建立一個專案,然後在新增引用中引用一個.net的裝配件:System.Management.dll,這樣你的專案才能使用WMI。程式碼如下:
using System;
using System.Management;
class Sample_ManagementObject
{
public static int Main(string[] args)
{
SelectQuery query=new SelectQuery("Select * From Win32_LogicalDisk");
ManagementObjectSearcher searcher=new ManagementObjectSearcher(query);
foreach(ManagementBaseObject disk in searcher.Get())
{
Console.WriteLine("/r/n"+disk["Name"] +" "+disk["DriveType"] + " " + disk["VolumeName"]);
}
Console.ReadLine();
return 0;
}
}
disk["DriveType"] 的返回值意義如下:
1 No type
2 Floppy disk
3 Hard disk
4 Removable drive or network drive
5 CD-ROM
6 RAM disk
3、如何用WMI獲得指定磁碟的容量?
using System;
using System.Management;
// This example demonstrates reading a property of a ManagementObject.
class Sample_ManagementObject
{
public static int Main(string[] args)
{
ManagementObject disk = new ManagementObject(
"win32_logicaldisk.deviceid=/"c:/"");
disk.Get();
Console.WriteLine("Logical Disk Size = " + disk["Size"] + " bytes");
Console.ReadLine();
return 0;
}
}Top
3 樓oyljerry(【勇敢的心】→ ㊣提拉米蘇√㊣)回覆於 2005-03-25 18:22:18 得分 0
C#等對WMI支援比較好,使用也方便,
vc6中使用比較的方法資料很少Top
4 樓gooyan(超級替補)回覆於 2005-03-28 09:41:13 得分 0
問一下,win98能支援WMI嗎?我看有的人說支援,也有人說不支援Top
5 樓oyljerry(【勇敢的心】→ ㊣提拉米蘇√㊣)回覆於 2005-03-28 10:26:49 得分 15
WMI基本是獨立於系統的,在某些系統中預設包含WMI,其他系統需要安裝WMI支援。
Top
6 樓keiy()回覆於 2005-03-28 10:52:09 得分 15
VC的例子
http://www.codeguru.com/Cpp/W-P/system/misc/article.php/c5675/
另外98的wmi下載
http://www.lingbo.cn/download.asp
wmi9x.exe
Top
7 樓goodheartppl(goodheart)回覆於 2005-03-28 13:11:14 得分 5
ManagementObjectSearcher searcher=new ManagementObjectSearcher(query);
foreach(ManagementBaseObject disk in searcher.Get())
{
Top
8 樓masterz(www.fruitfruit.com)回覆於 2005-04-13 18:04:49 得分 30
// Get WMI information.
//tested by onega VC20003,windows 2003
#include "stdafx.h"
#define _WIN32_DCOM
#include
using namespace std;
#include
#include
#include
# pragma comment(lib, "wbemuuid.lib")
struct InitOle
{
InitOle()
{
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
}
~InitOle() { ::CoUninitialize(); }
HRESULT hres;
} _init_InitOle_;
HRESULT PrintObject(IWbemClassObject* spInstance)
{
if(!spInstance)
return S_OK;
std::cout<<__FUNCTION__<<" start "< LPSAFEARRAY psa = NULL;
HRESULT hres;
hres = spInstance->GetNames( NULL,
WBEM_FLAG_ALWAYS | WBEM_FLAG_NONSYSTEM_ONLY,
NULL,
&psa);
long lLower, lUpper;
SafeArrayGetLBound(psa , 1, &lLower);
SafeArrayGetUBound(psa , 1, &lUpper);
for (long i = lLower; i <= lUpper; ++i)
{
CComBSTR bstrPropName;
if (S_OK != (hres = SafeArrayGetElement(psa, &i, &bstrPropName)) )
{
if (NULL != psa)
SafeArrayDestroy(psa);
return hres;
}
//if(bstrPropName!="name")
// continue;
std::cout<<" "<<(LPCTSTR)_bstr_t(bstrPropName);
_variant_t varProperty;
HRESULT hr = spInstance->Get(bstrPropName,0,&varProperty,NULL,0);
std::cout<<" = ";
if(varProperty.vt !=VT_EMPTY && VT_NULL != varProperty.vt)
{
_variant_t vDest;
hr = VariantChangeType(&vDest, &varProperty,0,VT_BSTR);
if(SUCCEEDED(hr))
std::cout<< (LPCTSTR)_bstr_t(varProperty);
else
{
if(varProperty.vt == (VT_ARRAY|VT_I4))
{
SAFEARRAY* psa = varProperty.parray;
int *pIntArray = NULL;
SafeArrayAccessData(psa,(VOID**)&pIntArray);
UINT uDim = SafeArrayGetDim(psa);
if(1==uDim)
{
long lLbound,lRbound;
SafeArrayGetLBound(psa,1,&lLbound);
SafeArrayGetUBound(psa,1,&lRbound);
for(long i=lLbound;i<=lRbound;i++)
{
std::cout<< pIntArray[i]<<" ";
}
}
SafeArrayUnaccessData(psa);
}
else
{
std::cout<< varProperty.vt << " type ";
}
}
}
else
{
if(varProperty.vt == VT_EMPTY)
std::cout<< "VT_EMPTY ";
else
std::cout<< "VT_NULL";
}
std::cout< }
if (NULL != psa)
SafeArrayDestroy(psa);
std::cout<<__FUNCTION__<<" end "< return S_OK;
}
int get_wmi_info(CComPtr& pSvc,_bstr_t classname)
{
// Step 6: --------------------------------------------------
// Use the IWbemServices pointer to make requests of WMI ----
_bstr_t wmi_query_str("SELECT * FROM ");
wmi_query_str +=classname;
CComPtr pEnumerator;//IEnumWbemClassObject* pEnumerator = NULL;
HRESULT hres = pSvc->ExecQuery(
bstr_t("WQL"),
wmi_query_str,
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);
if (FAILED(hres))
{
cout << "Query for "<<(LPCSTR)classname<<" failed."
<< " Error code = 0x"
<< hex << hres << endl;
return 1; // Program has failed.
}
// Step 7: -------------------------------------------------
// Get the data from the query in step 6 -------------------
ULONG uReturn = 0;
while (pEnumerator)
{
CComPtr pclsObj;//IWbemClassObject *pclsObj;
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
&pclsObj, &uReturn);
if(0 == uReturn)
{
break;
}
PrintObject(pclsObj);
}
return 0;
}
int main(int argc, char **argv)
{
HRESULT hres = S_OK;
if (FAILED(_init_InitOle_.hres))
{
cout << "Failed to initialize COM library. Error code = 0x"
<< hex << hres << endl;
return 1; // Program has failed.
}
// Step 2: --------------------------------------------------
// Set general COM security levels --------------------------
// Note: If you are using Windows 2000, you need to specify -
// the default authentication credentials for a user by using
// a SOLE_AUTHENTICATION_LIST structure in the pAuthList ----
// parameter of CoInitializeSecurity ------------------------
hres = CoInitializeSecurity(
NULL,
-1, // COM authentication
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
NULL, // Authentication info
EOAC_NONE, // Additional capabilities
NULL // Reserved
);
if (FAILED(hres))
{
cout << "Failed to initialize security. Error code = 0x"
<< hex << hres << endl;
return 1; // Program has failed.
}
// Step 3: ---------------------------------------------------
// Obtain the initial locator to WMI -------------------------
CComPtr pLoc;//IWbemLocator *pLoc = NULL;
hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &pLoc);
if (FAILED(hres))
{
cout << "Failed to create IWbemLocator object."
<< " Err code = 0x"
<< hex << hres << endl;
return 1; // Program has failed.
}
// Step 4: -----------------------------------------------------
// Connect to WMI through the IWbemLocator::ConnectServer method
CComPtr pSvc;//IWbemServices *pSvc = NULL;
// Connect to the root/cimv2 namespace with
// the current user and obtain pointer pSvc
// to make IWbemServices calls.
hres = pLoc->ConnectServer(
_bstr_t(L"ROOT//CIMV2"), // Object path of WMI namespace
NULL, // User name. NULL = current user
NULL, // User password. NULL = current
0, // Locale. NULL indicates current
NULL, // Security flags.
0, // Authority (e.g. Kerberos)
0, // Context object
&pSvc // pointer to IWbemServices proxy
);
if (FAILED(hres))
{
cout << "Could not connect. Error code = 0x"
<< hex << hres << endl;
return 1; // Program has failed.
}
cout << "Connected to ROOT//CIMV2 WMI namespace" << endl;
// Step 5: --------------------------------------------------
// Set security levels on the proxy -------------------------
hres = CoSetProxyBlanket(
pSvc, // Indicates the proxy to set
RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
NULL, // Server principal name
RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
NULL, // client identity
EOAC_NONE // proxy capabilities
);
if (FAILED(hres))
{
cout << "Could not set proxy blanket. Error code = 0x"
<< hex << hres << endl;
return 1; // Program has failed.
}
get_wmi_info(pSvc,"Win32_NetworkAdapter");
return 0; // Program successfully completed.
}Top
本文轉自
http://www.80diy.com/home/20050325/16/3881377.html
相關文章
- VC++ MFC程式設計版本資訊控制C++C程式程式設計
- VC MFC SDI/MDI框架各部分指標獲取方式框架指標
- VC編寫多執行緒sql盲注工具.doc執行緒SQL
- MFC中獲取程式自身的版本資訊
- VC++、MFC、COM和ATL的區別C++
- VC啟動一個新執行緒的三種方法執行緒
- (MFC)子執行緒的資料如何傳遞給主執行緒中?執行緒
- VC++執行庫安裝難題C++
- MFC DLL如何響應PreTranslateMessage訊息
- Linux應用程式獲取執行緒棧的資訊Linux執行緒
- 深入淺出Win32多執行緒程式設計--之MFC的多執行緒Win32執行緒程式設計
- vc code 配置
- win10執行vc++6.0閃退如何解決_win10開啟vc++6.0結果閃退怎麼處理Win10C++
- 更新win10系統後VC++ 6.0無法執行如何解決Win10C++
- 利用訊號量實現執行緒順序執行執行緒
- 如何使用CMake生成Windows DLL版本控制資訊Windows
- vc實現ping
- 154 、MFC中使用者介面執行緒執行緒
- 如何利用執行緒堆疊定位問題執行緒
- Java如何獲取當前執行緒Java執行緒
- 【java】【多執行緒】獲取和設定執行緒名字、獲取執行緒物件(3)Java執行緒物件
- VC++除錯技巧C++除錯
- VC++ gen uuid and timeC++UI
- Java利用執行緒工廠監控執行緒池Java執行緒
- 多執行緒------執行緒與程式/執行緒排程/建立執行緒執行緒
- MySQL 如何獲取執行中的Queries資訊?MySql
- 程式執行緒篇——程式執行緒基礎執行緒
- 遠端執行緒注入dll,突破session 0執行緒Session
- Java多執行緒-執行緒通訊Java執行緒
- Java併發程式設計之執行緒安全、執行緒通訊Java程式設計執行緒
- 簡述CUDA執行緒及求CUDA中執行緒索引執行緒索引
- QT和VC操作快捷鍵QT
- 多執行緒程式是如何執行程式碼的?執行緒行程
- 如何優雅獲的獲取不同系統版本中的程式資訊
- 程式-程式-執行緒執行緒
- spring boot 併發請求,其他系統介面,丟失request的header資訊【多執行緒、執行緒池、@Async 】Spring BootHeader執行緒
- 多執行緒,你覺得你安全了?(執行緒安全問題)執行緒
- 技術分享 | DLL注入之遠執行緒注入執行緒
- 程式和執行緒有什麼區別?(Process and Threads)程式之間和執行緒之間是如何通訊的?執行緒thread