1

I was asked to write a program to detect whether or not a particular hid device is connected or not using the device's VID and PID. So I came up with this function below :

    public static HIDDevice FindDevice(int nVid, int nPid, Type oType) 
    {
        string strPath = string.Empty;
        string strSearch = string.Format("vid_{0:x4}&pid_{1:x4}", nVid, nPid); // first, build the path search string
        Guid gHid;
        HidD_GetHidGuid(out gHid);  // next, get the GUID from Windows that it uses to represent the HID USB interface
        IntPtr hInfoSet = SetupDiGetClassDevs(ref gHid, null, IntPtr.Zero, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);  // this gets a list of all HID devices currently connected to the computer (InfoSet)
        try
        {
            DeviceInterfaceData oInterface = new DeviceInterfaceData(); // build up a device interface data block
            oInterface.Size = Marshal.SizeOf(oInterface);
            // Now iterate through the InfoSet memory block assigned within Windows in the call to SetupDiGetClassDevs
            // to get device details for each device connected
            int nIndex = 0;
            while (SetupDiEnumDeviceInterfaces(hInfoSet, 0, ref gHid, (uint)nIndex, ref oInterface))    // this gets the device interface information for a device at index 'nIndex' in the memory block
            {
                string strDevicePath = GetDevicePath(hInfoSet, ref oInterface); // get the device path (see helper method 'GetDevicePath')
                if (strDevicePath.IndexOf(strSearch) >= 0)  // do a string search, if we find the VID/PID string then we found our device!
                {
                    HIDDevice oNewDevice = (HIDDevice)Activator.CreateInstance(oType);  // create an instance of the class for this device
                    oNewDevice.Initialise(strDevicePath);   // initialise it with the device path
                    return oNewDevice;  // and return it
                }
                nIndex++;   // if we get here, we didn't find our device. So move on to the next one.
            }
        }
        finally
        {
            // Before we go, we have to free up the InfoSet memory reserved by SetupDiGetClassDevs
            SetupDiDestroyDeviceInfoList(hInfoSet);
        }

        return null;    // oops, didn't find our device
    }

Now before calling the function, i try to cast the HID device returned by the FindDevice function to the device i am working with like so:

public static TD4PAIHandsetDevice FindTD4PAIHandset()
    {
        return (TD4PAIHandsetDevice)FindDevice(0x10C4, 0xEA80, typeof(TD4PAIHandsetDevice));
    }

And then i call the function using like this:

private TD4PAIHandsetDevice m_oTD4PAIDevice = null;
m_oTD4PAIDevice = TD4PAIHandsetDevice.FindTD4PAIHandset();

My problem is, m_oTD4PAIDevice is always a null when i run it on a 64 bit machine but works fine on a 32 bit machine. Is there anything i should do to make it work on both 64 bit and 32 bit machines?

Any suggestions will be greatly appreciated

Ahmad Tijani
  • 392
  • 3
  • 10
  • Have you stepped through your code? – huysentruitw Dec 03 '15 at 15:36
  • 1
    I'm not sure if its a case-sensitive search (I can't imagine it should be) but have you tried using `{0:X4}` instead of `{0:x4}` to make it upper case? All the VID/PID strings on my system are upper case, including the VID/PID part. – Ron Beyer Dec 03 '15 at 15:50
  • I have already tried converting strSearch variable to uppercase but still no difference – Ahmad Tijani Dec 03 '15 at 16:08
  • One thing i notice is that the while loop is always false. i.e. it doesn't get the device interface information – Ahmad Tijani Dec 03 '15 at 16:12
  • Have you tried reading the error code with the `GetLastError` method? It will tell you why the call is failing. – Ron Beyer Dec 03 '15 at 16:58
  • it's 1784, "buffer not valid for requested operation" – Ahmad Tijani Dec 03 '15 at 22:29
  • @RonBeyer, it's a platform dependency problem. I tested it on several machines but it only worked on a 32 bit machine. I just edited the question to show this. – Ahmad Tijani Dec 04 '15 at 10:45

0 Answers0