-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_dump.cs
More file actions
111 lines (89 loc) · 3.86 KB
/
Copy pathprocess_dump.cs
File metadata and controls
111 lines (89 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//Code for generating a minidump of a defined process (HP.SkyRoom.exe)This code changed in the release version.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using System.Diagnostics;
using System.ComponentModel;
using Microsoft.Win32.SafeHandles;
namespace DumpGeneration
{
public static class DumpHelper
{
public enum DumpType
{
MiniDumpNormal = 0,
MiniDumpWithDataSegs = 1,
MiniDumpWithFullMemory = 2,
MiniDumpWithHandleData = 4,
MiniDumpFilterMemory = 8,
MiniDumpScanMemory = 16,
MiniDumpWithUnloadedModules = 32,
MiniDumpWithIndirectlyReferencedMemory = 64,
MiniDumpFilterModulePaths = 128,
MiniDumpWithProcessThreadData = 256,
MiniDumpWithPrivateReadWriteMemory = 512,
MiniDumpWithoutOptionalData = 1024,
MiniDumpWithFullMemoryInfo = 2048,
MiniDumpWithThreadInfo = 4096,
MiniDumpWithCodeSegs = 8192,
}
[DllImportAttribute("dbghelp.dll")]
[return: MarshalAsAttribute(UnmanagedType.Bool)]//dll from c++ used number parameters above passed to DLL for greating the minidump
private static extern bool MiniDumpWriteDump(
[In] IntPtr hProcess,
uint ProcessId,
SafeFileHandle hFile,
DumpType DumpType,
[In] IntPtr ExceptionParam,
[In] IntPtr UserStreamParam,
[In] IntPtr CallbackParam);
//different types of memory dumps supported by Dump Helper
public static void WriteTinyDumpForThisProcess(string fileName)
{
WriteDumpForThisProcess(fileName, DumpType.MiniDumpNormal);
}
public static void WriteFullDumpForThisProcess(string fileName)
{
WriteDumpForThisProcess(fileName, DumpType.MiniDumpWithFullMemory);
}
public static void WriteDumpForThisProcess(string fileName, DumpType dumpType)
{
WriteDumpForProcess(Process.GetCurrentProcess(), fileName, dumpType);
}
public static void WriteTinyDumpForProcess(Process process, string fileName)
{
WriteDumpForProcess(process, fileName, DumpType.MiniDumpNormal);
}
public static void WriteFullDumpForProcess(Process process, string fileName)
{
WriteDumpForProcess(process, fileName, DumpType.MiniDumpWithFullMemory);
}
public static void WriteDumpForProcess(Process process, string fileName, DumpType dumpType)
{
using (FileStream fs = File.Create(fileName))
{
if (!MiniDumpWriteDump(Process.GetCurrentProcess().Handle,
(uint)process.Id, fs.SafeFileHandle, dumpType,
IntPtr.Zero, IntPtr.Zero, IntPtr.Zero))
{
throw new Win32Exception(Marshal.GetLastWin32Error(), "Error calling MiniDumpWriteDump.");
}
}
}
}
class Program //This is an example of getting an active process then creating a DMP file of it.
{
static void Main(string[] args)
{
Process process = Process.GetProcessesByName("notepad")[0];//out of range exception thrown if the process is not currently running
//This is a workaround, when you get processes returned it's in an array, parameter [0] returns the first element
process.WaitForInputIdle();//WaitForChangedResult for any input ToolboxItemAttribute cease
DumpHelper.WriteFullDumpForProcess(
process, @"C:\Skyroom.dmp");//Write file location has to be the desktop folder, need a variable value for the folder path.
//process.Kill(); Process will continue to run after the dump
}
}
}