当应用程序启动时,PInvoke 用于将以下结构从 VB.net 传递到非托管 C。当我逐步执行代码时,所有值都正确传递。在被调用的 C 方法中,我将 ptr 保存到传入的结构中。
<StructLayout(LayoutKind.Sequential)>
Structure teststruct
<MarshalAs(UnmanagedType.I2)> Public TestData As Short
<MarshalAs(UnmanagedType.I2)> Public TestData2 As Short
<MarshalAs(UnmanagedType.I2)> Public TestData3 As Short
<MarshalAs(UnmanagedType.ByValArray, ArraySubType:=UnmanagedType.I2, SizeConst:=256)> Public TestData4() As Short
End Structure
//Matching C struct
struct teststruct
{
short TestData;
short TestData2;
short TestData3;
short TestData4[256];
};
//The method in C that saves ptr to the struct that is passed in (Worked with VB6)
struct teststruct *VCstruct;
void DllCallbackFunc(struct teststruct *VBStruct)
{
VCstruct = VBStruct;
}
在我将 ptr 保存到传入的结构后,一旦函数返回 VB.net,ptr 就会失效。
我怀疑在回调函数返回后,marshaller 分配的内存被释放了
有没有办法告诉编组器不要释放它在编组期间分配的内存?
感谢任何评论。谢谢