Different Ways to Start Processes under a Debugger

The other day I got a question about how to start a process under the debugger and there are quite a few different ways to do that. I thought I’d discuss them all because the information about how to do each of these is scattered around various spots in the Visual Studio documentation. As the title says, this article is about starting processes, if you want to attach to processes in Visual Studio that’s a matter of the Debug, Attach to Process, or Tools, Attach to Process menu items. Finally, these techniques work for both managed and native development.

Obviously, if you have a single console or GUI program (i.e. not a service application), you know that pressing F5 will debug your process, which is the simple case. Things get a little more interesting if you have a DLL-type project, such as a class library or COM object. Say for example you are working on a MMC Snap-in written in C#, which means your code runs inside the Microsoft Management Console (MMC.EXE). To set up debugging for your DLL, you’d go to the project properties, Debug tab, and choose the “Start external program” button and enter “C:Windowssystem32mmc.exe” as the program to debug.

If you had the opposite situation where you have a native DLL project that loads into a managed EXE, in the project properties for the native DLL, you’d navigate to the Configuration PropertiesDebugging node. In the Command edit control you would type the complete name to the managed EXE that loads your DLL. Since you’ve got both managed and native code in the same process, you’ll also want to set the Debugger Type field to “Mixed” so you can single step between managed and native. Now that Visual Studio 2010 has full x64 mixed debugging support, there’s no excuse not to upgrade.

Where things get much more interesting is if you have a two EXE processes that you need to debug. Let’s say I have a client/server solution that’s got those two EXEs and I need to debug the startup of both of those projects. What I’ve seen most people do to accomplish this is open up two instances of Visual Studio, one for each project, and press F5 in both. That works, but there’s a hidden trick in Visual Studio where you can put both EXE projects in the same solution. After combining the two projects in the solution, right click on the Solution and select Properties.

In the Solution Property Pages dialog, navigate to the Common PropertiesStartup Project node. The default option selected will be the “Single startup project.” The trick is to select the “Multiple startup projects:” radio button and for each project, set the Action to Start for all projects.

Note that the default action for multiple EXE projects is None so other than the non-selected project in the Solution Explorer won’t run. As you can see from the dropdown above, you can choose to start the project without debugging so your client or server won’t stall.

As the multiple start option is a solution level option that means your settings are saved to the hidden .SUO file in the same directory as the solution itself. If you delete the .SUO file, you’ll need to go update the solution options to have the multi process startup execute your multiple EXE programs.

For a final bonus tip, what if you have the situation where one of the processes you need to ensure is started you don’t have the source for like a program from a third party or Microsoft? You can still start the other program with your particular EXE project, by right clicking on your solution, selecting Add, Existing Project from the context menu. In the Add Existing Project dialog, browse to the directory with the particular EXE file, select the EXE file, and click the Open button. In the screen shot below I’ve added NOTEPAD.EXE as a third EXE file.

With that EXE added you can treat it just like one of your projects in the Solution Property Pages dialog as I’ve shown above.

Ladies and gentlemen, go start your EXEs! As always, let me know if you have any questions.

John Robbins

View Comments

  • I have a native application that is launched by another application with relatively complex command line options. It runs and exits quickly so attaching is not an option. For me, then, the best option is to set the debugger using gflags for my exe to vsjitdebugger.exe.
    I do not know the command line option for this, but I do it through the gflags GUI, on the image tab.

  • How about launching an exe on a remote machine instead of the machine hosting Visual Studio? I run into trouble with the working directory setting a lot.

  • Tim,
    Yep, in the case of process A starting process B, GFLAGS.EXE is definitely the best approach. In the case I'm covering it's parallel starting.
    Chris,
    The only difference in starting a process on the remote machine is setting the remote debugger option. The working directory in the remote debugging case is the working directory on the *REMOTE* computer where your application is running.
    Hope it helps!
    - John Robbins

  • Is it possible to start an exe from inside VS and later attach the VS instance the EXE was started from to a second process (sources of both exes are in the same solution)?

  • Daniel,
    Could you rephrase your question? I don't understand what you're asking.
    Thanks,
    John Robbins

  • Hi John,
    I have a sliverlight app which is calling a web service (WCF). During F5, I am not able to step into the web service code. Any idea how I can do that.

  • John,
    How can I attach the debugger to a .Net dll invoked via a COM interface from a COM app (in this case VBA)? Under VB6 you could run a seperate version of VS with the dll component and a debug point set, and this would then be invoked by another VS instance running the main app. I dont think .Net works in this way. Any ideas?

  • NativeCPP,
    You can't, unfortunately because that's a step across processes. Attach a debugger to the web service code and set a BP where you want to stop. In the Silverlight app, step over the code that makes a call and you'll stop in the other debugger.
    VinceG,
    Your .NET DLL has to be running in a process. What's the name of that process? That's the one you want to attach to.
    Hope it helps!
    - John Robbins

  • Great article! Exactly what I needed. I have Vs2010 Prof and want to change to debug type to mixed. Problem: I simply CANNOT see the dll property page the way it is shown in the article! Alt+Enter opens a differnt property page for a single project. The property page for the whole solution looks exactly as shown - but with only the "Configuation" node - the debugging node and all others a missing?
    What am I missing? Is this a feature of a special VS version?
    Thanks for your help
    Martin

Recent Posts

How to Navigate Azure Governance

 Cloud management is difficult to do manually, especially if you work with multiple cloud…

6 days ago

Why Azure’s Scalability is Your Key to Business Growth & Efficiency

Azure’s scalable infrastructure is often cited as one of the primary reasons why it's the…

3 weeks ago

Unlocking the Power of AI in your Software Development Life Cycle (SDLC)

https://www.youtube.com/watch?v=wDzCN0d8SeA Watch our "Unlocking the Power of AI in your Software Development Life Cycle (SDLC)"…

1 month ago

The Role of FinOps in Accelerating Business Innovation

FinOps is a strategic approach to managing cloud costs. It combines financial management best practices…

1 month ago

Azure Kubernetes Security Best Practices

Using Kubernetes with Azure combines the power of Kubernetes container orchestration and the cloud capabilities…

2 months ago

Mastering Compliance: The Definitive Guide to Managed Compliance Services

In the intricate landscape of modern business, compliance is both a cornerstone of operational integrity…

2 months ago