求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多執行緒 C++ 多執行緒執行緒C++
- VC++ 執行緒同步(轉)C++執行緒
- 執行緒安全-一個VC下多個網路請求執行緒
- mfc版本及其對應vc版本一覽表 (轉)
- VC++ MFC UpdateData()用法C++
- VC訊號量和執行緒的用法執行緒
- VC動態呼叫DLL
- vc入門寶典六(多執行緒) (轉)執行緒
- MFC中獲取程式自身的版本資訊
- VC中文FAQ寶庫之7:獲得各種目錄資訊(轉)
- VC++的DLL應用C++
- VC++/MFC 教程1(英文) (轉)C++
- VC++/MFC 教程2(英文) (轉)C++
- VC++/MFC 教程3(英文) (轉)C++
- VC編寫多執行緒sql盲注工具.doc執行緒SQL
- VC++獲取不同Windows版本的方法C++Windows
- VC MFC SDI/MDI框架各部分指標獲取方式框架指標
- 如何讓MFC編寫的應用程式(用VC6.0開發)能在別的電腦上執行
- VC程式執行時間測試函式C程式函式
- VC++中實現程式互斥執行 (轉)C++
- VC啟動一個新執行緒的三種方法執行緒
- MFC UI執行緒UI執行緒
- 利用VC++獲取異構型資料庫庫結構資訊 (轉)C++資料庫
- 用vc執行緒模擬實現並行演算法 (轉)執行緒並行演算法
- 關於VC中的DLL的程式設計 (轉)程式設計
- VC++、MFC、COM和ATL的區別C++
- VC++/MFC 教程4&5(英文) (轉)C++
- VC++/MFC 教程6&7(英文) (轉)C++
- 如何正確的關閉 MFC 執行緒執行緒
- (MFC)子執行緒的資料如何傳遞給主執行緒中?執行緒
- VC++的DLL應用(含Demo演示)C++
- 為什麼VC中DEBUG版本可以正常執行的程式有時無法在RELEASE版本下正常執行 (轉)
- VC++執行庫安裝難題C++
- 僅允許程式一個例項執行VC6
- VC++多執行緒下記憶體操作的最佳化 (轉)C++執行緒記憶體
- VC中ftp協議實現多執行緒斷點續傳(轉)FTP協議執行緒斷點
- 檢視win10系統VC執行庫版本的方法【圖文】Win10