How to Fire Windows Service start event? - windows

From the thread - Fire windows service stop event I know how to fire an event whenever a system service has stopped.
Now I want to fire an event when a service starts. But I can't find any service control code relating service starting. Is there anther way to implement it?
Any ideas will be appreciated.

Check out NotifyServiceStatusChange
Enables an application to receive notification when the specified
service is created or deleted or when its status changes.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms684276(v=vs.85).aspx
SERVICE_NOTIFY_START_PENDING (0x00000002) - Report when the service is
starting. The hService parameter must be a handle to the service.
You could also use WMI:
How can I monitor status changes of windows services under windows xp?

Related

I want to create an application to receive events raised from Windows Application

I want to create an application to receive events raised from Windows Application.
For example, if windows application is minimized(skype), event should be raised in my application. likewise, i want to handle events of windows application in my console application.

How to hook the service control handler from a Topshelf service

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.
Topshelf won't interfere with you calling these Win32 APIs.

Receive event before Windows enters standby or hibernate using Win32 API

I'm working on a small chat application written in C++/Qt. My users are complaining that the connection is not shut down gracefully when they are closing the laptop lid and the computer enters standby.
Is there a Win32 hook available that is called when Windows is about to enter standby?
WM_POWERBROADCAST
Reimplement QCoreApplication::winEventFilter in your application to handle Windows standby events.

Detect shutdown as SYSTEM user

I have an application that is started by a service. The application has no User Interface, it contains a DataModule and a TMyApplication implementation that calls Forms.Application.HandleMessage in a loop (like SvcMgr.TServiceApplication). I hooked the MainWindow to log any messages the hidden window of Forms.Application receives.
If I manually start the application so it runs on my useraccount, I receive WM_QUERYENDSESSION and WM_ENDSESSION messages on the hidden window of Forms.Application when logging off/shutting down.
If I start my application using the service, the application runs under the SYSTEM account. When running under the system account I receive only one message, $0000001A (WM_WININICHANGE?), at systemshutdown before my application is forcefully terminated by Windows. So no WM_QUERYENDSESSION and WM_ENDSESSION messages.
How can I detect a Windows shutdown on a SYSTEM account and close my application gracefully?
Have your service notify your sub-application.
Here's some information about how the service control manager notifies your service:
http://social.msdn.microsoft.com/Forums/en/netfxbcl/thread/063cef3a-de94-44d5-8f2e-4e63c3cfdee8
Do a FindWindow, and PostMessage(hwnd, WM_ENDSESSION) yourself.

Can a Windows Service receive Windows Messages?

I created an application to receive a broadcasted windows message which works fine. When I turn it into a service, install it, and start the service, the service doesn't receive the message.
The service probably must be granted access to the desktop. Do this from the Service properties, "Log On" tab, Log on as Local System account, and check "Allow service to interact with desktop".
Having windows that run as SYSTEM on a user desktop is a security issue, you should really use some other form of IPC (If all you need to do is notify the service without providing any other data, a (global) named event should be enough)
If on the other hand you want to catch notifications from windows itself about device changes, power and session events etc you don't do that with messages when you are running as a service, you get those events in your HandlerEx
Who's sending the broadcast? Unless the component sending the broadcast is running as Local System, it does not have the privilege to send window messages to window handles in different sessions.
Since all Windows Services (since Vista) run in Session 0, and almost all other components run in Session >=1, most probably that's why you are not receiving the broadcast.

Resources