Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
here is my c# code that takes dump of a process Test_app(a c++ application).

but the problem is that dump is generated when exception occurs in this program(code below) instead of Test_app.

I want to take dump of the Test_app when exception occurs in Test_app.
but i m unable to do so.

C#
using System;
using System.Reflection;
using System.Runtime.Remoting;
using System.Runtime.InteropServices;
using System.IO;
using System.Threading;
using System.Globalization;
using System.Diagnostics;
using System.Collections.Generic;


public class CallMyHelloWorld
{
    public static void Main()
    {

        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomainUnhandledException);


    string i;
    i = Console.ReadLine();
    int a = Convert.ToInt32(i);

    int j = 10 / a;
    Console.WriteLine("The Application succeeded");

    }

    public static class MINIDUMPTYPE
    {
        public const int MiniDumpNormal = 0x00000000;
        public const int MiniDumpWithDataSegs = 0x00000001;
        public const int MiniDumpWithFullMemory = 0x00000002;
        public const int MiniDumpWithHandleData = 0x00000004;
        public const int MiniDumpFilterMemory = 0x00000008;
        public const int MiniDumpScanMemory = 0x00000010;
        public const int MiniDumpWithUnloadedModules = 0x00000020;
        public const int MiniDumpWithIndirectlyReferencedMemory = 0x00000040;
        public const int MiniDumpFilterModulePaths = 0x00000080;
        public const int MiniDumpWithProcessThreadData = 0x00000100;
        public const int MiniDumpWithPrivateReadWriteMemory = 0x00000200;
        public const int MiniDumpWithoutOptionalData = 0x00000400;
        public const int MiniDumpWithFullMemoryInfo = 0x00000800;
        public const int MiniDumpWithThreadInfo = 0x00001000;
        public const int MiniDumpWithCodeSegs = 0x00002000;
    }

    [DllImport("dbghelp.dll")]
    public static extern bool MiniDumpWriteDump(IntPtr hProcess, 
                                                Int32 ProcessId, 
                                                IntPtr hFile, 
                                                int DumpType, 
                                                IntPtr ExceptionParam, 
                                                IntPtr UserStreamParam, 
                                                IntPtr CallackParam);
    private static void CurrentDomainUnhandledException(object sender, 
                                                        UnhandledExceptionEventArgs e)
    {
            CreateMiniDump();
    }

    private static void CreateMiniDump()
    {
        DateTime endTime = DateTime.Now;

        string dt = endTime.ToString("yyyy.MM.dd.HH.mm.ss", DateTimeFormatInfo.InvariantInfo);

        System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName("Test_app");

       string dumpFileName = "C:\\HelloWorldDump" + process[0] + dt + ".dmp";

        FileStream fs = new FileStream(dumpFileName, FileMode.Create);


        MiniDumpWriteDump(process[0].Handle, 
                          process[0].Id, 
                          fs.SafeFileHandle.DangerousGetHandle(), 
                          MINIDUMPTYPE.MiniDumpWithFullMemory, 
                          IntPtr.Zero, 
                          IntPtr.Zero, 
                          IntPtr.Zero);

         }
}
Posted
Updated 28-Apr-10 2:42am
v3

Did you programatically start the process? If so, you can watch for the exit code, but unless that child process is dumping its own state when it fails, I'm not sure what you could get back, because a process could also choose not to provide an exit code in the event of a failure.

So, unless you actually wrote the process you're watching, you're essentially SOL.
 
Share this answer
 
To get a debug dump when an unhandled exception occurs in the Test_App, you have to call the MiniDumpWriteDump function from the Unhandled Exception filter in the Test_App process (the C++ application). What you are doing is adding unhandled exception filter to the process that generates the mini-dump.

Alternatively, you can use the WIN32 debugging API (WaitForDebugEvent) in your C# application but that will be a little complex.
 
Share this answer
 
Thank You Rama Krishna for the prompt reply.

how can I use WIN32 debugging API (WaitForDebugEvent) in my C# application.

Can You provide me with the code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900