作为将我的应用程序划分为单独的appdomains以便在调用本机dll时捕获并从间歇性崩溃中恢复的一部分,我需要一种方法来可靠地触发此类崩溃,以确定我是否正在捕获appdomain正确地说。
我正在寻找一些简单的本机代码(C ++?)我可以编译成一个dll,然后从我的dotnet代码调用,期望它会用它来获取dotnet代码。
有什么建议吗?
答案 0 :(得分:1)
您可以从标准的DLL中导入并执行您知道会导致异常的操作,而不是编写自己的DLL。例如,使用hModule = 0
调用GetProcAddress应导致异常。 可能检查0,因为0可以是NULL
,但我真的怀疑它会检查其他小数字,如1或2:)
如何根据pinvoke.net导入GetProcAddress:
[DllImport("kernel32.dll", CharSet=CharSet.Ansi, ExactSpelling=true, SetLastError=true)]
public static extern UIntPtr GetProcAddress(IntPtr hModule, string procName);
这个程序:
using System.Runtime.InteropServices;
using System;
public class x
{
[DllImport("kernel32.dll", CharSet=CharSet.Ansi, ExactSpelling=true, SetLastError=true)]
public static extern UIntPtr GetProcAddress(IntPtr hModule, string procName);
static void Main()
{
Console.WriteLine(GetProcAddress((IntPtr)5, "asdf"));
}
}
导致以下错误在Mono中,因为我没有kernel32:
Unhandled Exception: System.EntryPointNotFoundException: GetProcAddress at (wrapper managed-to-native) x:GetProcAddress (intptr,string) at x.Main () [0x00000]
所以我不确定它在Windows上的.NET会做什么 - 希望这会导致崩溃!
答案 1 :(得分:0)
只需创建一个可以执行任何不良操作的本机C ++函数,例如除以零,即:
void BeABadMethod()
{
int i = 5;
int j = 0;
printf("%d", i/j);
}
答案 2 :(得分:0)
void CrashAndBurn (void)
{
*((int *)0) = 1337;
}
答案 3 :(得分:0)
您可以创建一个本机DLL,该DLL在DLL尝试调用之前对超出范围的托管代码委托实例进行回调。我不知道它在完整的.Net框架中有什么影响,但在.Net Compact Framework中,它会让你的应用程序立即消失,并且完全无法解决。
这可能不是您正在寻找的,但另一方面它可能正是您正在寻找的,因为这是一个相当常见的间歇性错误来源来自托管代码的本机DLL(更一般地说,当您传入未正确范围或未被“固定”的结构时,可能会发生这些类型的错误,因此当本机DLL是本机DLL时,可能会被GC处理或移动仍与他们合作)。