In the debug properties of my c# project I have selected Start External Program and selected the .exe of the program I wish to attach the debugger to. However, now I need to launch the program from a .bat file not the .exe but VS2005 does not seem to allow this. Is this possible?
EDIT: To clarify, I need to debug the .exe that gets spawned from the .bat. Not debug the .exe launched f开发者_开发技巧rom "start external program" and also have the .bat execute as well.
Cheers!
If you don't mind having to recompile the application every time you need to debug, you can throw in a temporary call to System.Diagnostics.Debugger.Break() on startup. This will invoke the just in time debugging dialog and allow you to attach at the point that Debugger.Break() is called.
You can also enable JIT debugging for an exe with a given name using a tool called "Global Flags" / gflags. With this option, there is no need to modify the source or recompile.
Download the debugging tools for windows from here: http://www.microsoft.com/whdc/Devtools/Debugging/default.mspx
Run gflags.exe in C:\Program Files\Debugging Tools for Windows\gflags.exe
Select the image file tab
Enter the name of your exe, e.g. myapplication.exe at the top and press tab
Select Debugger near the bottom and enter vsjitdebugger.exe
As long as this flag is set, you will be prompted to debug the application whenever it starts.
Yet another option is to add a command line switch to your application and batch file (e.g. /break) and only call Debugger.Break() when it is passed. This avoids the need to recompile when you decide to debug, and also eliminates the hassle of having to check off the debugger option in gflags every time.
The obvious advantages of these approaches is that they eliminate any kind of race between when the application starts and when you can attach.
Assuming there is no other simple answer, you could use a macro to start and attach to the process. There is a sample macro in the Samples macro project that gets installed with Visual Studio but I've pasted a relevant snippet below. Once have a macro to execute the batch file, pause for a bit, then attach to the process - you can add the macro to the Visual Studio toolbar so you can run it with a click.
' Lifted from Samples.vsmacros '
' VSDebugger.AttachToCalc '
Sub AttachToCalc()
Dim attached As Boolean = False
Dim proc As EnvDTE.Process
For Each proc In DTE.Debugger.LocalProcesses
If (Right(proc.Name, 8) = "calc.exe") Then
proc.Attach()
attached = True
Exit For
End If
Next
If attached = False Then
MsgBox("calc.exe is not running")
End If
End Sub
The only way to debug an EXE file that you don't start directly is to attach the debugger to an already running process. That requires that you have enough time to attach to the process and that may not always be the case.
I would suggest to isolate the factors involved in needing a batch file to start your program. I haven't ever come up a case where I couldn't debug an executable by specifying all the information in the debug section of the project config.
The three things to keep in mind when making an exe the debug target are:
Path to the executable. It needs to be full path. If the executable is output by your project you can use VS macros like
$(OutDir)
to specify it.Command line parameters. Those go in a separate text box, not in the same one as the path to the exe.
Working directory. That's the directory out which the executable is launched. Some programs might be sensitive to that. Default for C# project is the output directory, from memory.
If there is a need to run the batch file every time you debug then you can try editing it to not call the executable and run that batch file as a post-build step.
I would suggest using System.Diagnostics.Debugger.Launch()
enclosed in something conditional to keep it from being called outside a debug situation. This gets you a dialog asking for which debugger to use. If you have your project loaded, the dialog shown will have an etry allowing you to use your already running instance of VS.
精彩评论