msdn CM_GET_Parent 示例不起作用

发布于 2024-09-13 03:42:22 字数 1785 浏览 7 评论 0 原文

我正在努力从我插入的 USB 设备获取设备 ID...

上面的应用程序适用于 win-Xp,但不适用于 Windows 7

当我尝试计算它时,我发现 CM_Get_Parent () 不起作用...

任何人都可以吗指导我为什么它不起作用......以及是否有任何可以在 Windows 7

源代码上工作的兼容功能:

HDEVINFO hDevInfo;
GUID guid;
BYTE buffer[ 1024 ];
DWORD dwRequiredSize ;
WCHAR buf[ 1024 ];
DEVINST devInstParent;
WCHAR volume[1024];
int aLength, anIndex;
SP_DEVICE_INTERFACE_DATA devInterfaceData;
SP_DEVINFO_DATA devInfoData;
PSP_DEVICE_INTERFACE_DETAIL_DATA pDevDetail;

guid = GUID_DEVINTERFACE_VOLUME;
hDevInfo = SetupDiGetClassDevs( &guid, NULL, NULL, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT );

if( hDevInfo == INVALID_HANDLE_VALUE )
{
    qDebug() << "Error - SetupDiGetClassDevs failed\n";
}

for( DWORD dwIndex = 0;; dwIndex ++ )
{
    ZeroMemory( &devInterfaceData, sizeof( devInterfaceData ) );
    devInterfaceData.cbSize = sizeof(devInterfaceData );
    if( !SetupDiEnumDeviceInterfaces(hDevInfo, NULL, &guid,dwIndex, &devInterfaceData ) )
    {
        break;
    }

    ZeroMemory( &devInfoData, sizeof( devInfoData ) );
    devInfoData.cbSize = sizeof(devInfoData);

    pDevDetail = (PSP_DEVICE_INTERFACE_DETAIL_DATA)buffer;
    pDevDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA );

    // Get device interface detail data to get Device Instance from
    // SP_DEVINFO_DATA and Device Path from SP_DEVICE_INTERFACE_DETAIL_DATA   
    SetupDiGetDeviceInterfaceDetail(
      hDevInfo,&devInterfaceData,
      pDevDetail, // SP_DEVICE_INTERFACE_DETAIL_DATA
      1024,&dwRequiredSize,&devInfoData ); // SP_DEVINFO_DATA

    CM_Get_Parent( &devInstParent,devInfoData.DevInst, 0 );
    CM_Get_Parent( &devInstParent,devInstParent, 0 );
    CM_Get_Device_ID(  devInstParent, buf, 1024,0 );
}

I am working on getting the device id from the usb device i plug...

The above application works on win-Xp but not in Windows 7

When i try to figure it i found that CM_Get_Parent () not works...

can anybody please guide me on why its not working ...and is there any compatible function which can work on windows 7

source code:

HDEVINFO hDevInfo;
GUID guid;
BYTE buffer[ 1024 ];
DWORD dwRequiredSize ;
WCHAR buf[ 1024 ];
DEVINST devInstParent;
WCHAR volume[1024];
int aLength, anIndex;
SP_DEVICE_INTERFACE_DATA devInterfaceData;
SP_DEVINFO_DATA devInfoData;
PSP_DEVICE_INTERFACE_DETAIL_DATA pDevDetail;

guid = GUID_DEVINTERFACE_VOLUME;
hDevInfo = SetupDiGetClassDevs( &guid, NULL, NULL, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT );

if( hDevInfo == INVALID_HANDLE_VALUE )
{
    qDebug() << "Error - SetupDiGetClassDevs failed\n";
}

for( DWORD dwIndex = 0;; dwIndex ++ )
{
    ZeroMemory( &devInterfaceData, sizeof( devInterfaceData ) );
    devInterfaceData.cbSize = sizeof(devInterfaceData );
    if( !SetupDiEnumDeviceInterfaces(hDevInfo, NULL, &guid,dwIndex, &devInterfaceData ) )
    {
        break;
    }

    ZeroMemory( &devInfoData, sizeof( devInfoData ) );
    devInfoData.cbSize = sizeof(devInfoData);

    pDevDetail = (PSP_DEVICE_INTERFACE_DETAIL_DATA)buffer;
    pDevDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA );

    // Get device interface detail data to get Device Instance from
    // SP_DEVINFO_DATA and Device Path from SP_DEVICE_INTERFACE_DETAIL_DATA   
    SetupDiGetDeviceInterfaceDetail(
      hDevInfo,&devInterfaceData,
      pDevDetail, // SP_DEVICE_INTERFACE_DETAIL_DATA
      1024,&dwRequiredSize,&devInfoData ); // SP_DEVINFO_DATA

    CM_Get_Parent( &devInstParent,devInfoData.DevInst, 0 );
    CM_Get_Parent( &devInstParent,devInstParent, 0 );
    CM_Get_Device_ID(  devInstParent, buf, 1024,0 );
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初雪 2024-09-20 03:42:22

