Visual C++ Developer Center > Visual C++ Forums > Visual C++ General > How to access USB device without knowing GUID?
Ask a questionAsk a question
 

AnswerHow to access USB device without knowing GUID?

  • Saturday, November 07, 2009 6:15 PMkumarsunilsan Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi All,

    I am trying to access a USB device connected with my PC for opening handle of device you need GUID specified in the .inf.

    OpenUsbDevice( (LPGUID)&GUID,  //Not known
                           completeDeviceName);
    Its easy if you know the GUID but wht to do when you dont know the GUID without scanning reg manually??


    Please answer!!

    Another question in same context...

    For solving the above problem I manually scanned VID & PID in the Registry and I found the GUID for the device with which I was able to open the device handle easily.

    HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\DeviceClasses\{a5dcbf10-6530-11d2-901f-00c04fb951ed} 

    But problem came when there are two USB devices connected to my. In that case my program was accessing first connected device.

    Example: Two devices are
    HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\DeviceClasses\{a5dcbf10-6530-11d2-901f-00c04fb951ed}\##?#USB#Vid_0411&Pid_00a1#000000000F1102EE#{a5dcbf10-6530-11d2-901f-00c04fb951ed}

    HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\DeviceClasses\{a5dcbf10-6530-11d2-901f-00c04fb951ed}\##?#USB#Vid_058f&Pid_3820#5&337a22df&0&1#{a5dcbf10-6530-11d2-901f-00c04fb951ed}

    how to access desired one as we know GUID {a5dcbf10-6530-11d2-901f-00c04fb951ed} which comon to both devices?

    I am using following code!

     

    char *outNameBuf)

    {
    ULONG NumberDevices;

    HANDLE hOut = INVALID_HANDLE_VALUE;

    HDEVINFO hardwareDeviceInfo;

    SP_DEVICE_INTERFACE_DATA deviceInfoData;

    ULONG i;

    BOOLEAN done;

    PUSB_DEVICE_DESCRIPTOR usbDeviceInst;

    PUSB_DEVICE_DESCRIPTOR *UsbDevices = &usbDeviceInst;

    *UsbDevices = NULL;

    NumberDevices = 0;

     

     

     

    hardwareDeviceInfo = SetupDiGetClassDevs (

    pGuid,

    NULL,

     

    // Define no enumerator (global)

    NULL,

    // Define no

    (DIGCF_PRESENT |

    // Only Devices present

    DIGCF_DEVICEINTERFACE));

    done = FALSE;

    deviceInfoData.cbSize =

    NumberDevices = 4;

     

    sizeof (SP_DEVICE_INTERFACE_DATA); while (!done)  

    i=0;

    {NumberDevices *= 2;

    if (*UsbDevices) {

    *UsbDevices =realloc (*UsbDevices, (NumberDevices *

    sizeof (USB_DEVICE_DESCRIPTOR)));

    }

    else { sizeof (USB_DEVICE_DESCRIPTOR));  

    *UsbDevices = calloc (NumberDevices,

    }

    if (NULL == *UsbDevices) { 
    SetupDiDestroyDeviceInfoList (hardwareDeviceInfo); 
    return INVALID_HANDLE_VALUE;  

    }
    usbDeviceInst = *UsbDevices + i;

    for (; i < NumberDevices; i++) { if (SetupDiEnumDeviceInterfaces (hardwareDeviceInfo, 0,// We don't care about specific PDOspGuid,
    i,
    &deviceInfoData))
    {
    hOut = OpenOneDevice (hardwareDeviceInfo, &deviceInfoData, outNameBuf);
    if ( hOut != INVALID_HANDLE_VALUE )
    {
    done = TRUE;
    break;
    }}
    else{
    if (ERROR_NO_MORE_ITEMS == GetLastError())
    {
    done = TRUE;
    break;
    }}}
    }NumberDevices = i;
    SetupDiDestroyDeviceInfoList (hardwareDeviceInfo);
    free ( *UsbDevices );
    return hOut;

     

    }

    Please help!

    Regards
    Sunil Kumar

    HANDLE

    OpenUsbDevice( LPGUID pGuid,

Answers

  • Saturday, November 07, 2009 10:14 PM«_Superman_»MVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Under the registry key that you listed, there will be a key called #.
    In that key there is a value called SymbolicLink with a value like \\?\USB#VID_xxxx&PID_xxx...

    You can use this string as the first parameter to CreateFile to open the device.


    «_Superman_»
    Microsoft MVP (Visual C++)
  • Tuesday, November 10, 2009 6:13 PM«_Superman_»MVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    You can get the descriptors of a device using APIs.
    Look at the USBView sample in the WDK on how this is done.

    You will need something to identify the device.
    It could be the name or serial number etc.
    I believe the best method is using the VID, PID and serial number.
    The combination will be unique.

    Once you have identified the device, you can use the registry APIs like RegEnumKeyEx to enumerate all # keys and read and compare the VID/PID.

    «_Superman_»
    Microsoft MVP (Visual C++)

All Replies

  • Saturday, November 07, 2009 10:14 PM«_Superman_»MVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Under the registry key that you listed, there will be a key called #.
    In that key there is a value called SymbolicLink with a value like \\?\USB#VID_xxxx&PID_xxx...

    You can use this string as the first parameter to CreateFile to open the device.


    «_Superman_»
    Microsoft MVP (Visual C++)
  • Tuesday, November 10, 2009 5:41 PMkumarsunilsan Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks for your reply.

    I have one more query in the same context. The process mentioned above is manual process. To go registry and found symbolic link in registry.

    Is there any way you can locate the device automaticaly using some API and later retrieve the device path and then open the device with that path.

    As I have seen this in many USB Trace tools which locates the device and trace their transaction without knowing the VID and PID of device.

    Please give some detail on that.


    Thanks in advance.

    Sunil

  • Tuesday, November 10, 2009 6:13 PM«_Superman_»MVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    You can get the descriptors of a device using APIs.
    Look at the USBView sample in the WDK on how this is done.

    You will need something to identify the device.
    It could be the name or serial number etc.
    I believe the best method is using the VID, PID and serial number.
    The combination will be unique.

    Once you have identified the device, you can use the registry APIs like RegEnumKeyEx to enumerate all # keys and read and compare the VID/PID.

    «_Superman_»
    Microsoft MVP (Visual C++)