예제 #1
0
파일: MemoryDump.cs 프로젝트: 5l1v3r1/Aphid
        public static void Create(Process process, string filename)
        {
            using (var s = File.Create(filename))
            {
                var ptrs = Marshal.GetExceptionPointers();

                bool result;

                if (ptrs == IntPtr.Zero)
                {
                    result = DbgHelp.MiniDumpWriteDump(
                        process.Handle,
                        (uint)process.Id,
                        s.SafeFileHandle.DangerousGetHandle(),
                        MiniDumpType.MiniDumpWithFullMemory,
                        IntPtr.Zero,
                        IntPtr.Zero,
                        IntPtr.Zero);
                }
                else
                {
                    MINIDUMP_EXCEPTION_INFORMATION mei;
                    mei.ClientPointers    = false;
                    mei.ExceptionPointers = ptrs;
                    mei.ThreadId          = Kernel32.GetCurrentThreadId();

                    result = DbgHelp.MiniDumpWriteDump(
                        process.Handle,
                        (uint)process.Id,
                        s.SafeFileHandle.DangerousGetHandle(),
                        MiniDumpType.MiniDumpWithFullMemory,
                        ref mei,
                        IntPtr.Zero,
                        IntPtr.Zero);
                }

                if (!result)
                {
                    throw new InvalidOperationException(
                              string.Format("Failed creating memory dump '{0}'.", filename),
                              new Win32Exception(Marshal.GetLastWin32Error()));
                }
            }
        }