2012-03-21 36 views
4

如何在Windows上檢索磁盤標識符?這不要與卷標識符混淆;他們是兩回事。磁盤標識符是駐留在MBR中的4字節標識符(如果使用GPT,則爲16字節標識符)。如果運行diskpart並查詢磁盤的詳細信息,則爲標有「磁盤ID」的值。如何在Windows上獲取磁盤標識符?

我已經瀏覽了所有看起來相關的MSDN文檔,但我還沒有設法找到任何能夠做到這一點的東西;顯然這是可能的,但看到diskpart能夠從某處獲得該值。

我總是可以調用diskpart作爲最後的手段並解析它的輸出,但是我真的希望避免這樣做。有誰知道我怎樣才能以編程方式獲得這個號碼?

+0

假設您擁有管理員權限,您可以打開一個文件句柄到'\\。\ PhysicalDrive0'並直接讀取MBR。 – 2012-03-22 06:55:28

回答

3

可以使用Win32_DiskDrive WMI類和Signature財產。

入住此示例應用程序

#include "stdafx.h" 
#define _WIN32_DCOM 
#include <iostream> 
using namespace std; 
#include <comdef.h> 
#include <Wbemidl.h> 
# pragma comment(lib, "wbemuuid.lib") 


#pragma argsused 
int main(int argc, char* argv[]) 
{ 
    BSTR strNetworkResource; 
    //To use a WMI remote connection set localconn to false and configure the values of the pszName, pszPwd and the name of the remote machine in strNetworkResource 
    strNetworkResource = L"\\\\.\\root\\CIMV2"; 

    COAUTHIDENTITY *userAcct = NULL ; 
    COAUTHIDENTITY authIdent; 

    // Initialize COM. ------------------------------------------ 

    HRESULT hres; 
    hres = CoInitializeEx(0, COINIT_MULTITHREADED); 
    if (FAILED(hres)) 
    { 
     cout << "Failed to initialize COM library. Error code = 0x" << hex << hres << endl; 
     cout << _com_error(hres).ErrorMessage() << endl; 
     cout << "press enter to exit" << endl; 
     cin.get();  
     return 1;     // Program has failed. 
    } 

    // Set general COM security levels -------------------------- 


     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; 
     cout << _com_error(hres).ErrorMessage() << endl; 
     CoUninitialize(); 
     cout << "press enter to exit" << endl; 
     cin.get();  
     return 1;     // Program has failed. 
    } 

    // Obtain the initial locator to WMI ------------------------- 

    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; 
     cout << _com_error(hres).ErrorMessage() << endl; 
     CoUninitialize();  
     cout << "press enter to exit" << endl; 
     cin.get();  
     return 1;     // Program has failed. 
    } 

    // Connect to WMI through the IWbemLocator::ConnectServer method 

    IWbemServices *pSvc = NULL; 

     hres = pLoc->ConnectServer(
      _bstr_t(strNetworkResource),  // 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;  
     cout << _com_error(hres).ErrorMessage() << endl; 
     pLoc->Release(); 
     CoUninitialize(); 
     cout << "press enter to exit" << endl; 
     cin.get();   
     return 1;    // Program has failed. 
    } 

    cout << "Connected to root\\CIMV2 WMI namespace" << endl; 

    // 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; 
     cout << _com_error(hres).ErrorMessage() << endl; 
     pSvc->Release(); 
     pLoc->Release(); 
     CoUninitialize(); 
     cout << "press enter to exit" << endl; 
     cin.get();  
     return 1;    // Program has failed. 
    } 

    // Use the IWbemServices pointer to make requests of WMI ---- 

    IEnumWbemClassObject* pEnumerator = NULL; 
    hres = pSvc->ExecQuery(L"WQL", L"SELECT * FROM Win32_DiskDrive", 
    WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator); 

    if (FAILED(hres)) 
    { 
     cout << "ExecQuery failed" << " Error code = 0x" << hex << hres << endl; 
     cout << _com_error(hres).ErrorMessage() << endl; 
     pSvc->Release(); 
     pLoc->Release(); 
     CoUninitialize(); 
     cout << "press enter to exit" << endl; 
     cin.get();  
     return 1;    // Program has failed. 
    } 

    // Secure the enumerator proxy 


     hres = CoSetProxyBlanket(
      pEnumerator,     // Indicates the proxy to set 
      RPC_C_AUTHN_DEFAULT,   // RPC_C_AUTHN_xxx 
      RPC_C_AUTHZ_DEFAULT,   // RPC_C_AUTHZ_xxx 
      COLE_DEFAULT_PRINCIPAL,   // Server principal name 
      RPC_C_AUTHN_LEVEL_PKT_PRIVACY, // RPC_C_AUTHN_LEVEL_xxx 
      RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx 
      userAcct,      // client identity 
      EOAC_NONE      // proxy capabilities 
      ); 



    // Get the data from the WQL sentence 
    IWbemClassObject *pclsObj = NULL; 
    ULONG uReturn = 0; 

    while (pEnumerator) 
    { 
     HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn); 

     if(0 == uReturn || FAILED(hr)) 
      break; 

     VARIANT vtProp; 

       hr = pclsObj->Get(L"Name", 0, &vtProp, 0, 0);// String 
       if (!FAILED(hr)) 
       { 
        if ((vtProp.vt==VT_NULL) || (vtProp.vt==VT_EMPTY)) 
        wcout << "Name : " << ((vtProp.vt==VT_NULL) ? "NULL" : "EMPTY") << endl; 
        else 
        if ((vtProp.vt & VT_ARRAY)) 
        wcout << "Name : " << "Array types not supported (yet)" << endl; 
        else 
        wcout << "Name : " << vtProp.bstrVal << endl; 
       } 
       VariantClear(&vtProp); 

       hr = pclsObj->Get(L"Signature", 0, &vtProp, 0, 0);// Uint32 
       if (!FAILED(hr)) 
       { 
        if ((vtProp.vt==VT_NULL) || (vtProp.vt==VT_EMPTY)) 
        wcout << "Signature : " << ((vtProp.vt==VT_NULL) ? "NULL" : "EMPTY") << endl;     
        else 
        if ((vtProp.vt & VT_ARRAY)) 
        wcout << "Signature : " << "Array types not supported (yet)" << endl; 
        else 
        wcout << "Signature : " << hex << vtProp.uintVal << endl; 
       } 
       VariantClear(&vtProp); 


     pclsObj->Release(); 
     pclsObj=NULL; 
    } 

    // Cleanup 

    pSvc->Release(); 
    pLoc->Release(); 
    pEnumerator->Release(); 
    if (pclsObj!=NULL) 
    pclsObj->Release(); 

    CoUninitialize(); 
    cout << "press enter to exit" << endl; 
    cin.get(); 
    return 0; // Program successfully completed. 
} 

此代碼返回

enter image description here

而且DiskPart的

enter image description here

+0

我希望避免WMI,因爲代碼太糟糕了,但看起來沒有選擇。謝謝。 – 2012-03-22 02:50:01

1

您應該可以使用WMI完成此任務。

你所尋求的數據可能是一個在Win32_DiskDrive類的DeviceID成員,否則你不得不嘗試:)

0

運行在命令行中輸入以下代碼給出了磁盤ID該系統上的設備的S:

wmic diskdrive get serialnumber 

注意:如果外部驅動器,諸如USB閃盤是connecte到你的機器,它會在上面的命令的輸出中列出了。