以下稍作修改的代码

#include <Windows.h>
#include <setupapi.h>
#include <cfgmgr32.h>
#include <tchar.h>
#include <stdio.h>
#define ARRAY_SIZE(arr)     (sizeof(arr)/sizeof(arr[0]))

#pragma comment (lib, "setupapi.lib")

int main()
{
    HDEVINFO hDevInfo;
    GUID guid;
    BYTE buffer[ 1024 ];
    DWORD dwRequiredSize ;
    DEVINST devInstParent;
    SP_DEVICE_INTERFACE_DATA devInterfaceData;
    SP_DEVINFO_DATA devInfoData;
    PSP_DEVICE_INTERFACE_DETAIL_DATA pDevDetail;
    CONFIGRET status;
    TCHAR szDeviceInstanceID [MAX_DEVICE_ID_LEN];
    BOOL bSuccess;

    guid = GUID_DEVINTERFACE_VOLUME;
    hDevInfo = SetupDiGetClassDevs( &guid, NULL, NULL, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT );
    if( hDevInfo == INVALID_HANDLE_VALUE ) {
        return 1;
    }

    for( DWORD dwIndex = 0;; dwIndex ++ ) {
        ZeroMemory( &devInterfaceData, sizeof( devInterfaceData ));
        devInterfaceData.cbSize = sizeof( devInterfaceData );

        if( !SetupDiEnumDeviceInterfaces( hDevInfo, NULL, &guid, dwIndex, &devInterfaceData ) ) {
            DWORD dwErrorCode = GetLastError();
            if (dwErrorCode == ERROR_NO_MORE_ITEMS)
                break;
            else 
                return 1;  // ERROR!!!
        }

        ZeroMemory( &devInfoData, sizeof( devInfoData ) );
        devInfoData.cbSize = sizeof( devInfoData);
        pDevDetail = ( PSP_DEVICE_INTERFACE_DETAIL_DATA )buffer;
        pDevDetail->cbSize = sizeof( SP_DEVICE_INTERFACE_DETAIL_DATA );

        // Get device interface detail data to get Device Instance from
        // SP_DEVINFO_DATA and Device Path from SP_DEVICE_INTERFACE_DETAIL_DATA
        bSuccess = SetupDiGetDeviceInterfaceDetail( hDevInfo,&devInterfaceData,
               pDevDetail,// SP_DEVICE_INTERFACE_DETAIL_DATA
               sizeof(buffer),&dwRequiredSize,&devInfoData ); // SP_DEVINFO_DATA
        status = CM_Get_Device_ID (devInfoData.DevInst, szDeviceInstanceID, ARRAY_SIZE(szDeviceInstanceID), 0);
        if (status == CR_SUCCESS) {
            _tprintf (TEXT("%s\n"), szDeviceInstanceID);
        }

        status = CM_Get_Parent( &devInstParent,devInfoData.DevInst, 0 );
        if (status == CR_SUCCESS) {
            status = CM_Get_Device_ID (devInstParent, szDeviceInstanceID, ARRAY_SIZE(szDeviceInstanceID), 0);
            if (status == CR_SUCCESS)
                _tprintf (TEXT("    %s\n"), szDeviceInstanceID);

            status = CM_Get_Parent( &devInstParent,devInstParent, 0 );
            if (status == CR_SUCCESS) {
                status = CM_Get_Device_ID(  devInstParent, szDeviceInstanceID, ARRAY_SIZE(szDeviceInstanceID), 0);
                if (status == CR_SUCCESS) {
                    _tprintf (TEXT("        %s\n"), szDeviceInstanceID);
                }
            }
            else {
                continue;
            }
        }
        else {
            continue;
        }
    }
}

在我的 Windows 7 计算机上运行没有任何问题,并产生如下输出:

