如果禁用本机代码调试,vshost32-clr2.exe 在调用外部函数时挂起

vshost32-clr2.exe hangs when calling extern function if native code debugging is disabled

情况:

在我的 C# 解决方案 (VS 2013) 中,我添加了一个 C 项目。 C 项目包含一个在 C# 中声明的函数:

[DllImport("My_C_Dll.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern void My_C_Function(IntPtr OutputArray, int Input);

使用此代码调用函数:

var OutputArray = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(UInt16)) * OutputLength);

My_C_Function(OutputArray, Input);

Int16[] ManagedOutputArray= new Int16[OutputLength];
Marshal.Copy(OutputArray, ManagedOutputArray, 0, OutputLength);
Marshal.FreeHGlobal(OutputArray);

问题:

当调用 "My_C_Function" 时应用程序挂起(实际上是 Visual Studio 托管进程 "vshost32-clr2.exe" 挂起)当满足以下所有条件时:

我的想法

似乎 "vshost32-clr2.exe" 停止工作,因为如果 "native code debugging" 被禁用,它就无法执行 C extern 函数。 似乎它需要一些仅在启用 "native code debugging" 时才提供的功能。

我的问题似乎与 AccessViolationException goes away when native code debugging is enabled 相同,但没有人回答为什么会这样。

我的问题 为什么?对于另一个项目,我已经做了一个不同的解决方案,它调用了一个不同的 C extern 函数而没有这样的问题(我可以保持启用 "hosting process" 和禁用 "native code debugging" 没有任何问题)但是我找不到两者之间的区别导致此错误的两种解决方案。

如有任何帮助,我们将不胜感激!

通过在 C++ 项目属性->C/C++->优化中禁用代码优化,我已经能够在 C++ 代码中找到未处理的异常。在修复了一些小的内存泄漏后,我设法使其即使在托管进程处于活动状态且本机代码调试已禁用的情况下也能正常工作。