是否可以捕获或隐藏非托管异常 window?
Is it possible to catch or hide an unmanaged exception window?
考虑以下本机代码(由于某些原因不可修改):
#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();
}
}
这就是正在发生的事情:
我的问题是我们是否可以在不向用户显示 window 的情况下使应用程序崩溃?我的意思是,好吧,非托管代码发生了一些错误,只需关闭带有错误代码的应用程序,不要向用户显示任何内容。
可行吗?
使用_CrtSetReportMode函数,像这样:
extern "C" {
__declspec(dllexport) void terminate_me(void) {
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG); // define that anywhere in your init code, etc.
puts("hello from C");
std::terminate();
puts("bb from C");
}
}
请注意,当 _DEBUG
未定义时(因此,在发布中),对 _CrtSetReportMode
的调用在预处理期间被删除。
您的 BadImageFormatException
错误可能是因为您在发布模式下选中了 .NET 项目属性中的 "Prefer 32-bit" 复选框。此错误总是 x86-x64 不匹配问题。
考虑以下本机代码(由于某些原因不可修改):
#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();
}
}
这就是正在发生的事情:
我的问题是我们是否可以在不向用户显示 window 的情况下使应用程序崩溃?我的意思是,好吧,非托管代码发生了一些错误,只需关闭带有错误代码的应用程序,不要向用户显示任何内容。
可行吗?
使用_CrtSetReportMode函数,像这样:
extern "C" {
__declspec(dllexport) void terminate_me(void) {
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG); // define that anywhere in your init code, etc.
puts("hello from C");
std::terminate();
puts("bb from C");
}
}
请注意,当 _DEBUG
未定义时(因此,在发布中),对 _CrtSetReportMode
的调用在预处理期间被删除。
您的 BadImageFormatException
错误可能是因为您在发布模式下选中了 .NET 项目属性中的 "Prefer 32-bit" 复选框。此错误总是 x86-x64 不匹配问题。