SetupDiEnumDeviceInterfaces() always returns false - C# WPF

0

I am trying to connect to a USB device that is detected as a CDC device on my system using the following code : (I have presented only the part of the code relevant to my problem) The code compiles fine and SetupDiGetClassDevs function runs ok too. But SetupDiEnumDeviceInterfaces always returns false and because of this I am not able to move any further. Please tell me where it is wrong. Thanks.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Runtime.InteropServices;
using System.IO.Ports;

//**************************************************************
// Static class for access to USB dll for all the activity 
// related to USB device
//**************************************************************
namespace USBDeviceConnect
{

#region Unmanaged

public class Native
{
    [DllImport("setupapi.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr SetupDiGetClassDevs(
        ref Guid ClassGuid,
           IntPtr Enumerator,
        IntPtr hwndParent,
        UInt32 Flags);

    [DllImport("setupapi.dll", CharSet = CharSet.Auto)]
    public static extern bool SetupDiEnumDeviceInterfaces(
        IntPtr hDevInfo,
        SP_DEVINFO_DATA devInfo,
        ref Guid interfaceClassGuid,
        UInt32 memberIndex,
        ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData);

    [DllImport(@"setupapi.dll", CharSet = CharSet.Auto)]
    public static extern Boolean SetupDiEnumDeviceInterfaces(
       IntPtr hDevInfo,
       IntPtr devInfo,
       ref Guid interfaceClassGuid,
       UInt32 memberIndex,
       ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData);

    [DllImport("setupapi.dll", CharSet = CharSet.Auto)]
    public static extern int SetupDiDestroyDeviceInfoList(
           IntPtr DeviceInfoSet);
    [DllImport("setupapi.dll", CharSet = CharSet.Auto)]
    public static extern bool SetupDiEnumDeviceInfo(
       IntPtr DeviceInfoSet,
       UInt32 MemberIndex,
       ref SP_DEVINFO_DATA DeviceInfoData);

    //SP_DEVINFO_DATA
    [StructLayout(LayoutKind.Sequential)]
    public struct SP_DEVINFO_DATA
    {
        public int cbSize;
        public Guid ClassGuid;
        public int DevInst;
        public ulong Reserved;
    }

    [StructLayout(LayoutKind.Sequential)]
    public class SP_DEVICE_INTERFACE_DATA
    {
        public int cbSize;
        public Guid interfaceClassGuid;
        public int flags;
        public ulong reserved;
    };
    //PARMS
    public const int DIGCF_ALLCLASSES = (0x00000004);
    public const int DIGCF_PRESENT = (0x00000002);
    public const int DIGCF_PROFILE = (0x00000008);
    public const int DIGCF_DEVICEINTERFACE = (0x00000010);
    public const int INVALID_HANDLE_VALUE = -1;
    public const int MAX_DEV_LEN = 1000;
    public const int DEVICE_NOTIFY_WINDOW_HANDLE = (0x00000000);
    public const int DEVICE_NOTIFY_SERVICE_HANDLE = (0x00000001);
    public const int DEVICE_NOTIFY_ALL_INTERFACE_CLASSES = (0x00000004);
    public const int DBT_DEVTYP_DEVICEINTERFACE = (0x00000005);
    public const int DBT_DEVNODES_CHANGED = (0x0007);
    public const int WM_DEVICECHANGE = (0x0219);
    public const int DIF_PROPERTYCHANGE = (0x00000012);
    public const int DICS_FLAG_GLOBAL = (0x00000001);
    public const int DICS_FLAG_CONFIGSPECIFIC = (0x00000002);
    public const int DICS_ENABLE = (0x00000001);
    public const int DICS_DISABLE = (0x00000002);
    public const long ERROR_NO_MORE_ITEMS = 259L;
    public const int ERROR_SUCCESS = 0;

}
#endregion
/****************************************************************************/
static class AccessUSB
{
    public static void Main()
    {
        ConnectToDevice();
    }

    /* Execution starts here */
    public static void ConnectToDevice()
    {
        /* Get the info of all connected devices */
        //Globally Unique Identifier (GUID) for CDC class devices.

        Guid InterfaceClassGuid = new Guid("a5dcbf10653011d2901f00c04fb951ed");
        IntPtr DeviceInfoTable = (IntPtr)Native.INVALID_HANDLE_VALUE;
        Native.SP_DEVICE_INTERFACE_DATA InterfaceDataStructure = new Native.SP_DEVICE_INTERFACE_DATA();
        Native.SP_DEVINFO_DATA DevInfoData = new Native.SP_DEVINFO_DATA();

        uint InterfaceIndex = 0;
        uint ErrorStatus;
        StringBuilder DeviceName = new StringBuilder();

        DeviceInfoTable = Native.SetupDiGetClassDevs(ref InterfaceClassGuid, IntPtr.Zero, IntPtr.Zero, Native.DIGCF_PRESENT | Native.DIGCF_DEVICEINTERFACE);

        if (DeviceInfoTable.ToInt32() != Native.INVALID_HANDLE_VALUE)
        {
            //Initialize an appropriate SP_DEVINFO_DATA structure.
            DevInfoData.cbSize = Marshal.SizeOf(DevInfoData);
            while (true)
            {
                InterfaceDataStructure.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(InterfaceDataStructure);
                bool bRslt = Native.SetupDiEnumDeviceInterfaces(DeviceInfoTable, IntPtr.Zero, ref InterfaceClassGuid, InterfaceIndex, ref InterfaceDataStructure);
                if (bRslt == true)
                {
                    ErrorStatus = (uint)System.Runtime.InteropServices.Marshal.GetLastWin32Error();

                    if (Native.ERROR_NO_MORE_ITEMS == ErrorStatus)
                    {
                        Native.SetupDiDestroyDeviceInfoList(DeviceInfoTable);//Clean up the old structure we no longer need.
                        return;
                    }
                }
                else    //Else some other kind of unknown error ocurred...
                {
                    ErrorStatus = (uint)System.Runtime.InteropServices.Marshal.GetLastWin32Error();
                    Native.SetupDiDestroyDeviceInfoList(DeviceInfoTable);   //Clean up the old structure we no longer need.
                    return;
                }

                InterfaceIndex++;

            }//end of while(true)
        }
    }
}

}
c#
wpf
usb
asked on Stack Overflow Apr 10, 2014 by user3518666

1 Answer

0

I'm not sure why you are trying to use SetupAPI. If it is a detected as a CDC ACM device, then it should have the usbser.sys driver associated with it, and it should show a COM port number in the Device Manager, and you can connect to that COM port using the .NET SerialPort class:

http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx

answered on Stack Overflow Nov 11, 2014 by David Grayson

User contributions licensed under CC BY-SA 3.0