SCSI\CDROM&VEN_ELBY&PROD_CLONEDRIVE&REV_1.4\1&2AFD7D61&0&000000
    ROOT\SCSIADAPTER\0000
        HTREE\ROOT\0
STORAGE\VOLUME\{21C004DA-0EB2-11DF-8C02-806E6F6E6963}#0000000000100000
    ROOT\VOLMGR\0000
        HTREE\ROOT\0
STORAGE\VOLUME\{21C004DA-0EB2-11DF-8C02-806E6F6E6963}#000000004B100000
    ROOT\VOLMGR\0000
        HTREE\ROOT\0
STORAGE\VOLUME\{21C004DA-0EB2-11DF-8C02-806E6F6E6963}#00000037C7A00000
    ROOT\VOLMGR\0000
        HTREE\ROOT\0

基于评论的更新:在 VMware 中的 Windows XP 上,相同的实用程序产生以下

FDC\GENERIC_FLOPPY_DRIVE\6&1435B2E2&0&0
    ACPI\PNP0700\5&324D5432&0
        ACPI\PNP0A05\4&5289E18&0
IDE\CDROMNECVMWAR_VMWARE_IDE_CDR10_______________1.00____\3031303030303030303030303030303030303130
    PCIIDE\IDECHANNEL\4&23686003&0&1
        PCI\VEN_8086&DEV_7111&SUBSYS_197615AD&REV_01\3&61AAA01&0&39
STORAGE\VOLUME\1&30A96598&0&SIGNATURE91929192OFFSET7E00LENGTH1FFD5AA00
    ROOT\FTDISK\0000
        HTREE\ROOT\0

输出它是另一个操作系统上的另一台计算机(虚拟计算机),我们收到另一个设备列表。您当前的问题是使函数 CM_Get_Parent() 在 Windows XP 和 Windows 7 上运行。发布的代码就像我们所看到的那样。或者它现在可以在您的 Windows XP 或 Windows 7 计算机上正常工作吗?

如何获取pid和vid(产品id和ventor id)我在获取有关 Windows7 - 32 位系统上的磁盘驱动器结果的信息,但对于任何驱动器号 而不适用于音量设备。人们可以轻松更改代码以显示更多信息,例如卷名称、总线名称和其他信息。但这一切已经是另一个问题了。

更新:我从 .ok-soft-gmbh.com/ForStackOverflow/EnumMassStorage.c" rel="nofollow noreferrer">http://www.ok-soft-gmbh.com/ForStackOverflow/EnumMassStorage.c 因为受到限制stackoverflow.com 中的消息大小。测试输出类似于 http://www.ok-soft-gmbh.com /ForStackOverflow/EnumMassStorage.txt

The following a litle modified code

#include <Windows.h>
#include <setupapi.h>
#include <cfgmgr32.h>
#include <tchar.h>
#include <stdio.h>
#define ARRAY_SIZE(arr)     (sizeof(arr)/sizeof(arr[0]))

#pragma comment (lib, "setupapi.lib")

