views:

281

answers:

3

Using VS .NET 2003. Would like to run the .exe from outside the IDE (i.e. command prompt or double clicking .exe icon in windows) However, still want break points to hit in the IDE.

How do I set this up?
(Running from outside IDE but IDE seeing it as run from "Debug" -> "Start")

Thanks.

+10  A: 

On the Debug menu, choose the "Attach to process" option to attach a debugger to your externally-running application.

David M
Just opened the debug process dialog box, didn't select anything closed it and went to rebuild. Unable to attache to machine TT2 Do you want to continue anyway? (Continue nothing happens)
Tommy
Make sure you also choose the correct code type (Managed, Native, Script, etc..) for the application that you are attaching to (or choose automatic).
adrianbanks
OK, got it now. However, I have to re-attach it each time I run? I had to put a delay in the code so I could go and attach it while it is running (so it shows up in the Available Processes Box) Is there a better way of doing all this?
Tommy
Add a call to System.Diagnostics.Debugger.Break() at the start of your application. When you run it, it will throw up a box allowing you to choose a Visual Studio instance to attach to it with. You will need to remove this call for your release build though!
adrianbanks
@Tommy: I you need to attach at the very beginning, why not start debugging from IDE? Why do you need to start .exe outside of IDE?
Paul
Alternatively there is: if (!System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Launch();
Robert Paulson
+2  A: 

Visual Studio enables Just In Time Debugging by default. If you haven't turned it off you can call DebugBreak() and you will get a popup allowing you to attach a debugger. If you don't attach a debugger then the program will exit, so you could try wrapping the DebugBreak call in a MessageBox or some other conditional code based on an environment variable or config item.

Dolphin
I am actually using C. Is DebugBreak() still the correct function? Program gives me "there was an error with...." when I use DebugBreak()
Tommy
@Tommy: Maybe you have no Just-In-Time debuggers set up, in this case when DebugBreak is executing system searches for JIT debugger, can't find it and the program terminates.
Paul
Looks like its enabled for all 3 types...
Tommy
A: 

Since it is C the call to DebugBreak() is correct - this will give you a nasty error dialog (different look depending on the OS), which should have a 'Debug' option. If you click this you should get a dialog to select one of the installed debuggers (VS.NET shoud be among them). Selecting it should bring you to the DebugBreak() line. However this can fail if the debugger can not find the pdb files for your app - in that case you will just get the disassembly view and no source code view.

You can also use WinDBG and the 'Open executable option' - again it will need the pdb files to yield anything useful.

S.Skov