Windows: How do you figure out the driveway that the system will download for the USB building?



  • There's a composite USB device, known Hub, and the port number where it's connected, can be accessed by descriptors. I'd like to know what driveway the system will load for each of the functions in this device. The list of GUID_DEVINTERFACE_USB_DEVICE via SetupAPI determines the path to the driver, but for the composite devices only the diver device is produced. Where to look?



  • A list of subsidiary devices can be obtained for composite devices using SetupAPI. https://docs.microsoft.com/en-us/windows-hardware/drivers/install/devpkey-device-children ) and remove their driver. The vault returns the list of InstanceID subsidiary devices separated by zero byte (REG_SZ_MULTI). Then we need to complete the list of all devices, filter the necessary InstanceID devices, and we can get a DVD.

    Example of the code on C++ to remove the name of the driver, the way to INF-fail and the version of the driveway for all USB devices and their subsidiary devices:

    #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 and exit the process
    
    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);
    

    }

    /Вывод информации об устройстве с указанным InstanceID/
    BOOL PrintDevice(wchar_t* id)
    {
    unsigned index;
    HDEVINFO hDevInfo;
    SP_DEVINFO_DATA DeviceInfoData;
    TCHAR id_upper[1024]=L"";
    TCHAR buf[1024]=L"";
    TCHAR match[1024];
    DEVPROPTYPE dpt=0;

    for(int i=0;i&lt;wcslen(id);i++){
        id_upper[i]=toupper(id[i]);//преобразование в заглавные буквы
    }       
    
    // List all connected devices
    hDevInfo = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_PRESENT | DIGCF_ALLCLASSES);
    for (index = 0; ; index++) {
        DeviceInfoData.cbSize = sizeof(DeviceInfoData);
        if (!SetupDiEnumDeviceInfo(hDevInfo, index, &amp;DeviceInfoData)) {
            return FALSE;     // no match
        }
    
        BOOL res=SetupDiGetDeviceProperty(hDevInfo,&amp;DeviceInfoData,
                        &amp;DEVPKEY_Device_InstanceId,&amp;dpt,(PBYTE)buf,1000,NULL,0);
        if(res==FALSE)continue;
    
    
        if(wcscmp(buf,id_upper)==0){
            //устройство найдено
            res=SetupDiGetDeviceProperty(hDevInfo,&amp;DeviceInfoData,
                        &amp;DEVPKEY_Device_DeviceDesc,&amp;dpt,(PBYTE)buf,1000,NULL,0);
    
            //name
            if(res==FALSE)ErrorMes(L"SetupDiGetDeviceProperty");
            else wprintf(L"\n\t * %s\n ",buf);          
    
            //service
            res=SetupDiGetDeviceProperty(hDevInfo,&amp;DeviceInfoData,
                    &amp;DEVPKEY_Device_Service,&amp;dpt,(PBYTE)buf,1000,NULL,0);
    
            if(res==FALSE)ErrorMes(L"SetupDiGetDeviceProperty");
            else wprintf(L"\tDriver: %s\n ",buf);
    
            //driver
            res=SetupDiGetDeviceProperty(hDevInfo,&amp;DeviceInfoData,
                &amp;DEVPKEY_Device_DriverInfPath,&amp;dpt,(PBYTE)buf,1000,NULL,0);
    
            if(res==FALSE)ErrorMes(L"SetupDiGetDeviceProperty");
            else wprintf(L"\tINF File: %s\n ",buf);         
    
            //version
            res=SetupDiGetDeviceProperty(hDevInfo,&amp;DeviceInfoData,
                &amp;DEVPKEY_Device_DriverVersion,&amp;dpt,(PBYTE)buf,1000,NULL,0);
    
            if(res==FALSE)ErrorMes(L"SetupDiGetDeviceProperty");
            else wprintf(L"\tVersion: %s\n ",buf);  
    
            return TRUE;
        }
    
    
    }
    return FALSE;//устройство не найдено
    

    }

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

    GUID guid;
    
    HRESULT hr = CLSIDFromString(L"{A5DCBF10-6530-11D2-901F-00C04FB951ED}", (LPCLSID)&amp;guid);//USB device
    
    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);
    
            //перечисление устройств
            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())
                {                    
                   //получение информации о устройстве
                   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;
    
                   //location
                   res=SetupDiGetDeviceProperty(deviceInfoHandle,&amp;devinfo,
                    &amp;DEVPKEY_Device_LocationInfo,&amp;dpt,(PBYTE)buffer,1000,NULL,0);
    
                   if(res==FALSE)ErrorMes(L"SetupDiGetDeviceProperty");
                   else wprintf(L"%s\n",buffer);
    
                   //name
                   res=SetupDiGetDeviceProperty(deviceInfoHandle,&amp;devinfo,
                    &amp;DEVPKEY_Device_DeviceDesc,&amp;dpt,(PBYTE)buffer,1000,NULL,0);
    
                   if(res==FALSE)ErrorMes(L"SetupDiGetDeviceProperty");
                   else wprintf(L"Name: %s\n",buffer);
    
                   //service
                   res=SetupDiGetDeviceProperty(deviceInfoHandle,&amp;devinfo,
                    &amp;DEVPKEY_Device_Service,&amp;dpt,(PBYTE)buffer,1000,NULL,0);
    
                   if(res==FALSE)ErrorMes(L"SetupDiGetDeviceProperty");
                   else wprintf(L"Driver: %s\n",buffer);
    
                   //driver
                   res=SetupDiGetDeviceProperty(deviceInfoHandle,&amp;devinfo,
                    &amp;DEVPKEY_Device_DriverInfPath,&amp;dpt,(PBYTE)buffer,1000,NULL,0);
    
                   if(res==FALSE)ErrorMes(L"SetupDiGetDeviceProperty");
                   else wprintf(L"INF file: %s\n",buffer);
    
                   //version
                   res=SetupDiGetDeviceProperty(deviceInfoHandle,&amp;devinfo,
                    &amp;DEVPKEY_Device_DriverVersion,&amp;dpt,(PBYTE)buffer,1000,NULL,0);
    
                   if(res==FALSE)ErrorMes(L"SetupDiGetDeviceProperty");
                   else wprintf(L"Version: %s\n",buffer);
    
    
                   //children
                   res=SetupDiGetDeviceProperty(deviceInfoHandle,&amp;devinfo,
                    &amp;DEVPKEY_Device_Children,&amp;dpt,(PBYTE)buffer,1000,NULL,0);
    
                   if(res!=FALSE){
                       printf("Children: \n");
                       wchar_t* p=buffer;//указатель на текущий элемент REG_SZ_MULTI
    
                       //вывод дочерних устройств
                       while(true){
                        if(wcscmp(p,L"")==0)break;
                        PrintDevice(p); 
                        p+=wcslen(p)+1; //переход к следующему элементу REG_SZ_MULTI                        
                       }
    
                   }//endif                     
    
                }
            }
            else
            {
                break;
            }
    
            ++deviceIndex;
            printf("\n");
        }//end while
    
        SetupDiDestroyDeviceInfoList(deviceInfoHandle);
    }          
    
    system("PAUSE");
    return 0;
    

    }

    Example of conclusion:

    USB устройства


Log in to reply
 


Suggested Topics

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