2012-03-21 12 views
4

Windowsでディスクの識別子を取得するにはどうすればよいですか?これはボリューム識別子と混同しないでください。彼らは2つの異なったものです。ディスク識別子は、MBRに存在する4バイトの識別子(またはGPTを使用する場合は16バイトの識別子)です。 diskpartを実行してディスクの詳細を問い合わせると、それは 'Disk ID'というラベルの値です。Windowsでディスクの識別子を取得するにはどうすればよいですか?

私は関連性があると思われるMSDNドキュメントをすべて実行しましたが、これを実行できるものは何も見つかりませんでした。明らかに、diskpartがこの値をどこかから得ることができると見なすことは可能です。

私はいつも最後の手段としてdiskpartを呼び出し、出力を解析することができましたが、それを避けることを本当にしたいと思います。誰も私がプログラムでこの数字をどのように得ることができるか知っていますか?

+0

あなたは '\\。\ PhysicalDrive0'にファイルハンドルを開き、MBRを直接読み取ることができます。 –

答えて

3

を試す必要があるでしょう、おそらくWin32_DiskDriveクラスのDeviceIDメンバーの1です。このサンプルは、アプリケーション

#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を特に避けたいと思っていましたが、選択肢がないように見えます。ありがとう。 –

1

このタスクにはWMIを使用できます。

あなたが求めるデータは、そうでなければ、あなたはWin32_DiskDrive WMIクラスとSignatureプロパティを使用することができます:)

0

を返すディスクIDを与える

チェックシステム上のデバイスのS:

wmic diskdrive get serialnumber 

注:このようペンドライブなどの外部ドライブはあなたのマシンにconnecteであれば、上記のコマンドの出力に出て表示されます。

関連する問題