开发者

msdn CM_GET_Parent Example Doesn't Work

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.cbSiz开发者_开发技巧e = 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 );
}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