SetupDiEnumDeviceInterfaces() always returns false - C# WPF -


i trying connect usb device detected cdc device on system using following code : (i have presented part of code relevant problem) code compiles fine , setupdigetclassdevs function runs ok too. setupdienumdeviceinterfaces returns false , because of not able move further. please tell me 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 access usb dll activity  // related 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()     {         /* info of connected devices */         //globally unique identifier (guid) 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 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 old structure no longer need.                         return;                     }                 }                 else    //else other kind of unknown error ocurred...                 {                     errorstatus = (uint)system.runtime.interopservices.marshal.getlastwin32error();                     native.setupdidestroydeviceinfolist(deviceinfotable);   //clean old structure no longer need.                     return;                 }                  interfaceindex++;              }//end of while(true)         }     } }  } 

i'm not sure why trying use setupapi. if detected cdc acm device, should have usbser.sys driver associated it, , should show com port number in device manager, , can connect com port using .net serialport class:

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


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -