Debugging in multi-project under one solution in vs 2010 - c#

I have multiple projects in one solution. Project A (the starting project) starts Project B using Process.Start.
All the debugging methods work fine in project A, however after A starts B, not only do breakpoints not work, but also the output from calls to System.Diagnostics.Debug.WriteLine is not displayed.
Does anyone know how to debug in this situation?

In this scenario you have 2 processes running and you need to attach Visual Studio to both of them. Visual Studio supports attaching to multiple processes and getting it to do so is the same as attaching to a single process. Once the second process is launched do the following
Tools -> Attach to Process
Select the process
Hit Attach

The new process which is created at runtime will not have the debugger attached hence breakpoints and debug.writeline won't work.
You might be able to select "Debug" menu then "Attach to process" from within Visual Studio once the new process is running.

You are debugging the process that Project A is working and because you are starting a second process for Project B you have not attached your debugger to that process. So you need to attach to the second process.

You could add an call to System.Diagnostics.Debugger.Launch(); in the Main from Project B. So every time you start Project B it will ask you if you want to attach a Debugger.

Related

How to debug an exe with multiple dlls using breakpoints

I have a myapp.exe that after some complicated logic is run by another program. I wanted to debug the issues with myapp.exe just like visual studio preferably using breakpoints. What is the way to achieve this? The exe is a console application and is run on the spot. It's not a running process so I cant attach a debugger.
The expected behavior I would want is:
Do magic and set breakpoints for that exe and dlls
Call the exe from the other program
Visual studio hits the breakpoint and I can debug what is going on
Just use System.Diagnostics.Debugger.Launch() where you want to attach the debugger. You can place it just before your desired breakpoint location. Windows will ask you what debugger do you want to attach.
Another way is to check the System.Diagnostics.Debugger.IsAttached property and wait for the debugger like this (polling):
while (!Debugger.IsAttached)
{
Thread.Sleep(500);
}
The application will loop until you attach the debugger (in visual studio via attach to process command, Ctrl+alt+P ).
Again, just place a break point after or even in the loop and you're done.
This is a well know way used to debug a windows service application and can be useful also for your intent.

How do I attach a debugger to other process than I start in Visual Studio 2017?

An architecture which I have to deal with is quite complicated:
a native C++ Windows Service creates an out-of-process COM object (native C++, CoCreateInstance with context = CLSCTX_LOCAL_SERVER) which dynamically loads (AfxLoadLibrary) my C# DLL.
I would like to debug my DLL when I click Debug -> Start Debugging.
I know that I can debug it when I attach to COM object process using Debug -> Attach to process and I can use System.Diagnostics.Debugger.Launch() in my DLL code if I need to debug startup but none of this options work automatically.
I also tried this technique: https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/a329t4ed(v=vs.100) but the COM process runs as a SYSTEM user so that I cannot select my existing Visual Studio Solution with my C# project to use as a debugger, I can only start a new instance with only EXE loaded.
I tried "Start external program" too but what I need to start is a service which is not the process I want to debug, because my DLL runs in another process (the COM-one). So, debugging stops as soon as service is started (because I run it with a script, maybe this is a problem).
I don't see an option to automate it entirely with standard Visual Studio features. So I would propose you to write a simple Visual Studio extension. Just use the template and add a button to the menu or to some custom window, which on click will:
iterate through the running processes and wait until an instance of the dllhost.exe with your COM class guid on the command line, appears
when you find the process, attach to it using the Process2 interface (code in VB is available)
I'm not sure if it will help, but you may check my simple VS extension. I wrote it some time ago to configure symbols in the debugger.

How to debug C# program launched from an external exe written in Borland C++Builder

