Detect shutdown as SYSTEM user - windows

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.

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 Fire Windows Service start event?

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?

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.

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.

Are there special considerations for a windows service to send messages to user windows?

I have to write a Windows Service application (no GUI) that will monitor an event, and if it occurs will send a standard windows message to an application. The handle of the application will be given to the service by a DLL which is then unloaded, so a windows message is the way we wish to use.
The question though is whether the service needs to do anything special to use SendMessage to the window handle, given that it might be on a different screen or something in Vista. Is this possible, and if so, what do I have to do please?
User Interface Privilege Isolation (UIPI):
Microsoft Windows Vista and later.
Message sending is subject to User
Interface Privilege Isolation (UIPI).
The thread of a process can send
messages only to message queues of
threads in processes of lesser or
equal integrity level.
Source
You can read about User Interface Privilege Isolation (UIPI) here.
To get around this you can set uiAccess to true in your manifest file. You also have to make sure your application is signed using authenticode with a certificate from a signing authority such as VeriSign. This can get pretty expensive.
Session 0 isolation:
It is also my belief that you can't call SendMessage across sessions. So if you have a service running in session 0 you need to find another means to communicate with your process that would be running in a session > 0. Example: via pipe.
In Windows Vista, Windows 2008 Server and later all services run in session 0, and all applications that you start run in session > 0. This is called session 0 isolation. Here is a good document that has information all about session 0 isolation.
If you don't have access to the source of the program you want to send messages to, you could get around this by making an application that communicates with your service and acts as a proxy to relay the message to the application in its same session.
Overall:
If you develop your application on pre-Vista and it works fine. There is a very high chance it will be broken in Vista.

Resources