考虑以下本地代码:
#include <cstdio>
#include <exception>
extern "C" {
__declspec(dllexport) void terminate_me(void) {
puts("hello from C");
std::terminate();
puts("bb from C");
}
}
哪个从C#调用
using System.Runtime.InteropServices;
class Program
{
[DllImport("Project1.dll")]
static extern void terminate_me();
static void Main(string[] args)
{
terminate_me();
}
}
这就是发生的事情:
我的问题是,我们可以在不向用户显示此窗口的情况下将其崩溃吗?我的意思是,好的,非托管代码发生了一些不好的事情,只是用错误代码关闭应用程序,不向用户显示任何内容。
这可行吗?
使用_CrtSetReportMode函数,如下所示:
extern "C" {
__declspec(dllexport) void terminate_me(void) {
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG);
puts("hello from C");
std::terminate();
puts("bb from C");
}
}
注意,当未定义_DEBUG
时(因此,在发行版中,在预处理期间将删除对_CrtSetReportMode
的调用。
您的BadImageFormatException
错误可能是由于您已经在发布模式下选中了.NET项目属性中的“首选32位”复选框而导致的。此错误是always x86-x64不匹配的问题。