The following is my flow
//'''''''''''' ''''''''''''
//' Program A' --->Call CreateProcess----> ' Program B'
//' BCB6 ' ' C# 2010 '
//'''''''''''' ''''''''''''
I want to debug Program B in Visual Studio 2010. It is written in C#. I have set Properties -> Debug -> Start External Program to Program A's file path, but I cannot enter the breakpoint in main().
Does anyone know how to solve this issue?
Update: The following is my setting and code. I still cannot understand why I cannot enter the breakpoint in the C# program :-(
You have it around the wrong way. If you wish to debug the 2nd app (c#) Program B then that is the project you should have loaded up in Visual Studio.
In the debug settings for the c# app, set
Properties -> Debug -> Start External Program to Program A
This will allow you to then set breakpoints in Program B's c# code.
The same thing happens if you are working on a .dll project; you want to set breakpoints in it; but the dll is invoked by an .exe outside of your control (say a native process). You set the Start External Program as above and set the path to the external process.
Alternatively
You can Debug.Attach to Process but that takes careful timing, the process must be running first and you have to cross your fingers that the line in question isn't executed before you are able to set a breakpoint. (Normally you can't set a breakpoint until you attach first)
If Program A uses Program B, start Program A; open the solution for Program B in Visual Studio; and go under the debug drop-down and select "Attach To Process". This will let you put in breakpoints and debug through your code. You can also configure your builds to start Program A when you Build/Start Program B in VS, as explained in the answer from User4534....
In addition to MickyD's answer, I had a similar issue where I couldn't load the symbols for the C# app I was trying to debug. The app is launched by an external process.
Process A --> C# app (dll) to debug
Since timing was not an issue in my case, I attached the debugger to the external process. The solution for me was to select the right kind of code type instead of letting the Visual Studio determine it.
Debug -> Attach to Process
Select the right (external) process to attach to.
In "Attach to: Automatic: Native code"
Select... -> Debug these code types
Select the right code type for your project (in my case it was Managed (.NET 4.x) for a .NET Framework 4.7.2 project.
This solution worked for me and I was able to debug my C# app after Visual Studio loaded the right .pdb files that were generated.
Credit to this solution goes to Dan Sinclair from sitecore.stackexchange here

How to debug .NET console application from Visual Studio 2013

I've created a .NET 4.5 console application (an .exe file) with c# that processes some data in various files. The whole thing runs in less than one second. I have a problem with a testing version of the .exe that I'd like to step through in the Visual Studio 2013 debugger using particular input files (the filenames are passed as command line arguments). I can't do an "Attach to process..." since the process has completed too quickly to attach to it. How do I debug in this case.
Note that I'm from the python world, and the python equivalent of what I'm trying to do is python -m pdb pdb_script.py.
Thanks for any advice!
Right click on your project and go to "properties".
Then select the "debugging" tab on the left-hand side.
There's a box where you can input the desired command line arguments for use when running in debug mode.
EDIT: If you're asking how to start the debugger, then add some breakpoints to your code, then right click on the project in your solution and pick Debug > Start New Instance.
Alternatively you can right click and choose "Set as Startup Project", after which you can start debugging with F5 or the "Start" button at the top of the UI.
Add the following line in your code:
System.Diagnostics.Debugger.Break();
This will allow you to debug your application before it ends.
In Visual Studio, you can add commandline arguments before starting a debugging session by right-clicking your project, selecting properties and then going down to the debug tab.
Then you can start it in Visual Studio with f5
If that's too much trouble (because you are going to change the arguments a lot), you could do something like adding a Console.ReadLine to your program at the beginning that will give you a change to attach a debugger. You could even have an extra command line argument for debugging that will only pause for you to attach the debugger if you pass that argument.

Debugging an application called from another in Visual Studio?

I have a lobby application which invokes a client-application (think: League of Legends). They're two separate applications and the first invokes the second from itself - how can I get Visual Studio to debug this application as well?
You simply need to launch a separate Visual Studio, and then use Debug | Attach to Process to attach to the other process. The trick is using two Visual Studios.
Debug -> Attach to process
Select the executable from the list.
Make sure to select the right code type with the Select.. button.
Have you tried Debug -> Attach to Process?
Ref: http://msdn.microsoft.com/en-gb/library/vstudio/3s68z0b3.aspx
Like Matthew said Debug|Attach to process. If the other application is in a different service you might also want to look into remote debugging
The other answers are correct, but I just wanted to add another approach:
If you add both projects to a single Visual Studio Solution, you could:
Right-click the solution -> Properties -> Common Properties -> Startup Project
There you would select Multiple startup projects and select both the Lobby and the Client applications.
That way you can debug several VS projects without having to run several VS instances.

Categories