【发布时间】:2011-02-04 00:36:47
【问题描述】:
我有一种情况,我正在单步执行非/托管代码,我认为我的方法导致我得到“FatalExecutionEngineError”。具体来说,我有一个 C# 回调(“X”),它将另一个回调(“Y”)返回到 C#,其中 X 和 Y 都由非托管 C++ 代码(由 C# 调用)调用。从回调 Y 返回时抛出错误。
Dll 导入语句:
[DllImport ("SearchDLL.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern unsafe bool Dll_Search_Text (..., delegate_add_result Fn);
非托管 C 代码:
extern "C" {
__declspec(dllexport) bool Dll_Search_Text (..., bool (*Add_Result (int, int)) (int, int, int)) {
bool (*Add_Result_Word) (int, int, int);
bool Add_Word_Ret;
...
Add_Result_Word = Add_Result ([int param], [int param]);
if (0 == Add_Result_Word) return false;
...
for (Itr=Set->begin (); Itr != Set->end (); Itr++) {
...
Add_Word_Ret = Add_Result_Word ([int param], [int param], [int param]);
}
}
return true;
}
}
委托定义:
public delegate bool delegate_add_result_word (int A, int B, int C);
public delegate delegate_add_result_word delegate_add_result (int D, int E);
错误信息:
"Managed Debugging Assistant 'FatalExecutionEngineError' has detected a
problem in 'C:\Neuric\bin\Search.exe'. Additional Information: The
runtime has encountered a fatal error. The address of the error was
at 0x6ea2ceca, on thread 0x744. The error code is 0xc0000005. This
error may be a bug in the CLR or in the unsafe or non-verifiable
portions of user code. Common sources of this bug include user marshaling
errors for COM-interop or PInvoke, which may corrupt the stack."
代码有时可以工作(在我添加嵌套回调之前工作得很好),但在其余时间抛出此异常。
【问题讨论】:
-
您如何将 .NET 委托转换为 C 函数指针,您如何处理函数指针/委托以支持转换?
-
x86 或 x64 的架构是什么?
标签: c# c++ delegates unmanaged dllimport