I’m trying to do an interop to a C++ structure from C#. The structure (in a C# wrapper) is something like this
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct SENSE4_CONTEXT
{
public System.IntPtr dwIndex; // Or UInt64, depending on platform.
}
The underlying C++ structure is a bit abnormal. On a 32-bit OS, dwIndex must be IntPtr in order for the interop to work, but on a 64-bit OS, it must be UInt64 in order for the interop to work.
How can I modify the above structure to make it work on both a 32-bit and 64-bit OS?
If the “dw” prefix in
dwIndexis accurate then it sounds like aDWORD, which is a 32-bit unsigned integer. In that case you need to useUIntPtr, which will be likeUInt32on 32-bit and likeUInt64on 64-bit.It seems unlikely that your C++ program requires a signed integer on a 32-bit platform and an unsigned one on a 64-bit one (though not impossible, of course).