问题描述
我正在尝试从C#对C ++结构进行互操作.结构(在C#包装器中)是这样的
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.
}
底层的C ++结构有点异常.在32位操作系统中,dwIndex
必须为IntPtr
才能使互操作性起作用,但在64位OS中,必须为UInt64
以便互操作性起作用.
The underlying C++ structure is a bit abnormal. In 32 bit OS, dwIndex
must be IntPtr
in order for the interop to work, but in 64 bit OS, it must be UInt64
in order for the interop to work.
有什么想法如何修改上述结构以使其在32位和64位OS上均可工作?
Any idea how to modify the above structure to make it work on both 32 and 64 bit OS?
推荐答案
如果dwIndex
中的"dw"前缀正确,则听起来像DWORD
,它是32位 unsigned 整数.在这种情况下,您需要使用UIntPtr
,它在32位上类似于UInt32
,在64位上类似于UInt64
.
If the "dw" prefix in dwIndex
is accurate then it sounds like a DWORD
, which is a 32-bit unsigned integer. In that case you need to use UIntPtr
, which will be like UInt32
on 32-bit and like UInt64
on 64-bit.
您的C ++程序不太可能在32位平台上需要带符号的整数,而在64位平台上需要无符号的整数(当然,这并非没有可能).
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).
这篇关于在32位操作系统中为IntPtr,在64位操作系统中为UInt64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!