在vc ++中使用WMI枚举分区驱动器号

时间:2013-10-09 18:02:30

标签: windows visual-c++ com wmi

我想获取计算机上所有分区的系统驱动器号,除了活动分区,使用WMI,这是我到目前为止所得到的(我不知道如何获取驱动器号和如何检查它们是否都是硬盘驱动器或系统驱动器,所以我没有得到任何CD-ROM):

 std::vector<CString> GetPartitionsVector(bool bCoInit   = false  )
{
    std::vector<CString> partVect;
    HRESULT hres = NULL;
    if(bCoInit)
    {

        hres =  CoInitializeEx(0, COINIT_MULTITHREADED); 
        if (FAILED(hres))
        {
            goto end_routine;
        }

        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))
        {
            goto end_routine;
        }
    }


    IWbemLocator *pLoc = NULL;

    hres = CoCreateInstance(
        CLSID_WbemLocator,             
        0, 
        CLSCTX_INPROC_SERVER, 
        IID_IWbemLocator, (LPVOID *) &pLoc);

    if (FAILED(hres))
    {
        goto end_routine;
    }


    IWbemServices *pSvc = NULL;

    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))
    {
        goto end_routine;
    }

    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))
    {
        goto end_routine;
    }

    // Step 6: --------------------------------------------------
    // Use the IWbemServices pointer to make requests of WMI ----

    // For example, get the name of the operating system
    IEnumWbemClassObject* pEnumerator = NULL;
    hres = pSvc->ExecQuery(
        bstr_t("WQL"), 
        bstr_t("SELECT * FROM Win32_DiskPartition"),                // Win32_DiskPartition
        WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, 
        NULL,
        &pEnumerator);

    if (FAILED(hres))
    {
        pSvc->Release();
        pLoc->Release();

        goto end_routine;
    }

    // Step 7: -------------------------------------------------
    // Get the data from the query in step 6 -------------------

    IWbemClassObject *pclsObj;
    ULONG uReturn = 0;

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

        if(0 == uReturn)
        {
            break;
        }

        VARIANT vtProp;

        // Get the value of the Name property
        hr = pclsObj->Get(L"BootPartition", 0, &vtProp, 0, 0);

        if(!vtProp.boolVal)     //if this is not boot partition, get some more
        {
            hr = pclsObj->Get(L"BootPartition", 0, &vtProp, 0, 0);
            if(FAILED(hr))
            {
                //log here
            }
            hr = pclsObj->Get(L"SystemName", 0, &vtProp, 0, 0);
          if(!FAILED(hr))
              partVect.push_back(vtProp.bstrVal);
        }




        VariantClear(&vtProp);
        pclsObj->Release();
    }

    // Cleanup
    // ========

    pSvc->Release();
    pLoc->Release();
    pEnumerator->Release();



end_routine:

    if(bCoInit)
    {
        CoUninitialize();
    }
    return partVect;
}

1 个答案:

答案 0 :(得分:1)

要将Win32_DiskPartition WMI类返回的分区列表与驱动程序字母(逻辑磁盘)关联,必须按照此顺序使用。

  1. 首先致电Win32_DiskDrive班级
  2. 然后使用DeviceID属性和Win32_DiskPartition关联类查询Win32_DiskDriveToDiskPartition的所有实例。
  3. 现在使用Win32_DiskPartition.DeviceID属性和Win32_LogicalDisk关联类调用代表分区的Win32_LogicalDiskToPartition WMI类。
  4. 最后,您可以从Win32_LogicalDisk.DeviceID属性获取驱动器号。
  5. 这里有一组如何使用ASSOCIATORS OF WMI句子的样本。