Petter Nordlander
Petter Nordlander

Reputation: 22279

VS Debug DLL invoked from system service

I am developing a DLL (in Visual C# Express) with some plugin logic for an application.

Is there any way to debug this DLL, once it get's used by the application? The application is running as a service on Windows, and it's a COTS application, meaning that it's not a C# project I can start debugging from.

I am aware that there are limitations regarding debugging in Visual C# Express. Is this task possible in Visual Studio Pro?

What I want to acheive is to be able to step through the logic, set break points to see what happends when the call comes. Any clues?

Upvotes: 1

Views: 1064

Answers (3)

dugas
dugas

Reputation: 12493

You could use the System.Diagnostics.Debugger.Break() method.

Upvotes: 1

ssube
ssube

Reputation: 48327

While I'm not entirely sure how this works with services specifically, you can set an arbitrary debug target.

In the debug panel (in project properties), you can set any executable or other command as the debug target. The process spawned by this command will have the debugger attached, regardless of whether where the executable came from. There are no requirements that the target be a C# project, or any Visual Studio project. This should be available in both Express and Pro. It's possible to attach to a later process (if you have a launcher), but that's probably beyond your current scope.

You then set breakpoints in your code as usual, and when the code is hit (regardless of whether your code calls it or the host executable), the breakpoint will be triggered. Depending on how much information you have about the host, you may be able to effectively debug it as well; even if you have no information, you'll be able to step through the assembly.

The only requirement here is that the target load and run your code. Depending on the context (plugin to a program, injected dependency, whatnot) this is of varying difficulty. The technique is used in a number of places, particularly plugin systems where you may not be able to debug the actual host, but still want to do so with your plugin.

Another, slightly uglier variation, is to force the host to break and self-identify. This is only useful for debugging, as it's very disruptive. The typical method is to show a message box (modal) with the process ID. The process will then be suspended until the message is dismissed, and a debugger can be attached. This becomes more difficult with services, although there are still ways to publish your information in a blocking fashion.

Upvotes: 1

esertbas
esertbas

Reputation: 486

I am not sure about VS Express but, normally,

  1. Open Visual Studio
  2. Open your Solution (Windows Service Project)
  3. Debug -> Attach To Process
  4. Select your Service from Available Process List
  5. You may use breakpoints and other stuff.

Hope it helps.

Upvotes: 3

Related Questions