title: Windows 7 64 bit and accessing Win32 API calls via P/Invoke & Marshal problems
body: <p>I'm relatively new to .net/C# (though very experienced in Win32 / MFC and other platforms) and need to write a utility to talk to a custom USB HID device.  The protocol is pretty simple and I already have a working utility written in MFC, but I would prefer to write the utility in .Net / C# as I'm trying to move with the times and leave MFC behind.</p>
  
  <p>I did some investigation and came across this article which seemed to help me understand how to access HID devices from .Net/C#, especially as it's just calling out the to Win32 API calls I am already familiar with:</p>
  
  <p><a href="http://www.developerfusion.com/article/84338/making-usb-c-friendly/" rel="noreferrer">http://www.developerfusion.com/article/84338/making-usb-c-friendly/</a></p>
  
  <p>The sample code provided gave me an excellent introduction to how to access the Win32 API calls to talk to the USB device (just as my previous MFC code did) and this all works fine on a 32 bit installation of Windows Vista or 7, but when I try to run the same code on a 64 bit installation it fails.  Even if I try creating a dedicated 64 bit application it still fails.</p>
  
  <p>I'm pretty sure the problem is with how the Marshal is passing the parameters (on the stack?) to the Win32 API, but my knowledge and experience of .Net/C# at this stage is not really good enough to understand exactly what the issue is and how to solve it - the problem is likely more advanced than the level I am currently at.</p>
  
  <p>Everything seems to work fine in the code until I reach the instruction...</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Where the SetupDI... returns true on 32 bit systems and subsequently iterates through all the connected USB devices but returns false on 64 bit systems.  I'm pretty sure it's likely to be an issue with how parameters are being passed into the Win32 API function but I don't understand what the exact problem is.  The DLLImport definition for the function is:</p>
  
  <pre><code>[DllImport("setupapi.dll", SetLastError = true)] protected static extern bool SetupDiEnumDeviceInterfaces(IntPtr lpDeviceInfoSet, uint nDeviceInfoData, ref Guid gClass, uint nIndex, ref DeviceInterfaceData oInterfaceData);
  </code></pre>
  
  <p>I wonder if anyone is able to suggest what the offending parameter might be and how I might fix it?</p>
  
  <p>Thanks in advance for any help offered, if more information is required please ask for it!
  Rich</p>