C#结构体转换成IntPtr问题

tPlaybackCapture ntpc =new tPlaybackCapture();
IntPtr sp = Marshal.AllocHGlobal(Marshal.SizeOf(ntpc));
Marshal.StructureToPtr(ntpc, sp, false);

这样转换出现System.ArgumentException异常

异常的提示为Additional information: Type could not be marshaled because the length of an embedded array instance does not match the declared length in the layout.

哪位大神帮忙解决一下啊,感激不尽!!!!

你先看一下 Marshal.SizeOf(ntpc)长度是多少? 还有 是不是 tPlaybackCapture 对齐问题,你直接修改一下看看

tPlaybackCapture ntpc =new tPlaybackCapture();
IntPtr sp = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(tPlaybackCapture)));
Marshal.StructureToPtr(ntpc, sp, true);//false容易造成内存泄漏