Runar Ovesen Hjerpbakk

Software Philosopher

Debugger.Launch() in Services on Windows 8

I tried debugging (sorry Uncle Bob) a regular Windows service on Windows 8.1 the other day. As so many times before, I started with adding Debugger.Launch() in the beginning of the service, compiled it under Debug, copied the DLLs and started the service.

In Windows 7, the JIT debugger selection popup would then appear, and the correct instance of Visual Studio could be selected to perform the debugging session. This time though, nothing.

As always when nothing happens in Windows, I checked the event viewer.

Debugger launch in services on Windows 8

An unhandled Microsoft .NET Framework exception occurred in WindowsService.Server.exe [6652]. Just-In-Time debugging this exception failed with the following error: The operation attempted is not supported.

Check the documentation index for 'Just-in-time debugging, errors' for more information.

So I did.

The Solution

You need to change a registry key to enable debugging of a Session 0 process (like a windows service) on Windows 8. The Visual Studio JIT Debugger has its AppIDFlags set to APPIDREGFLAGS_IUSERVER_ACTIVATE_IN_CLIENT_SESSION_ONLY (0x20) and APPIDREGFLAGS_IUSERVER_UNMODIFIED_LOGON_TOKEN (0x8), thus preventing debugging.

Change the key to 0x8 by running the following command:

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

When your code works and you never want to debug again, restore the default value 0x28 by running:

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