none
[求助]如何通过volume名,查到此volume所在磁盘的磁盘控制器的PCI 位置(Bus: Slot: Func)信息? RRS feed

  • 问题

  • 我的代码如下:

    TCHAR *szVolName = _T("\\Device\\HarddiskVolume1");
    void PrintVolPCIInfo(void)
    {
        HDEVINFO hDevInfo;
        SP_DEVINFO_DATA spDevInfoData;

        DWORD i = 0;
        TCHAR szDevName[MAX_PATH] = {0};
        TCHAR szBuffer[MAX_PATH] = {0};

        DWORD dwSize = 0;
        DWORD dwDataType = 0;
        DWORD dwPropertyRegDataType = 0;
        
        GUID clsGUID = {0};
        
        DWORD addr = 0;
        DWORD bus = 0;
        DWORD slot = 0;
        DWORD func = 0;
        DWORD dev = 0;
            
        memset(&hDevInfo, 0sizeof(hDevInfo));
        memset(&spDevInfoData, 0sizeof(spDevInfoData));

        //clsGUID = GUID_DEVINTERFACE_STORAGEPORT;
        clsGUID = GUID_DEVINTERFACE_VOLUME;
        hDevInfo = SetupDiGetClassDevs(&clsGUID, 00, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
        if (hDevInfo == INVALID_HANDLE_VALUE)
        {
            MyPrint(_T("failed to get devices tree, errno: %d"), GetLastError());
            return;
        }

        spDevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
        for (i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &spDevInfoData); i++)
        {
            if(!SetupDiGetDeviceRegistryProperty(hDevInfo, &spDevInfoData, SPDRP_PHYSICAL_DEVICE_OBJECT_NAME,
                                                &dwPropertyRegDataType, (PBYTE)szDevName, sizeof(szDevName), &dwSize))
            {
                MyPrint(_T("failed to get device name, errno: %d"), GetLastError());
                break;
            }

            MyPrint(_T("Get the [ %d ] device:\t %s"), i, szDevName);
            
            if(_tcscmp(szDevName, szVolName))
                continue;

            if (!SetupDiGetDeviceRegistryProperty(hDevInfo, &spDevInfoData,
                       SPDRP_BUSNUMBER, &dwDataType, (PBYTE)&bus, sizeof(bus), NULL)) {
                MyPrint(_T("failed to get device pci bus num of dev: %s, errno: %d"), szDevName, GetLastError());
                break;
            }

            if (!SetupDiGetDeviceRegistryProperty(hDevInfo, &spDevInfoData,
                       SPDRP_ADDRESS, &dwDataType, (PBYTE)&addr, sizeof(addr), NULL)) {
                MyPrint(_T("failed to get device pci address dev: %s, errno: %d"), szDevName, GetLastError());
                break;
            }

            if (!SetupDiGetDeviceRegistryProperty(hDevInfo, &spDevInfoData,
                       SPDRP_UI_NUMBER, &dwDataType, (PBYTE)&slot, sizeof(slot), NULL)) {
                MyPrint(_T("failed to get device pci slot number dev: %s, errno: %d"), szDevName, GetLastError());
                break;
            }

            func = addr & 0x0000FFFF;
            dev = (addr >> 16) & 0x0000FFFF;
            
            MyPrint(_T("pci info, domain: %d, bus: %d, slot: %d, func: %d"), dev, bus, slot, func);
        }

        SetupDiDestroyDeviceInfoList(hDevInfo);
        return;
    }

    但是发现一执行,就在 SPDRP_BUSNUMBER 那一行上报错了:

    Get the [ 0 ] device:    \Device\Ide\IdeDeviceP0T1L0-1
    Get the [ 1 ] device:    \Device\Ide\IdeDeviceP1T0L0-2
    Get the [ 2 ] device:    \Device\HarddiskVolume1
    failed to get device pci bus num of dev: \Device\HarddiskVolume1, errno: 13

    查时若是把 GUID 配置为: 

    clsGUID = GUID_DEVINTERFACE_STORAGEPORT;

    就可以了。

    那么有没有什么办法,在只知道VOLUME名称的情况下,找到这个VOLUME所在磁盘的控制器的PCI的 BDF 信息呢?

    2017年6月27日 11:08

全部回复