0

I have an existing Windows service that uses Topshelf very successfully.

I now need to have the service receive notifications when specific USB devices are connected / disconnected to / from the machine.

I can't see anything in Topshelf that would allow me to do this. But I believe that there is a way to do it using Win32 APIs like ServiceControlHandlerEx and RegisterDeviceNotification with the DEVICE_NOTIFY_SERVICE_HANDLE option.

Will using these Win32 APIs interfere with Topshelf? As I understand it, Topshelf wraps the service control handler itself so my service also trying to do the same thing may cause it some problems?

Update: It is possible to hook device notifications from the service control handler within a Topshelf based service. See sample project for details. Many thanks to Chris Patterson for his help.

Jack Hughes
  • 5,514
  • 4
  • 27
  • 32

1 Answers1

2

Topshelf won't interfere with you calling these Win32 APIs.

Travis
  • 10,444
  • 2
  • 28
  • 48
  • Thanks Travis. Do you know where I can get the equivalent to ServiceBase.ServiceHandle from? The only way I can see to get it at the moment is to derive my service from ServiceBase but that is something that Topshelf takes care of for me. I need the service handle so I can pass it through to RegisterDeviceNotification. – Jack Hughes Sep 20 '13 at 12:26
  • Sorry, stupid question. Use `var serviceController = new ServiceController("ServiceNameHere");` then use the `serviceController.ServiceHandle` property. – Jack Hughes Sep 20 '13 at 13:17
  • Well, that was easy on my part. – Travis Sep 20 '13 at 15:17