0

I Have written Two programs in C++, A and B. In the A program I call B with arguments (console Based Program).

What I want is when I call B want to be in the debug mode (known that I run the program A). How could I set that in the Visual Studio 2010 ?

Thank you

Blood-HaZaRd
  • 2,049
  • 2
  • 20
  • 43
  • Possible duplicate of [Can Visual Studio be made to debug child processes like WinDBG?](http://stackoverflow.com/questions/771039/can-visual-studio-be-made-to-debug-child-processes-like-windbg) – IInspectable Feb 06 '17 at 11:26

2 Answers2

1

When I have had the same issue I just added Sleep(10000); (or similarly) at the start of program B and then manually attached a debugger. You can also make that conditional on an environment variable/command line argument/...

I know that it is a hack - but it sort of works; and would also prefer a better solution.

Hans Olsson
  • 11,123
  • 15
  • 38
  • It is a hack, and requires that you *can* change the code for program B. Better solutions exist. The proposed duplicate offers two such solutions. – IInspectable Feb 06 '17 at 11:44
  • @IInspectable Depending on circumstances there might be better solutions, but the chrome-macro-link was dead, and the Image Execution Options means changing the registry back and forth. The child process tool seems to require VS 2013 Update 2; or later - and OP used VS 2010. – Hans Olsson Feb 06 '17 at 12:31
  • Writing a batch script to enable and disable the Image Execution Options is a matter of 2 minutes. It's convenient, too, because you don't have to manually go looking for the process to attach the debugger to. In addition, it is the only way to go forward, if you need to debug startup code. – IInspectable Feb 06 '17 at 12:57
-1

In program B at the start of main call ::DebugBreak() this will allow you to attach the debugger to your 2nd process. See: https://msdn.microsoft.com/en-us/library/windows/desktop/ms679297(v=vs.85).aspx

Richard Critten
  • 2,138
  • 3
  • 13
  • 16
  • No, this won't work. `DebugBreak()` breaks into the debugger, when a debugger is already attached. This is straight from the [documentation](https://msdn.microsoft.com/en-us/library/windows/desktop/ms679297.aspx): *"If the process is not being debugged, the function uses the search logic of a standard exception handler. In most cases, this causes the calling process to terminate because of an unhandled breakpoint exception."* – IInspectable Feb 06 '17 at 11:28
  • that made my prog crashes at the call !! – Blood-HaZaRd Feb 06 '17 at 11:30
  • I get a nice dialog box, paraphrasing "An exception has occurred. Do you want to Abort, Retry, Debug the process?" – Richard Critten Feb 06 '17 at 11:32
  • @RichardCritten: Yes, you do. **If** a debugger is already attached. – IInspectable Feb 06 '17 at 11:35