获取物理磁盘路径

时间:2011-12-16 23:27:15

标签: c++ windows visual-c++

我想要获取Windows下所有物理磁盘的列表并且昨天与this问题相关联,这确实有效,但它似乎找不到我的未格式化硬盘(虽然我不知道t认为它有所不同,磁盘通过USB连接。)

是否有另一种解决办法来获取所有已连接硬盘的“\\。\ PhysicalDrive”名称?

3 个答案:

答案 0 :(得分:2)

您可以使用SetupDi API列出“DISK”类中的所有设备。 (它将与设备管理器中显示的列表相同)

答案 1 :(得分:0)

或者您可以使用WMI ...

#include <comutil.h>
#pragma comment(lib, "comsuppd.lib")
#pragma comment(lib, "comsuppwd.lib")
#define _WIN32_DCOM
#include <wbemcli.h>
#pragma comment(lib, "wbemuuid.lib")

#include <string>
#include <vector>

using namespace std;

IWbemLocator* pWbemLoc;
IWbemServices* pWbemSvc;

bool ReleaseWMI()
{
    if (pWbemSvc != NULL)
        pWbemSvc->Release();
    pWbemSvc = NULL;
    if (pWbemLoc != NULL)
        pWbemLoc->Release();
    pWbemLoc = NULL;

    CoUninitialize();

    return true;
}

bool InitWMI()
{
    HRESULT hr;
    hr = CoInitializeEx(0, COINIT_MULTITHREADED);
    if (FAILED(hr))
        return false;

    hr = CoInitializeSecurity(
        NULL,
        -1,
        NULL,
        NULL,
        RPC_C_AUTHN_LEVEL_DEFAULT,
        RPC_C_IMP_LEVEL_IMPERSONATE,
        NULL,
        EOAC_NONE,
        NULL);
    if (FAILED(hr))
        return false;

    HRESULT hres = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID*) &pWbemLoc);
    if(FAILED(hres))
    {
        ReleaseWMI();
        return false;
    }

    hres = pWbemLoc->ConnectServer(_bstr_t(L"ROOT\\CIMV2"), NULL, NULL, NULL, WBEM_FLAG_CONNECT_USE_MAX_WAIT, NULL, NULL, &pWbemSvc);
    if(FAILED(hres))
    {
        ReleaseWMI();
        return false;
    }

    hres = CoSetProxyBlanket(pWbemSvc, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE);
    if(FAILED(hres))
    {
        ReleaseWMI();
        return false;
    }

    return true;
}

struct diskInfo
{
    wstring deviceId;
    wstring freeSpace;
};

wstring GetProperty(IWbemClassObject* pclsObj, const wstring &property)
{
    wstring retVal( L"" );
    VARIANT vtProp;
    VariantInit(&vtProp);
    HRESULT hr;
    hr = pclsObj->Get(property.c_str(),0,&vtProp,0,0);
    if (!FAILED(hr))
    {
        VARIANT vtBstrProp;
        VariantInit(&vtBstrProp);
        hr = VariantChangeType(&vtBstrProp, &vtProp, 0, VT_BSTR);
        if (!FAILED(hr))
        {
            retVal = vtBstrProp.bstrVal;
        }
        VariantClear(&vtBstrProp);
    }
    VariantClear(&vtProp);

    return retVal;
}

bool QueryWMI(_bstr_t query, vector<diskInfo> &disks)
{
    bool bRet = false;

    HRESULT hres;
    IEnumWbemClassObject* pEnumerator = NULL;
    IWbemClassObject* pclsObj = NULL;

    if(pWbemSvc)
    {
        hres = pWbemSvc->ExecQuery(bstr_t("WQL"), query, WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator);
        if(!FAILED(hres))
        {
            ULONG uReturn = 0;
            while(pEnumerator)
            {
                HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
                if(uReturn == 0)
                    break;
                diskInfo d;
                d.deviceId  = GetProperty(pclsObj, L"DeviceID");
                d.freeSpace = GetProperty(pclsObj, L"FreeSpace");
                disks.push_back(d);
                bRet = true;
            }
            if(pclsObj != NULL)
                pclsObj->Release();
            if(pEnumerator != NULL)
                pEnumerator->Release();
        }
    }

    return bRet;
}

