Reputation: 801
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.
Upvotes: 5
Views: 6433
Reputation: 59258
Windows provides a way to start a debugger for any executable by setting the Registry value Image File Execution Options (MSDN on archive.org). The article describes exactly what you need: starting Visual Studio as the debugger.
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\<Yourapp>.exe
.Debugger
devenv /debugexe
Don't forget to remove it when done.
Upvotes: 1
Reputation: 3813
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. :)
Upvotes: 2
Reputation: 256
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
Upvotes: 11
Reputation: 17185
Two things here: You cannot debug the batch script using the VS debugger. So when your exe returns the result is picked up by the script but you can't single-step trough that. To debug a batch script, flood it with pause
commands and leave echo on
to see what's going on.
To attach to your exe when it is started from the script, there are a couple of possibilities. One is (temporarily) modifying the exe to wait at startup a certain time (a few seconds or so) so you have time to attach the debugger. There's even a Win32 function to check whether a debugger is attached, if you want to. I remember having seen a possibility using the registry to automatically launch a process with the debugger, but I can't recall the exact procedure now.
Edit: Here's a topic describing the idea in short (answer 1).
Upvotes: 1