JIT Debugging using Visual Studio may fail when trying to debug a Session 0 Process on Windows 8

When you try to JIT debug a Session 0 process (like a windows service) on Windows 8 using Visual Studio Debugger, you may find it fails to launch the debugger and see below error in the Application Event Log

"Just-In-Time debugging this exception failed with the following error: The operation attempted is not supported".

The error is happening because by default on Windows 8 the AppIDFlags for VS JIT Debugger is set to 0x28 (40) in the registry. This value is a combination of below two flags

APPIDREGFLAGS_IUSERVER_ACTIVATE_IN_CLIENT_SESSION_ONLY = 0x20

And

APPIDREGFLAGS_IUSERVER_UNMODIFIED_LOGON_TOKEN = 0x8

The presence of the flag APPIDREGFLAGS_IUSERVER_ACTIVATE_IN_CLIENT_SESSION_ONLY would prevent the debugger to be launched in a different user session. To work around the issue one can remove the flag. To remove the flag from the default value (0x28) one can use the below command

reg add "HKCR\AppID\{E62A7A31-6025-408E-87F6-81AEB0DC9347}" /v AppIDFlags /t REG_DWORD /d 8 /f

And to restore the default value use the below command

reg add "HKCR\AppID\{E62A7A31-6025-408E-87F6-81AEB0DC9347}" /v AppIDFlags /t REG_DWORD /d 40 /f

NOTES:

  1. The work around described above involves the modification of registry keys. So it is strongly recommended to take a back up of registry keys being modified so that it can be restored back later, if required
  2. The AppIDFlags mentioned above can contain any combination of values given below

            #define APPIDREGFLAGS_ACTIVATE_IUSERVER_INDESKTOP 0x1

            #define APPIDREGFLAGS_SECURE_SERVER_PROCESS_SD_AND_BIND 0x2

            #define APPIDREGFLAGS_ISSUE_ACTIVATION_RPC_AT_IDENTIFY 0x4

            #define APPIDREGFLAGS_IUSERVER_UNMODIFIED_LOGON_TOKEN 0x8

            #define APPIDREGFLAGS_IUSERVER_SELF_SID_IN_LAUNCH_PERMISSION 0x10

            #define APPIDREGFLAGS_IUSERVER_ACTIVATE_IN_CLIENT_SESSION_ONLY 0x20

            #define APPIDREGFLAGS_RESERVED1 0x40

So before executing the commands described above note the current value and negate 0x20(32) to enable the debugger be spawned from session 0 and add the value 0x20(32) back if you want to restore the original value .