Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear All,

I am using C# to communicate with an external hardware unit connected to the PC.
I am still in the initial stage of USB communication,actually speaking device enumeration.

I am using "SetupDiEnumDeviceInterfaces" metod in windows API's to enumerate the devices connected.
But I always get "false" with error "259".I can't understand the error related to this code.
I will be very thankful if some one can reply me.

Code:
C#
private class Sp_DEVICE_INTERFACE_DETAIL_DATA
{
  public int cbsize;
  public string Devicepath;
}

private class SP_DEVICE_INTERFACE_DATA
{
  public int cbSize;
  public Guid InterfaceClassGuid;
  public int FLAGS;
  public int Reserved;
};

[DllImport("hid.dll")]
private static extern void HidD_GetHidGuid(ref Guid GUID);

[DllImport("setupapi.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SetupDiGetClassDevsA(ref Guid ClassGuid, UInt32 Enumerator, IntPtr hwndParent, UInt32 Flags);

IntPtr deviceinfoset;

[DllImport("setupapi.dll")]
private static extern Boolean SetupDiEnumDeviceInterfaces(IntPtr DeviceInfoSet, int DeviceInfoData, ref Guid InterfaceClassGuid, int MemberIndex, ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData);
SP_DEVICE_INTERFACE_DATA MyDeviceInterfaceData = new SP_DEVICE_INTERFACE_DATA();
int MemberIndex;
Boolean Result;

[DllImport("setupapi.dll", CharSet = CharSet.Auto)]
private static extern Boolean SetupDiGetDeviceInterfaceDetail(IntPtr DeviceInfoSet, ref SP_DEVICE_INTERFACE_DATA DeviceIntefaceData, IntPtr DeviceInterfaceDetailData, int DeviceInterfacedetailDatasize, ref int DeviceInterfacedetaildataSize, IntPtr DeviceInfoData);
int BufferSize;
Boolean Success;
int counter;

public void Initialize_Device(){
  counter = 0;

  Guid hidguid = new Guid();
  HidD_GetHidGuid(ref hidguid);

  const int DIGCF_PRESENT = (0x00000002);
  const int DIGCF_DEVICEINTERFACE = (0x000000001);
  deviceinfoset = SetupDiGetClassDevsA(ref hidguid, 0, IntPtr.Zero, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);

   listBox1.Items.Add("Device Info Set=" + deviceinfoset.ToString());
   listBox1.Items.Add("HidGuid=" + hidguid.ToString());

  

   MyDeviceInterfaceData.cbSize = 28;
   MyDeviceInterfaceData.InterfaceClassGuid = System.Guid.Empty;
   MyDeviceInterfaceData.FLAGS = 0;
   MyDeviceInterfaceData.Reserved = 0;
   MemberIndex = 0;
   Result = SetupDiEnumDeviceInterfaces(deviceinfoset, 0, ref hidguid, MemberIndex, ref MyDeviceInterfaceData);

   listBox1.Items.Add("Result=" + Result.ToString());

}

//the above method is called at the click event of a button


[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 20-Aug-11 21:34pm
v2

Please try to look onto that,

 Get the HID GUID with HidD_GetHidGuid.

 Call SetupDiGetClassDevs and

 See the results with SetupDiEnumDeviceInfo and SetupDiGetDeviceRegistryProperty.

 Call SetupDiEnumDeviceInterfaces, making sure to use the GUID parameter
 
Share this answer
 
Comments
Sameera Fonseka 23-Aug-11 3:42am    
Tkx alot..Tell me this if you have got time to reply..I tested this code with an USB mouse..Is it OK?since the OS has already enumerated and created a file for the mouse,is it possible to get the GUID for the USB mouse from my software in the way i am trying to do it?
Md. Rashim Uddin 23-Aug-11 7:09am    
I guess yes.But i need to try it first then i might confirm you.By this time you might try with this.

Thanks,
Rashim
Sameera Fonseka 23-Aug-11 7:33am    
Ok thankx alot fr ur effrt u had on this.And I will be very thankfl to you f u cn hv ur knd atttn on ths furthur.Thankx alot.
Md. Rashim Uddin 23-Aug-11 8:11am    
If i have enough time surely i will look on that and come back to you again.

Thanks,
Rashim
Dear All,

I think i found the solution for USB communication.I will upload the software source codes in C#,firmware codes in PIC C and hardware circuits implemented using PIC18f4550 to this forum as soon as possible.Thank you very much!!!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900