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:
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());
}
[edit]Code block added - OriginalGriff[/edit]