1

I've created a windows service in C (using WinAPI) and I want Visual Studio to automatically attach to the process of the service when I start the service from the Services panel. How can this be achieved. P.S. Putting a MessageBox at the service's initialization function and than manually attaching is no the kind of solution I'm looking for.

conectionist
  • 2,094
  • 2
  • 25
  • 43
  • 2
    Visual Studio only supports the [attach to process](http://msdn.microsoft.com/en-us/library/7a50syb3.aspx) route that you're trying to prevent. A workaround would be to write your own service host, that loads your service executable and calls into its exports. A .NET implementation can be found [here](http://stackoverflow.com/questions/2255335/how-to-runf5-windows-service-from-visual-studio) including additional information. – IInspectable Sep 05 '13 at 13:34

2 Answers2

2

I found the solution.Simply add the following key in the registry:

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\THE_NAME_OF_YOUR_SERVICE_EXECUTABLE.exe]

And add the following value and data:

"Debugger"="vsjitdebugger.exe"

Note that you'll have to remove this value when you're done testing, otherwise Visual Studio will prompt a message to attach whenever the service is started.

This works under Windows XP x32. I haven't tested on anything else.

conectionist
  • 2,094
  • 2
  • 25
  • 43
  • Valuable information, thanks for sharing this. Do you happen to have a link with documentation, or a blog entry? And what registry data type is the value? – IInspectable Sep 09 '13 at 20:39
  • Sorry, I don't have any documentation. A friend showed this to me, but he doesn't remember where he found this info. The type is REG_SZ. – conectionist Sep 10 '13 at 13:29
0

Place __asm int3; at start of your code or place where you want attach debugger.

vasylz
  • 157
  • 1
  • 4
  • 1
    The supported procedure is [`DebugBreak`](http://msdn.microsoft.com/en-us/library/windows/desktop/ms679297.aspx). Inline assembly is not supported [on Itanium and x64](http://msdn.microsoft.com/en-us/library/4ks26t93.aspx) processors. – IInspectable Sep 06 '13 at 14:04
  • This works but it involves modifying and recompiling the code (and modifying int back when you're done). While it's more elegant than the "MessageBox" solution I said I didn't want, it's still not really what I'm looking for. – conectionist Sep 09 '13 at 06:40