Check if application was started from within Visual Studio debug session

前端 未结 3 1826
眼角桃花
眼角桃花 2020-12-03 02:20

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 (o

相关标签:
3条回答
  • 2020-12-03 03:08

    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

    0 讨论(0)
  • 2020-12-03 03:17

    Testing whether or not the module name of the current process contains the string ".vshost" is the best way I have found to determine whether or not the application is running from within the VS IDE.

    Using the System.Diagnostics.Debugger.IsAttached property is also okay but it does not allow you to distinguish if you are running the EXE through the VS IDE's Run command or if you are running the debug build directly (e.g. using Windows Explorer or a Shortcut) and then attaching to it using the VS IDE.

    You see I once encountered a problem with a (COM related) Data Execution Prevention error that required me to run a Post Build Event that would execute editbin.exe with the /NXCOMPAT:NO parameter on the VS generated EXE.

    For some reason the EXE was not modified if you just hit F5 and run the program and therefore AccessViolationExceptions would occur on DEP-violating code if run from within the VS IDE - which made it extremely difficult to debug. However, I found that if I run the generated EXE via a short cut and then attached the VS IDE debugger I could then test my code without AccessViolationExceptions occurring.

    So now I have created a function which uses the "vshost" method that I can use to warn about, or block, certain code from running if I am just doing my daily programming grind from within the VS IDE.

    This prevents those nasty AccessViolationExceptions from being raised and thereby fatally crashing the my application if I inadvertently attempt to run something that I know will cause me grief.

    0 讨论(0)
  • 2020-12-03 03:19

    Try: System.Diagnostics.Debugger.IsAttached

    0 讨论(0)
提交回复
热议问题