Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically detach debugger

Tags:

c#

.net

debugging

I have a 3rd party library that is doing something internally that causes it to slow down greatly when the debugger is attached, even in release mode.

I have found 100's of explanations of how to detach the debugger manually in Visual Studio by going to Debug -> Detach process. However I have yet to see anyone provide a example where a program could detach any attached debuggers to itself.

Basically is there an detach version of Debugger.Launch()?

like image 272
Scott Chamberlain Avatar asked Sep 20 '11 05:09

Scott Chamberlain


1 Answers

According to Why can't you detach in interop-debugging?, the CLR doesn't support detaching of processes. However Visual Studio can do it. But the article is 5 years old so can you use DebugActiveProcessStop from the Windows Api via pinvoke?

BOOL WINAPI DebugActiveProcessStop(
  __in  DWORD dwProcessId
);


[DllImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool DebugActiveProcessStop([In] int Pid );

Edit: Just tried this: On the current process it gives Access Denied even if elevated.

Also is there anything in the CLR Managed Debugger (mdbg) Sample 2006 or 2011 version

Finally this article explains what you need to do to use ICorDebug::Detach and I suppose visual studio does do this.

like image 59
Preet Sangha Avatar answered Sep 21 '22 05:09

Preet Sangha