0

I am using the SetupDiEnumDeviceInterfaces function to get the device interfaces that are contained in a device information set. But the GUID is not passing "SP_DEVICE_INTERFACE_DATA" structure. Here is my code snippet.

I have tried to see what is the issue by using GetLastError.It always returns zero.

//GUID.
GetHidGuid(Myguid)

[DllImport("hid.dll", SetLastError = true)]
    static extern unsafe void GetHidGuid(
         ref GUID lpHidGuid);

 [StructLayout(LayoutKind.Sequential)]
    public unsafe struct GUID
    {
        public int Data1;
        public System.UInt16 Data2;
        public System.UInt16 Data3;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
        public byte[] data4;
    }

// SetupDiEnumDeviceInterfaces function.

public unsafe int CT_SetupDiEnumDeviceInterfaces(int memberIndex)
    {
        int ErrorStatus;
        mySP_DEVICE_INTERFACE_DATA = new SP_DEVICE_INTERFACE_DATA();--> here is where i Have problem.GUID is zero.

        mySP_DEVICE_INTERFACE_DATA.cbSize = Marshal.SizeOf(mySP_DEVICE_INTERFACE_DATA);
        int result = SetupDiEnumDeviceInterfaces(
            hDevInfo,
            0,
            ref  MYguid,
            memberIndex,
            ref mySP_DEVICE_INTERFACE_DATA);
        return result;
        ErrorStatus = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
    }

    public unsafe struct SP_DEVICE_INTERFACE_DATA
    {
        public int cbSize;
        public GUID InterfaceClassGuid;
        public int Flags;
        public int Reserved;
    }

any help is appreciated. Thanks in adv.

prashanth
  • 2,059
  • 12
  • 13
user1668957
  • 77
  • 3
  • 11
  • Your pinvoke declarations contain several mistakes. Get good ones from www.pinvoke.net – Hans Passant Sep 25 '12 at 16:05
  • Better yet, use a language that can include the windows header files directly (C++/CLI). All the example code for these APIs is for C and C++, your life will be much easier. – Ben Voigt Sep 25 '12 at 16:14

1 Answers1

1

From pInvoke, it seems your GetHidGuid should be declared as

[DllImport("hid.dll", EntryPoint="HidD_GetHidGuid", SetLastError=true)]
static extern void HidD_GetHidGuid(out Guid hidGuid);

Another complete example is here

prashanth
  • 2,059
  • 12
  • 13
  • Actually I have created GUID before this function using "GetHidGuid(Myguid)" . When I use my mouse, GUID is passing in to SP_DEVICE_INTERFACE_DATA structure. But When I connect other USB HID device, GUID is not passing in to structure.How to pass already created GUID to "mySP_DEVICE_INTERFACE_DATA .InterfaceClassGuid" or how to assign existing GUID ( in this case Myguid) to "mySP_DEVICE_INTERFACE_DATA .InterfaceClassGuid". Any Ideas. Any help is appreciated – user1668957 Sep 25 '12 at 16:11
  • Hello Prashant, Thanks for reply.Is it not possible to assign GUID to "mySP_DEVICE_INTERFACE_DATA .InterfaceClassGuid. I am getting some compiling errors when I followed your way. – user1668957 Sep 25 '12 at 17:13
  • more over I am able to pass GUID to SP_DEVICE_INTERFACE_DATA structure when a USB mouse is hooked up. Similar action is not happened with external chip. – user1668957 Sep 25 '12 at 17:18
  • what is diffrence in passing out value as "ref" and as "out" for example [DllImport("hid.dll", SetLastError = true)] static extern unsafe void GetHidGuid( ref GUID HidGuid); and [DllImport("hid.dll", EntryPoint="HidD_GetHidGuid", SetLastError=true)] static extern void HidD_GetHidGuid(out GUID hidGuid); does this make large diffrence in th code. Can any one answer it. Thanks – user1668957 Sep 25 '12 at 18:46
  • I think the problem is with "SetupDiEnumDeviceInterfaces" itself. int result= SetupDiEnumDeviceInterfaces(hDevInfo, 0, ref MYguid, memberIndex, ref mySP_DEVICE_INTERFACE_DATA);Except the size rest of the structure members are not getting any values.In the above call, "ref MYguid" has the GUID.Same GUID is not getting into this structure. Any ideas Gurus.public unsafe struct SP_DEVICE_INTERFACE_DATA{ public int cbSize;public GUID InterfaceClassGuid; public int Flags; public int Reserved;} – user1668957 Sep 25 '12 at 19:42
  • Any more suggestions please on this issue – user1668957 Sep 26 '12 at 13:41
  • cant think of any, as i cant test it on my box.. sorry – prashanth Sep 26 '12 at 22:18