Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug a C# .NET application in Visual Studio 2010 that is started from another process

I have a .NET GUI application written in C# and a PDF printer. The PDF printer has a field where you can set a command to start an external application.

In this case, I can print a document with this printer and the printer starts my EXE file with the filepath to the generated PDF file as argument. How can I debug my application when it is started from the printer?

In Visual Studio 2010, I can set debug information for command line arguments, and this works fine. But if the application is started from the printer the application doesn't work fine. Therefore I want to debug my application when it is started from printer. How can I do this? Is there a parameter to start an EXE file in debug mode or something like this?

like image 462
CubaLibre Avatar asked May 20 '11 15:05

CubaLibre


3 Answers

Try to attach to the process:

http://msdn.microsoft.com/en-us/library/c6wf8e4z.aspx

To attach to a running process 1.On the Debug menu, select Attach to Process. If no project is open, select Attach to Process on the Tools menu.

2.In the Attach to Process dialog box, find the program that you want to attach to from the Available Processes list.

a.If the program that you want to debug is running on another computer, you must first select the remote computer. For more information, see How to: Select a Remote Machine.

b.If the process is running under a different user account, select the Show processes from all users check box.

c.If you are connected through Remote Desktop Connection, select the Show processes in all sessions check box.

3.In the Attach to box, make sure that the type of code you will debug is listed. The default Automatic setting tries to determine what type of code you want to debug. If the automatic setting is not appropriate:

a.Click Select.

b.In the Select Code Type dialog box, click Debug these code types and select the types to debug.

c.Click OK.

4.Click Attach.

The Available Processes list is displayed automatically when you open the Processes dialog box. Processes can start and stop in the background while the dialog box is open. However, the contents are not always current. You can refresh the list at any time to see the current list of processes by clicking Refresh.

You can be attached to multiple programs when you are debugging, but only one program is active in the debugger at any time. You can set the active program in the Debug Location toolbar or the Processes window. For more information, see How to: Set the Current Program.

All Debug menu execution commands affect the active program. You can break any debugged program from the Processes dialog box or break all attached programs from the Debug menu. For more information, see How to: Break Execution.

like image 144
Alexandre Brisebois Avatar answered Sep 20 '22 20:09

Alexandre Brisebois


You can attach to a process when it starts using a small registry tweak.

Go to

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options

Create a new key with the name of the executable as it will appear in Task Manager, for example, myapp.exe. Under this, create a new string value called debugger and set it to vsjitdebugger.exe.

Now, when the EXE file is triggered, a window will appear asking which debugger to attach to.

like image 44
AntonyW Avatar answered Sep 24 '22 20:09

AntonyW


Consider adding a call into your code that explicitly requests that the debugger be attached at the current location. This has been around since Win32 days, and surfaces in .NET as System.Diagnostics.Debugger.Break (and System.Diagnostics.Debugger.Launch).

You can also add logic to decide when to trigger this if you don't want to do it the first time through:

   #if DEBUG
      if (++staticCounter > 3) System.Diagnostics.Debugger.Break();
   #endif

And, of course, you'll want to disable it for production.

like image 44
codingoutloud Avatar answered Sep 21 '22 20:09

codingoutloud