【发布时间】:2013-10-11 11:54:45
【问题描述】:
下面是我的代码sn-p
class Program
{
static void Main(string[] args)
{
Program.GetState(new State() { enabled = true, currentLimit = 30 });
}
private static void GetState(State result)
{
IntPtr Ptr = Marshal.AllocHGlobal(Marshal.SizeOf(result));
Marshal.StructureToPtr(result, Ptr, false);
}
}
[StructLayout(LayoutKind.Sequential)]
public struct State
{
[MarshalAsAttribute(UnmanagedType.I8)]
public uint currentLimit;
[MarshalAsAttribute(UnmanagedType.I1)]
public bool enabled;
}
它总是抛出一个错误,即
类型“MarshellingStructureSize.State”不能作为 非托管结构;无法计算出有意义的大小或偏移量。
我的意图是通过 pInvoke 发送本机 DLL 的结构,但是当我尝试通过 Marshal 在托管代码中为我的结构分配内存时,它总是抛出上述错误。
任何帮助将不胜感激。
【问题讨论】:
-
因为它与 pinvoking 本机 dll 部分相关,因此有用的点也可以来自 C++ 社区。span>
-
问题是 currentLimit 的 MarshalAsAttribute(UnmanagedType.I8)。 C# 中的 uint 只有 4 个字节。将属性更改为 UnmangedType.U4。