1

デバイスの VID と PID を使用して、特定の hid デバイスが接続されているかどうかを検出するプログラムを作成するように依頼されました。だから私は以下のこの機能を思いついた:

    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
    }

関数を呼び出す前に、FindDevice 関数によって返された HID デバイスを、次のように使用しているデバイスにキャストしようとします。

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

そして、次のように関数を呼び出します:

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

私の問題は、m_oTD4PAIDevice64ビットマシンで実行すると常にnullですが、32ビットマシンでは正常に動作することです。64 ビット マシンと 32 ビット マシンの両方で動作させるために何かすべきことはありますか?

どんな提案でも大歓迎です

4

0 に答える 0