Deamonpog
Deamonpog

Reputation: 815

Debug two programs where one of them starts another program

There is a program (a WPF app written in C#) that calls the other program ( commandline program written in C++) . They both are in the same solution. At some points, the caller gets some stuff done by using the other program by using the methodology provided in System.Diagnostics.Process. I want to debug the both programs while testing this project. How to do that? is there some easy method like "attaching" which we do to libraries?

--EDIT--
Process A starts the process B. Then A waits until process B exits. Furthermore, B something very small like dir on the command prompt which exits quickly.

Upvotes: 2

Views: 1970

Answers (4)

Deamonpog
Deamonpog

Reputation: 815

  • Since it was hitting the deadline there was no time to wrap C++ with managed code as suggested by Wilbert.
  • Running the two programs on separate VS also doesn't work ( confirmed by trying out ) since program B is run only when necessary by the program A and each time command line arguments for starting B were different and generated at run-time. ( cannot be done even in same VS since debugging one process seems to pause execution of the other process )
  • Calling Debug in the program is not possible since B is a native C++ application. To access visual studio extensions, the project needs to be a managed project. Converting the current project to managed was not possible due to a problem with my version of VS. ( after installing Net Framework 4.5, VS 2010 needs to be updated to SP1 to be able to work use some features )

Applied Solution:

  1. In source of A , put a break-point below the place where command line arguments are generated for B (I put it at where proc.Start() is called).
    e.g.

    Process otherProcess = new Process();
    ...
    otherProcess.StartInfo.Arguments = "abc" + foo() + "," + bar();// what we need
    ...
    otherProcess.Start(); // break-point is here
    
  2. Run A in debug mode until the break-point is reached.

  3. Read the value of proc.StartInfo.Arguments and copy the value for later usage.
  4. Safely exit debugging A (stop debugging).
  5. Set command line arguments of B as the copied values (Project Properties > Debugging > Command Line Arguments)
  6. Start debugging B (right click on project > debug > start a new instance).

Upvotes: 2

Wilbert
Wilbert

Reputation: 7419

You could also write a small class that uses C++/Cli. To the outside, your class looks like any C# class, and can be used directly from C#. But inside the C++/Cli, you can freely call into C++. That way, you can remove the use of System.Diagnostics.Process and directly use the other program.

It's a bit of work up-front, but it allows you to directly step into the C++ code as you do with the C# code right now.

Upvotes: 0

Marius Bancila
Marius Bancila

Reputation: 16338

If I understand you correctly, just attach the debugger to the other process. From one instance of Visual Studio you can attach to more than just one process, but you cannot attach two debuggers to the same process. Make sure however, you attach the right debugger to each process, i.e. the native debugger to the C++ application and the managed debugger to the WPF application.

You can switched between the debugger processes from the Processes tool window (given they are both paused at breakpoints). The call stack and all the others are updated for the currently selected process there.

Upvotes: 1

progpow
progpow

Reputation: 1580

Run another instance of VS, open solution and try to connect to running process http://msdn.microsoft.com/en-us/library/vstudio/3s68z0b3.aspx

Upvotes: 0

Related Questions