【发布时间】:2020-04-14 08:17:38
【问题描述】:
考虑使用本机代码(由于某些原因无法修改):
#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();
}
}
这就是正在发生的事情:
我的问题是我们可以在不向用户显示此窗口的情况下使应用程序崩溃吗?我的意思是好的,非托管代码发生了一些不好的事情,只需关闭带有错误代码的应用程序,不要向用户显示任何内容。
可行吗?
【问题讨论】:
-
构建发布版本?
-
在发布时我什至没有得到
Hello from C:Unhandled Exception: System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)。 (我仔细检查了正确的目标平台) -
我不明白这个问题。你的 C++ 程序没有崩溃;它调用std::terminate,它具有明确定义的行为。你最终想要完成什么?
-
我想改变调用外部 C++ 程序的行为。我现在发现的唯一方法是启动一个新进程,这样它就会被终止而不是我的。
标签: c# c++ windows exception pinvoke