Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Marshal.PtrToStructure()该结构包含双指针menebre(映射到IntPtr)_C#_.net_Marshalling_Intptr - Fatal编程技术网

C# Marshal.PtrToStructure()该结构包含双指针menebre(映射到IntPtr)

C# Marshal.PtrToStructure()该结构包含双指针menebre(映射到IntPtr),c#,.net,marshalling,intptr,C#,.net,Marshalling,Intptr,我正在动态加载一个win32 DLL并调用一个函数。该函数包含一个passing by ref参数,该参数是一个包含双指针作为成员的结构。我将双指针成员封送为IntPtr,然后将struct参数也封送为IntPtr。这部分代码似乎没有问题 在函数调用(MethodInfo.Invoke())之后,我使用Marshal.PtrToStructure()从IntPtr获取返回的结构数据,并获取异常“无法计算表达式,因为当前方法的代码已优化。”。我确保项目中的“优化代码”未选中 以下是简化代码: ty

我正在动态加载一个win32 DLL并调用一个函数。该函数包含一个passing by ref参数,该参数是一个包含双指针作为成员的结构。我将双指针成员封送为IntPtr,然后将struct参数也封送为IntPtr。这部分代码似乎没有问题

在函数调用(
MethodInfo.Invoke()
)之后,我使用
Marshal.PtrToStructure()
从IntPtr获取返回的结构数据,并获取异常
“无法计算表达式,因为当前方法的代码已优化。”
。我确保项目中的“优化代码”未选中

以下是简化代码:

typedef struct {
    int x;
    int** xx;
}Struct_DoubPtr
int PassOutDoubPtrStructMember(Struct_DoubPtr* ptrStructMember);
封送结构到IntPtr:

var obj = Activator.CreateInstance(Type.GetType("Struct_DoubPtr"));
IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(obj));
在调用Marshal.StructureToPtr(obj,ptr,false)之前,用成员x=20和xx=IntPtr初始化“obj”

将IntPtr封送到结构:

Type type = Type.GetType("Struct_DoubPtr");
var newObj = Activator.CreateInstance(type);
FieldInfo[] fields = type.GetFields();
foreach (FieldInfo field in fields)
{   
    //if the field is an IntPtr 
    //a seperate function iscalled here to determine if the memeber is a IntPtr
    {   
        field.SetValue(newObj, IntPtr.Zero);
    }
}
Marshal.PtrToStructure(dataPtr, newObj); 
//where dataPtr is the strcut parameter from invoking PassOutDoubPtrStructMembe()

任何意见/建议都将非常有用。

您是否将解决方案和项目都设置为调试配置?是的,两者都处于调试模式。