1
votes

Please can anyone give me direct for realization next functional for Windows.

I have USB device which connects to the PC (it is JTAG programmer.) I know VID and PID of this hardware. I need:

1 Check what type of driver this hardware use (detecting winusb driver or not will be enough. Maybe do I need to read registry?)

2 If driver is not winusb I need to install winusb driver for this USB device from my application.

2

2 Answers

0
votes
  1. The current driver assigned to the device is stored in the registry, so you could read it directly from there. However, it is probably better to use SetupAPI, an API provided by Microsoft. The function to call is SetupDiGetDeviceRegistryProperty, and the third argument should be SPDRP_SERVICE. This will return the name of the driver as a string. Note that you will need to call several other SetupAPI functions before you have all the pieces of info you need to call SetupDiGetDeviceRegistryProperty.

  2. I have not tried it, but libwdi has features for installing WinUSB onto a device node. It might also have functions for getting the current driver, so you should try using it before you spend too much time learning SetupAPI. The devcon utility from Microsoft (which is open source now) might be another option.

  3. Without knowing the details of what you are doing, I question whether you really need to do this. It might be simpler to provide a signed driver package to users and instruct them to use the "Update Driver Software..." option from the Device Manager to apply it to particular device.

0
votes

I made first part of task.

#ifdef Q_OS_WIN
DEFINE_GUID(GUID_DEVCLASS_WINUSB,0x88BAE032,0x5A81,0x49f0,
        0xBC,0x3D,0xA4,0xFF,0x13,0x82,0x16,0xD6);
#endif

bool WinUSB::isWinUsbDriver(quint16 vid, quint16 pid)
{
#ifndef Q_OS_WIN
Q_UNUSED(vid);
Q_UNUSED(pid);
return true;
#else
HDEVINFO deviceInfoSet;
GUID *guidDev = (GUID*) &GUID_DEVCLASS_WINUSB;
deviceInfoSet = SetupDiGetClassDevs(guidDev, NULL, NULL, DIGCF_PRESENT |    DIGCF_PROFILE);
DWORD buffersize =4000;
TCHAR buffer [buffersize];
int memberIndex = 0;
bool retval = false;
QString vidPid;

vidPid =  "VID_" + QString("%1").arg(vid,4,16,QChar('0')) + "&";
vidPid += "PID_" + QString("%1").arg(pid,4,16,QChar('0'));

while (true)
{
    SP_DEVINFO_DATA deviceInfoData;
    ZeroMemory(&deviceInfoData, sizeof(SP_DEVINFO_DATA));
    deviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
    if (SetupDiEnumDeviceInfo(deviceInfoSet, memberIndex, &deviceInfoData) == FALSE) {
        if (GetLastError() == ERROR_NO_MORE_ITEMS) {
            break;
        }
    }

    DWORD nSize=0 ;
    SetupDiGetDeviceInstanceId (deviceInfoSet, &deviceInfoData, buffer, sizeof(buffer), &nSize);
    buffer [nSize] ='\0';

    QString str = QString::fromWCharArray(buffer);

    if (str.indexOf(vidPid) >= 0) {
        retval = true;
        break;
    }

        memberIndex++;
}

if (deviceInfoSet) {
    SetupDiDestroyDeviceInfoList(deviceInfoSet);
}

return retval;
#endif
}