Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I debug an executable that is called by a batch file within that context?

I'm running into a tricky little problem. I have a compiled C# executable that is called with arguments in a batch file. I would like to run this executable through the VS2012 debugger, however I am unsure of how to attach the debugger to the executable as it is run from the batch script.

I am not able to set the batch script as the project's debug startup file (only .exes), and the only process I can find that is associated with the batch file is cmd.exe, which does not allow for debugging. I have added a pause to the beginning of the batch script so ideally the process should be running and I should be able to attach it, but I can't find anything of the expected name.

Anyone know how to do this? It seems like a pretty straightforward problem but I can't quite figure out how to get it into the debugger.

like image 475
UpQuark Avatar asked Dec 12 '22 05:12

UpQuark


2 Answers

Or simply add a Debugger.Launch() at the start of your code:

System.Diagnostics.Debugger.Launch();

It will prompt you with all available VS debuggers, choose the one your are currently using.

See link for more information: http://msdn.microsoft.com/en-us/library/system.diagnostics.debugger.launch(v=vs.110).aspx

like image 127
jpgauthier Avatar answered May 16 '23 06:05

jpgauthier


Add this line:

System.Diagnostics.Debug.Fail("stopping!");

to a good place in your code. Run your app. Hit "Retry" and you should be prompted for a debugger. Choose Visual Studio.

Edit:

jp.gauthier's solution to use System.Diagnostics.Debugger.Launch(); is much cleaner. Upvote his instead. :)

like image 36
David Crowell Avatar answered May 16 '23 06:05

David Crowell