none
How to receive WM_ messages in a Windows Service?

    Question

  • Hi all, I currently have a vb.net windows app that overrides WndProc to receive device notificaitons when I hook it using RegisterHidNotification. Now I'm trying to convert it to a windows service and can't seem to find any sort of override for windows messages?

     

    Any help would be greatly appreciated

    Wednesday, November 14, 2007 9:37 PM

Answers

  • If you are using a service, you need to use RegisterDeviceNotification instead. This allows you to receive device notifications through a callback handler rather than through windows messages.

     

    Thursday, November 15, 2007 6:50 PM

All replies

  • Services don't (shouldn't) interact with the desktop and therefore don't have a window or WndProc. What messages are you trying to capture?

     

    Thursday, November 15, 2007 12:23 AM
  • I'm trying to receive WM_DEVICECHANGE so when a USB mass storage device is inserted it alerts the service.

    Thursday, November 15, 2007 3:27 AM
  • Each message have the following structure:

     

    (hWnd As IntPtr, msg As Integer, wParam As Integer, lParam As IntPtr)

     

    HWnd is the handle for the window (form or control) that shall receive the message, so it is obvious that without a window, no messages. If a WM_DEVICECHANGE message is generated in your system, it must be send to a window (form or control). If you can find that window (not quite obvious), you can either override the OnDeviceChanged method or make a handler for the DeviceChanged event (I presume that OnDeviceChanged raises the DeviceChanged event).

    Thursday, November 15, 2007 10:43 AM
  • If you are using a service, you need to use RegisterDeviceNotification instead. This allows you to receive device notifications through a callback handler rather than through windows messages.

     

    Thursday, November 15, 2007 6:50 PM
  • Thanks for the replies so far guys, so according to RegisterDeviceNotification Function (Windows)

     

    If hRecipient is a service status handle, SERVICE_CONTROL_DEVICEEVENT notifications are sent to the service control handler

     

    Now in a VB.Net service, is there a way to override the control handler to process the SERVICE_CONTROL_DEVICEEVENT or will I have to use RegisterServiceCtrlHandlerEx & HandlerEx?

    Thursday, November 15, 2007 8:40 PM
  • How do I use the Servicer Control handler in C#?

    Do you have any code that can be used?

    I am trying to figure out to create a Windows Serrvice to capture all new devices plugged in to a machine.

     

    thank you.

     

    Wednesday, December 12, 2007 12:00 AM