void main()
{
    InitWMI();
    vector<diskInfo> d;
    bool ret = QueryWMI(bstr_t("SELECT * FROM Win32_LogicalDisk WHERE DriveType=3"), d);
    ReleaseWMI();
}

答案 2 :(得分:0)

对于在未来搜索中可能会遇到此问题的任何人,这是我最终提出的问题。请注意,有一些未定义的类型是外部库的一部分或由我定义,而不是Windows的一部分。例如nowideqDebug

评论是至少可以说,但是函数调用的交叉引用MSDN文档应该可以帮助你。目标是列出每个磁盘的每个列表,并获取所需的信息,以便让我获得设备的HANDLE,并填充即插即用信息(设备描述/名称)。

typedef struct _DISK_DRIVE_INFORMATION
{
    std::string Path;
    std::string FriendlyName;
} DISK_DRIVE_INFORMATION;

unsigned i;
DWORD dwSize, dwPropertyRegDataType = SPDRP_PHYSICAL_DEVICE_OBJECT_NAME;
CONFIGRET r;
HDEVINFO hDevInfo;
SP_DEVINFO_DATA DeviceInfoData;
SP_DEVICE_INTERFACE_DATA interfaceData;

TCHAR szDeviceInstanceID [MAX_DEVICE_ID_LEN];
TCHAR szDesc[1024];

GUID HddClass;
HddClass = GUID_DEVINTERFACE_DISK;//GUID_DEVCLASS_DISKDRIVE;

// List all connected disk drives
hDevInfo = SetupDiGetClassDevs (&HddClass, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (hDevInfo == INVALID_HANDLE_VALUE)
    return;

// Find the ones that are driverless
for (i = 0; ; i++)
{
    DeviceInfoData.cbSize = sizeof (DeviceInfoData);
    // Get the next device info
    if (!SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData))
        break;
    interfaceData.cbSize = sizeof(SP_INTERFACE_DEVICE_DATA);
    // Get the next device interface
    if (!SetupDiEnumInterfaceDevice(hDevInfo, NULL, &HddClass, i, &interfaceData))
    {
        break;
    }

    // Get the device ID
    r = CM_Get_Device_ID(DeviceInfoData.DevInst, szDeviceInstanceID , MAX_PATH, 0);
    if (r != CR_SUCCESS)
        continue;

    // To add to the vector
    DISK_DRIVE_INFORMATION AddToVector;

    DWORD requiredSize = 0;

    // Get the path
    SetupDiGetDeviceInterfaceDetail(hDevInfo, &interfaceData, NULL, NULL, &requiredSize, NULL);
    SP_INTERFACE_DEVICE_DETAIL_DATA* data = (SP_INTERFACE_DEVICE_DETAIL_DATA*) malloc(requiredSize);

    data->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);


    if (!SetupDiGetDeviceInterfaceDetail(hDevInfo, &interfaceData, data, requiredSize, NULL, NULL))
    {
        continue;
    }

    AddToVector.Path = nowide::convert(std::wstring(data->DevicePath));
    qDebug("Disk path: %s", AddToVector.Path.c_str());

    // Friendly name (e.g. SanDisk Cruzer USB...)
    SetupDiGetDeviceRegistryProperty (hDevInfo, &DeviceInfoData, SPDRP_FRIENDLYNAME,
                                      &dwPropertyRegDataType, (BYTE*)szDesc,
                                      sizeof(szDesc),   // The size, in bytes
                                      &dwSize);
    AddToVector.FriendlyName = nowide::convert(std::wstring((TCHAR*)szDesc));
    qDebug("Friendly name: %s", AddToVector.FriendlyName.c_str());

    OutVector.push_back(AddToVector);
    delete data;
}