Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if application was started from within Visual Studio debug session

I am working on an application that installs a system wide keyboard hook. I do not want to install this hook when I am running a debug build from inside the visual studio (or else it would hang the studio and eventually the system), and I can avoid this by checking if the DEBUG symbol is defined.

However, when I debug the release version of the application, is there a way to detect that it has been started from inside visual studio to avoid the same problem? It is very annoying to have to restart the studio/the computer, just because I had been working on the release build, and want to fix some bugs using the debugger having forgotten to switch back to the debug build.

Currently I use something like this to check for this scenario:

System.Diagnostics.Process currentProcess = System.Diagnostics.Process.GetCurrentProcess(); string moduleName = currentProcess.MainModule.ModuleName; bool launchedFromStudio = moduleName.Contains(".vshost"); 

I would call this the "brute force way", which works in my setting, but I would like to know whether there's another (better) way of detecting this scenario.

like image 846
Grimtron Avatar asked Sep 19 '08 13:09

Grimtron


People also ask

How do I start an application in debug mode?

Press Ctrl + Alt + F5 (or Shift + F9 ) to launch the app in debug mode. Choose Run -> Attach to process and select the signature of an app to enable the debug mode, which is already installed via adb.

How do I see what Processes are running in Visual Studio?

To open the Processes viewFrom the Spy menu, choose Processes. The figure above shows the Processes view with process and thread nodes expanded.


2 Answers

Try: System.Diagnostics.Debugger.IsAttached

like image 119
TraumaPony Avatar answered Oct 07 '22 19:10

TraumaPony


For those working with Windows API, there's a function which allows you to see if any debugger is present using:

if( IsDebuggerPresent() ) {     ... } 

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

like image 37
William Casarin Avatar answered Oct 07 '22 17:10

William Casarin