int main()
{
    HDEVINFO hDevInfo;
    GUID guid;
    BYTE buffer[ 1024 ];
    DWORD dwRequiredSize ;
    DEVINST devInstParent;
    SP_DEVICE_INTERFACE_DATA devInterfaceData;
    SP_DEVINFO_DATA devInfoData;
    PSP_DEVICE_INTERFACE_DETAIL_DATA pDevDetail;
    CONFIGRET status;
    TCHAR szDeviceInstanceID [MAX_DEVICE_ID_LEN];
    BOOL bSuccess;

    guid = GUID_DEVINTERFACE_VOLUME;
    hDevInfo = SetupDiGetClassDevs( &guid, NULL, NULL, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT );
    if( hDevInfo == INVALID_HANDLE_VALUE ) {
        return 1;
    }

    for( DWORD dwIndex = 0;; dwIndex ++ ) {
        ZeroMemory( &devInterfaceData, sizeof( devInterfaceData ));
        devInterfaceData.cbSize = sizeof( devInterfaceData );

        if( !SetupDiEnumDeviceInterfaces( hDevInfo, NULL, &guid, dwIndex, &devInterfaceData ) ) {
            DWORD dwErrorCode = GetLastError();
            if (dwErrorCode == ERROR_NO_MORE_ITEMS)
                break;
            else 
                return 1;  // ERROR!!!
        }

        ZeroMemory( &devInfoData, sizeof( devInfoData ) );
        devInfoData.cbSize = sizeof( devInfoData);
        pDevDetail = ( PSP_DEVICE_INTERFACE_DETAIL_DATA )buffer;
        pDevDetail->cbSize = sizeof( SP_DEVICE_INTERFACE_DETAIL_DATA );

        // Get device interface detail data to get Device Instance from
        // SP_DEVINFO_DATA and Device Path from SP_DEVICE_INTERFACE_DETAIL_DATA
        bSuccess = SetupDiGetDeviceInterfaceDetail( hDevInfo,&devInterfaceData,
               pDevDetail,// SP_DEVICE_INTERFACE_DETAIL_DATA
               sizeof(buffer),&dwRequiredSize,&devInfoData ); // SP_DEVINFO_DATA
        status = CM_Get_Device_ID (devInfoData.DevInst, szDeviceInstanceID, ARRAY_SIZE(szDeviceInstanceID), 0);
        if (status == CR_SUCCESS) {
            _tprintf (TEXT("%s\n"), szDeviceInstanceID);
        }

        status = CM_Get_Parent( &devInstParent,devInfoData.DevInst, 0 );
        if (status == CR_SUCCESS) {
            status = CM_Get_Device_ID (devInstParent, szDeviceInstanceID, ARRAY_SIZE(szDeviceInstanceID), 0);
            if (status == CR_SUCCESS)
                _tprintf (TEXT("    %s\n"), szDeviceInstanceID);

            status = CM_Get_Parent( &devInstParent,devInstParent, 0 );
            if (status == CR_SUCCESS) {
                status = CM_Get_Device_ID(  devInstParent, szDeviceInstanceID, ARRAY_SIZE(szDeviceInstanceID), 0);
                if (status == CR_SUCCESS) {
                    _tprintf (TEXT("        %s\n"), szDeviceInstanceID);
                }
            }
            else {
                continue;
            }
        }
        else {
            continue;
        }
    }
}

work without any problem on my Windows 7 computer and produce output like following:

SCSI\CDROM&VEN_ELBY&PROD_CLONEDRIVE&REV_1.4\1&2AFD7D61&0&000000
    ROOT\SCSIADAPTER\0000
        HTREE\ROOT\0
STORAGE\VOLUME\{21C004DA-0EB2-11DF-8C02-806E6F6E6963}#0000000000100000
    ROOT\VOLMGR\0000
        HTREE\ROOT\0
STORAGE\VOLUME\{21C004DA-0EB2-11DF-8C02-806E6F6E6963}#000000004B100000
    ROOT\VOLMGR\0000
        HTREE\ROOT\0
STORAGE\VOLUME\{21C004DA-0EB2-11DF-8C02-806E6F6E6963}#00000037C7A00000
    ROOT\VOLMGR\0000
        HTREE\ROOT\0

UPDATED based on the comment: On my Windows XP in the VMware the same utility produce the following output

FDC\GENERIC_FLOPPY_DRIVE\6&1435B2E2&0&0
    ACPI\PNP0700\5&324D5432&0
        ACPI\PNP0A05\4&5289E18&0
IDE\CDROMNECVMWAR_VMWARE_IDE_CDR10_______________1.00____\3031303030303030303030303030303030303130
    PCIIDE\IDECHANNEL\4&23686003&0&1
        PCI\VEN_8086&DEV_7111&SUBSYS_197615AD&REV_01\3&61AAA01&0&39
STORAGE\VOLUME\1&30A96598&0&SIGNATURE91929192OFFSET7E00LENGTH1FFD5AA00
    ROOT\FTDISK\0000
        HTREE\ROOT\0

Of cause it is another computer (a virtual computer) on the other operation system and we receive another list of devices. You current question was to make the function CM_Get_Parent() working on both Windows XP and Windows 7. The posted code do this like we can see. Or it is now work correctly on your Windows XP or Windows 7 computer?

How to get pid and vid (product id and ventor id) I described in Get information about disk drives result on windows7 - 32 bit system, but for any drive letters and not for the volume devices. One can easy change the code to show more information like Volume name, Bus name and other information. But this all is already another question.

UPDATED: I placed the extended version of program from Get information about disk drives result on windows7 - 32 bit system under http://www.ok-soft-gmbh.com/ForStackOverflow/EnumMassStorage.c because of restriction with the message size in stackoverflow.com. The test output looks like http://www.ok-soft-gmbh.com/ForStackOverflow/EnumMassStorage.txt

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文