coinbird
coinbird

Reputation: 1217

Debugging an exe that's run by another exe

I took over a project recently, and I'm not sure how the last guy was debugging this... I have two executables, one eventually runs another. I will call them exe1 and exe2. These were created in C# and I use Visual Studio 2015

Running exe1 works fine. It does some stuff, and eventually kicks off exe2. exe2 eventually errors out. It's writing to my log file:

BaseException:System.NullReferenceException: Object reference not set to an instance of an object.

(I can usually troubleshoot that error. I'm sure it's just an issue creating an object or something, but my trial and errors method of fixing it have not worked so far. )

I don't have the source code for exe1 so I can't rig up some super program to do everything in one go (which would allow me to debug). Is there a way I can set breakpoints and step through exe2 doing its thing?

I should add, exe2 NEEDS exe1 to run. exe1 does a bunch of stuff, and without it exe2 just crashes immediately.

Upvotes: 5

Views: 2703

Answers (3)

Fletcher
Fletcher

Reputation: 420

There are two ways may solve this.

1. Using Debugger.Launch() as John gave the answer above.

public static void Main(string[] args)
            {
                Debugger.Launch();

                //Rest of your code 
            }

This will allow you to attach to your running visual studio or start a new instance.

2. When the previous one does not work, another way is to put a loop at the start of the code to check if a debugger is attached, this gives you enough time to do the "Attach to process" method.

public static void Main(string[] args)
{
    while(!Debugger.IsAttached)
    {
        Thread.Sleep(1000); //Sleep 1 seconds and check again.
    }

    //Rest of your code  
}

Upvotes: 1

John Koerner
John Koerner

Reputation: 38077

Since you have the code for exe2, you can add a Debugger.Launch() to the code and that will stop the program and allow you to attach a debugger at that line of code.

You can add this as the first line of the main method to ensure you catch it right at startup.

Upvotes: 1

Jorge Y.
Jorge Y.

Reputation: 1133

Visual Studio allows to debug child processes for some years now. I have tried it having the code for both projects (parent and child processes) and it works pretty well. Not sure if it could work in your case having only the code for the exe2 program, but I think it's worth the try. Set the exe1 as the debug target for the project, having the child debugging activated, and see if it hits breakpoints in exe2...

Upvotes: 4

Related Questions