Windows 7 64 bit and access to Win32 API calls through P / Invoke & Marshal problems

I am relatively new to .net / C # (although very experienced in Win32 / MFC and other platforms), and you need to write a utility to talk to a custom USB HID device. The protocol is quite simple, and I already have a utility utility written in MFC, but I would prefer to write this utility in .Net / C #, as I try to move over time and leave MFC behind.

I did some research and came across this article, which seemed to help me understand how to access HID devices from .Net / C #, especially since it just calls the Win32 API calls that I'm already familiar with:

http://www.developerfusion.com/article/84338/making-usb-c-friendly/

The above code example gave me a great idea on how to access Win32 API calls to talk to a USB device (like my previous MFC code), and all this works fine with a 32-bit installation of Windows Vista or 7, but when I am trying to run the same code on a 64 bit installation, it fails. Even if I try to create a dedicated 64-bit application, it still does not work.

I am sure that the problem is how the marshal passes parameters (on the stack?) To the Win32 API, but my knowledge and experience of .Net / C # at this stage is not very good, to understand exactly what the problem is and how to solve it - the problem is probably more developed than the level I'm in now.

Everything seems to work fine in the code until I get to the instructions ...

while (SetupDiEnumDeviceInterfaces(hInfoSet, 0, ref gHid, (uint)nIndex, ref oInterface))    // this gets the device interface information for a device at index 'nIndex' in the memory block

SetupDI... true 32- USB-, false 64- . , , , , API Win32, , . DLLImport :

[DllImport("setupapi.dll", SetLastError = true)] protected static extern bool SetupDiEnumDeviceInterfaces(IntPtr lpDeviceInfoSet, uint nDeviceInfoData, ref Guid gClass, uint nIndex, ref DeviceInterfaceData oInterfaceData);

, - , ?

, , , !

+5
2

, ( ), GetLastError, Marshal.GetLastWin32Error() .NET.

, oInterface. , cbSize. 64- (SP_DEVICE_INTERFACE_DATA) , 32- . ( ), , 32- 28 , 64- 32 .

+2

MSDN:

BOOL SetupDiEnumDeviceInterfaces(
  __in      HDEVINFO DeviceInfoSet,
  __in_opt  PSP_DEVINFO_DATA DeviceInfoData,
  __in      const GUID *InterfaceClassGuid,
  __in      DWORD MemberIndex,
  __out     PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData
);
DeviceInfoData [in, optional]
SP_DEVINFO_DATA...

, DeviceInfoData - IntPtr, uInt:

    [DllImport("setupapi.dll", SetLastError = true)] protected static
    extern bool SetupDiEnumDeviceInterfaces(IntPtr lpDeviceInfoSet,
        IntPtr pDeviceInfoData, ref Guid gClass,
        uint nIndex, ref DeviceInterfaceData oInterfaceData);

, , IntPtr.Zero 0.

+5

All Articles