❶ 请教一个C#生成mp文件的问题
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.IO;
using System.Threading;
namespace MiniDump
...{
class Program
...{
static void Main(string[] args)
...{
try
...{
string a = "";
a = null;
if (a.ToString() == "1")
Console.WriteLine("a is 1");
}
catch
...{
MiniDump.TryDump("c:MiniDmp.dmp", MiniDump.MiniDumpType.WithFullMemory);
}
Console.ReadKey();
}
}
/**//// <summary>
/// 该类要使用在windows 5.1 以后的版本,如果你的windows很旧,就把Windbg里面的dll拷贝过来,一般都没有问题
/// DbgHelp.dll 是windows自带的 dll文件 。
/// </summary>
public static class MiniDump
...{
/**//*
* 导入DbgHelp.dll
*/
[DllImport("DbgHelp.dll")]
private static extern Boolean MiniDumpWriteDump(
IntPtr hProcess,
Int32 processId,
IntPtr fileHandle,
MiniDumpType mpType,
ref MinimpExceptionInfo excepInfo,
IntPtr userInfo,
IntPtr extInfo );
/**//*
* MINIDUMP_EXCEPTION_INFORMATION 这个宏的信息
*/
struct MinimpExceptionInfo
...{
public Int32 ThreadId;
public IntPtr ExceptionPointers;
public Boolean ClientPointers;
}
/**//*
* 自己包装的一个函数
*/
public static Boolean TryDump(String dmpPath, MiniDumpType dmpType)
...{
//使用文件流来创健 .dmp文件
using (FileStream stream = new FileStream(dmpPath, FileMode.Create))
...{
//取得进程信息
Process process = Process.GetCurrentProcess();
// MINIDUMP_EXCEPTION_INFORMATION 信息的初始化
MinimpExceptionInfo mei = new MinimpExceptionInfo();
mei.ThreadId = Thread.CurrentThread.ManagedThreadId;
mei.ExceptionPointers = Marshal.GetExceptionPointers();
mei.ClientPointers = true;
//这里调用的Win32 API
Boolean res = MiniDumpWriteDump(
process.Handle,
process.Id,
stream.SafeFileHandle.DangerousGetHandle(),
dmpType,
ref mei,
IntPtr.Zero,
IntPtr.Zero);
//清空 stream
stream.Flush();
stream.Close();
return res;
}
}
public enum MiniDumpType
...{
None = 0x00010000,
Normal = 0x00000000,
WithDataSegs = 0x00000001,
WithFullMemory = 0x00000002,
WithHandleData = 0x00000004,
FilterMemory = 0x00000008,
ScanMemory = 0x00000010,
WithUnloadedMoles = 0x00000020,
= 0x00000040,
FilterMolePaths = 0x00000080,
WithProcessThreadData = 0x00000100,
WithPrivateReadWriteMemory = 0x00000200,
WithoutOptionalData = 0x00000400,
WithFullMemoryInfo = 0x00000800,
WithThreadInfo = 0x00001000,
WithCodeSegs = 0x00002000
}
}
}
❷ 加载到内存的dll可以mp么
作者用的就是这种方法,我说的没有对应进程就是,LODEPE点开后没有该DLL的名称什么的,就选择不了,所以不能mp出来。
我试过在文件全部复制完成的时候用winhex保存文件,结果看了没有导入表和导出表
❸ 怎么能让我的程序在崩溃时生成mp文件
调试Dump文件很简单,双击自动打开VC然后F7运行。
但是中间要注意很多事情。
1。Dump文件放在哪里
Dump文件不用非要放在你编译出来的位置,你完全可以建立一个新的文件夹来放它。
2。要恢复当时的现场
可能你要问,怎么可能,这个mp文件可是用户发给我的,我不可能去用户家里调试吧?
这个恢复现场可不是指的非要到那台机器上去,而是要把产生mp文件对应的二进制文件拿到。
但是恢复现场需要所有的二进制文件都要对应,你一定要有导致用户崩溃的那些Exe和Dll。既然是你发布的程序,Exe文件当然你会有。所以这里只考虑Dll就行了。
Dump文件中记录了所有dll文件的版本号和时间戳,所以你一定可以同过某种途径拿到它。如果你能从用户那里拿到最好,如果不方便,用户不可能用的是我们平常不常用的操作系统,所以找个有对应系统的机器一般都会有。但是记住不光是文件名称要一致,还要核对版本和时间戳,如果不同一样没有办法用。