How do you get a VID:PID for USB Hub in Windows?



  • I have a symbolic link to the USB Root Hub species \\\USB#ROOT_HUB#4E0974880#{f18a0e88-c30c-11d0-8815-00a0c906bed8} and I want to get a VID:PID for haba. I work as a user mode, IOCTL, I can send. But among those described in MSDN https://msdn.microsoft.com/en-us/library/windows/hardware/ff537421%28v=vs.85%29.aspx I don't find the right one. How to be?



  • VID and PID for USB Hub can be determined by its sampling https://docs.microsoft.com/en-us/windows-hardware/drivers/install/standard-usb-identifiers ♪ Hardware ID can be obtained by means of a function https://msdn.microsoft.com/en-us/library/windows/hardware/ff551967(v=vs.85).aspx (Setup API). The function returns several values, the first one with the VID/PID. For the root concentrate, the shape of the line is as follows:

    USB\ROOT_HUB30XVID8086 ECID8C31REV0005

    Example of code c+ for the removal of symbolic link, name, VID and PID for all USB Hub-s; based on https://stackoverflow.com/a/36728329/8674428 ♪

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

    #include <Windows.h>
    #include <Setupapi.h>
    #include <winusb.h>
    #include <stdlib.h>
    #include <Devpkey.h>
    #include <iostream>
    #include <string>
    #include <memory>
    #include <strsafe.h>

    void ErrorMes(LPTSTR lpszFunction)
    {
    // Retrieve the system error message for the last-error code

    LPVOID lpMsgBuf;
    LPVOID lpDisplayBuf;
    DWORD dw = GetLastError(); 
    
    FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER | 
        FORMAT_MESSAGE_FROM_SYSTEM |
        FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        dw,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR) &amp;lpMsgBuf,
        0, NULL );
    
    // Display the error message 
    
    lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, 
        (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR)); 
    wprintf(L"%s failed with error %d: %s", 
        lpszFunction, dw, lpMsgBuf);     
    
    LocalFree(lpMsgBuf);
    LocalFree(lpDisplayBuf);
    

    }

    int main()
    {
    setlocale(LC_ALL,"Russian");

    GUID guid;
    
    
    HRESULT hr = CLSIDFromString(L"{F18A0E88-C30C-11D0-8815-00A0C906BED8}", (LPCLSID)&amp;guid);//USB Hub
    
    HDEVINFO deviceInfoHandle = SetupDiGetClassDevs(&amp;guid, 0, 0, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
    
    if (deviceInfoHandle != INVALID_HANDLE_VALUE)
    {
        int deviceIndex = 0;
        while (true)
        {
            SP_DEVICE_INTERFACE_DATA deviceInterface = { 0 };
            deviceInterface.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
    
            //перечисление всех интерфейсов USB Hub
            if (SetupDiEnumDeviceInterfaces(deviceInfoHandle, 0, &amp;guid, deviceIndex, &amp;deviceInterface))
            {
                DWORD cbRequired = 0;
    
                SetupDiGetDeviceInterfaceDetail(deviceInfoHandle, &amp;deviceInterface, 0, 0, &amp;cbRequired, 0);
                if (ERROR_INSUFFICIENT_BUFFER == GetLastError())
                {
                    PSP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetail = 
                        (PSP_DEVICE_INTERFACE_DETAIL_DATA)(new char[cbRequired]);
                    memset(deviceInterfaceDetail, 0, cbRequired);
                    deviceInterfaceDetail-&gt;cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
    
                    if (!SetupDiGetDeviceInterfaceDetail(deviceInfoHandle, &amp;deviceInterface, deviceInterfaceDetail, 
                        cbRequired, &amp;cbRequired, 0))
                    {
                        deviceIndex++;
                        continue;
                    }
    
                    // Initialize the structure before using it.
                    memset(deviceInterfaceDetail, 0, cbRequired);
                    deviceInterfaceDetail-&gt;cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
    
                    // Call the API a second time to retrieve the actual
                    // device path string.
                    BOOL status = SetupDiGetDeviceInterfaceDetail(
                        deviceInfoHandle,  // Handle to device information set
                        &amp;deviceInterface,     // Pointer to current node in devinfo set
                        deviceInterfaceDetail,   // Pointer to buffer to receive device path
                        cbRequired,   // Length of user-allocated buffer
                        &amp;cbRequired,  // Pointer to arg to receive required buffer length
                        NULL);        // Not interested in additional data
    
                    wprintf(deviceInterfaceDetail-&gt;DevicePath);//вывод symbolic link
                    wprintf(L"\n");
    
                    //получение информации о устройстве
                    DEVPROPTYPE dpt=0;
                    wchar_t buffer[1000]=L"";
                    DWORD RequiredSize = 0;
                    SP_DEVINFO_DATA devinfo = { 0 };
                        devinfo.cbSize = sizeof(SP_DEVINFO_DATA);
    
                    BOOL success = SetupDiEnumDeviceInfo(deviceInfoHandle, deviceIndex, &amp;devinfo);
                    if(success==FALSE){ErrorMes(L"SetupDiEnumDeviceInfo");}
    
                    BOOL res;
    
                    //name
                    res=SetupDiGetDeviceProperty(deviceInfoHandle,&amp;devinfo,
                        &amp;DEVPKEY_Device_DeviceDesc,&amp;dpt,(PBYTE)buffer,1000,NULL,0);
    
                    printf("Name: ");
                    if(res==FALSE)ErrorMes(L"SetupDiGetDeviceProperty");
                    else wprintf(buffer);
                    printf("\n");
    
                    //hardware id
                    DWORD regType = REG_MULTI_SZ;
                    unsigned char* ptr = new unsigned char[2048];
                    if (SetupDiGetDeviceRegistryProperty(deviceInfoHandle, &amp;devinfo, SPDRP_HARDWAREID, &amp;regType, 
                        ptr, 2048, &amp;RequiredSize))
                    {
                        wchar_t* input=reinterpret_cast&lt;wchar_t*&gt;(ptr);
                        wchar_t sep[]=L"&amp;\\";
                        wchar_t vid[5]=L"";
                        wchar_t pid[5]=L"";
                        bool found=false;
    
                        /*проходим по всем элементам REG_MULTI_SZ*/
                        while(true){
    
                            wchar_t* tok=NULL;
                            wchar_t* pos=NULL;
    
                            //разбор hardware id
                            tok=wcstok(input,sep);
                            while( tok != NULL )  
                            {                       
                                pos=wcsstr(tok,L"VID");
    
                                if(pos!=NULL){                          
                                    pos+=3;
                                    if(pos[0]==L'_')pos++;
                                    StringCchCopy(vid,5,pos);   
                                    found=true;
                                }
    
                                pos=wcsstr(tok,L"PID");
    
                                if(pos!=NULL){                          
                                    pos+=3;
                                    if(pos[0]==L'_')pos++;
                                    StringCchCopy(pid,5,pos);
                                    found=true;
                                }
    
                                tok = wcstok( NULL, sep ); 
                            }//конец разбора hardware id
    
                            if(found!=false)break;
    
                            input+=wcslen(input)+1;//переход к следующему элементу REG_MULTI_SZ
    
                            if(wcscmp(input,L"")==0)break;
                        }//конец прохода
    
                        if(found!=false)
                        {
                            wprintf( L"VID: %s\n", vid );
                            wprintf( L"PID: %s\n", pid );                           
                        }
                        else 
                        {
                            wprintf( L"Not found\n");
                        }
    
                    }
                    else ErrorMes(L"SetupDiGetDeviceRegistryProperty");
    
                    delete[] ptr;
                    delete[] deviceInterfaceDetail;
    
                    printf("\n");                   
    
                }
            }
            else
            {
                break;
            }
    
            ++deviceIndex;
        }
    
        SetupDiDestroyDeviceInfoList(deviceInfoHandle);
    }
    
    
    system("PAUSE");
    return 0;
    

    }




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2