Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to detect if a debugger is attached to another process from C#?

Tags:

c#

debugging

I have a program that Process.Start() another program, and it shuts it down after N seconds.

Sometimes I choose to attach a debugger to the started program. In those cases, I don't want the process shut down after N seconds.

I'd like the host program to detect if a debugger is attached or not, so it can choose to not shut it down.

Clarification: I'm not looking to detect if a debugger is attached to my process, I'm looking to detect if a debugger is attached to the process I spawned.

like image 637
Lucas Meijer Avatar asked Feb 02 '10 22:02

Lucas Meijer


2 Answers

if(System.Diagnostics.Debugger.IsAttached) {     // ... } 
like image 117
Bryan Watts Avatar answered Sep 19 '22 21:09

Bryan Watts


You will need to P/Invoke down to CheckRemoteDebuggerPresent. This requires a handle to the target process, which you can get from Process.Handle.

like image 40
itowlson Avatar answered Sep 18 '22 21:09

itowlson