How to detect wake up from sleep mode in windows service? - c++

Good day! I write a windows service, which detects USB Flash drive connection and do something with connected drive. And now after tests i need to reprocess devices after wake up from sleep mode. I solve this problem when service work as windows program. To solve i additionally process DBT_DEVNODES_CHANGED, but service don't receive this message, it receives only DBT_DEVICE_ARRIVAL and DBT_DEVICEREMOVECOMPLETE. I also tried to check all messages handled by service except SERVICE_CONTROL_DEVICEEVENT and SERVICE_CONTROL_STOP when system is going to sleep and i don't get any message.
How can I determine when the system is waking up?

A normal application receives a WM_POWERBROADCAST:PBT_APMRESUMEAUTOMATIC window message, but a service will not get this message if it does not have a window. However, a service can receive SERVICE_CONTROL_POWEREVENT in its HandlerEx callback function, with the same parameters as WM_POWERBROADCAST
Notifies a service of system power events. The dwEventType parameter contains additional information. If dwEventType is PBT_POWERSETTINGCHANGE, the lpEventData parameter also contains additional information.
On Windows 8 and later, you can also use the PowerRegisterSuspendResumeNotification() function:
Registers to receive notification when the system is suspended or resumed.
The service equivalent of WM_DEVICECHANGE is SERVICE_CONTROL_DEVICEEVENT:
Notifies a service of device events. (The service must have registered to receive these notifications using the RegisterDeviceNotification function.) The dwEventType and lpEventData parameters contain additional information.

The best way to detect that system is waking up is process power messages.
Windows must process WM_POWERBROADCAST
Services must add SERVICE_ACCEPT_POWEREVENT to last call to SetServiceStatus and process SERVICE_CONTROL_POWEREVENT
My error was very stupid. I forget to add SERVICE_ACCEPT_POWEREVENT.

Related

How to know when the Winlogon desktop is ready for inputs?

I successfully used SendSAS in a service (Local System Account). I call the API four seconds after the service starts. It seems that whatever the boot process duration, Windows manage to cache (sort of) the call: the same code finally shows me a logon screen a few seconds after the power on, on a fast laptop (Win10), and also shows me the logon screen after a very much longer delay on a slow Server (2012R2) running virtualized (wmware).
I am also able to use CreateProcessAsUser (with an updated token) to inject a tiny executable in the Session 1, Station WinSta0, Desktop Winlogon. The process then uses SendInput to "auto-logon" the session (yes, this is an awful think to do do, I am aware of that).
My problem: if the tiny process starts "too early", nothing happens. If the service waits, say, 2 minutes, all is OK.
What API should I use (in the service or in the started process) to find out when the WinLogon desktop is ready to accept keyboard inputs?
I tried WTSGetActiveConsoleSessionId (in the service) and OpenInputDesktop (in the process) hopping that failure would indicate the need to wait, but with no avail.
When your process starts in session 1, attached to the WinSta0\Winlogon desktop, you can periodically test the Control Type of the currently IUIAutomationElement focused element.
The APIs to use are : IUIAutomation::GetFocusedElement and then IUIAutomationElement::GetCurrentPropertyValue for the UIA_ControlTypePropertyId property. When you successfully get a focused element of type UIA_EditControlTypeId, the Windows logon screen is ready to accept inputs.
Don't forget to add a Sleep call between each try.
Tested OK with Windows Server 2008R2 and 2012R2, and with Windows 10.

How to make an propertysheet application to facilitate "Closing Apps" action when we restart a machine

I am having a propertysheet application and I am doing some operation in it and now in the middle
I restarted my machine then all the applications or closed including my application.Instead of
forcely closing my application I want to make my application to support something similar to this when I press restart.
Exactly I should make application support same feature as in the above image.
Can anyone please let me know how can I solve this issue.
From MSDN: The WM_QUERYENDSESSION message is sent when the user chooses to end the session or when an application calls one of the system shutdown functions. If any application returns zero, the session is not ended. The system stops sending WM_QUERYENDSESSION messages as soon as one application returns zero.
You can handle this message in your main window. If there is no MFC macro for this message you can use the ON_MESSAGE macro in the message map.

How to notify client applications from a service on a Windows system with WinAPI and C++?

I was wondering, what is the best way to do the following using C++/WinAPIs on a Windows system?
I have a local service application along with running client applications (that run on each logged on user session account.) The service application needs to notify all client applications to perform a one time operation (say, read data from registry and process it.) How do you implement this mechanism of notifying all client apps of a one-time event?
Service could send a broadcast windows message for which all client apps would listen. Of course, client apps need to have a message loop. Have you looked at SendNotifyMessage function?
You could use an event with a particular name that all the apps agree on. Then reset it after a period of time.

An already running application to be informed about an incoming SMS - Windows Mobile 6.5

I have a C/C++ Windows Mobile 6.5 application that continuously runs on the phone. It needs to analyze the SMSes. How can I be notified in my application about an incoming SMS? I read in MSDN about the SmsSetMessageNotification() but this is starting an application once the SMS comes in, and in my case the application is already running, it just needs to be informed that a new SMS was received and then should have access to it in order to analize it and to decide if it's of any interest for the application or not.
So how can my already running application be informed about an incoming SMS which then needs to be analyzed?
I believe you need to use the IMailRuleClient to sign up for notifications of a text message being received.
There is a sample application as part of the sdk that demonstrates this functionality. You can download it from MSDN Code Samples
You can achieve your goal using the Radio Interface Layer (RIL) API. Download RIL.H to get started. Then use LoadLibrary and GetProcAddress to dynamically call the RIL functions from the DLL at runtime. You can setup a RIL notification callback to notify you when message activity happens. I found a code example here.

How can I Execute a Function when Windows Shut down

How Can I execute a function when Windows shutdown. Here is my scenario, I am mounting a drive using WNetAddConnection2 function in my application. Now I want user to set the option if the drive will be mounted on next system startup or not.
If he selects , not to mount on next startup , then I need to remove the drive using WNetCancelConnection2 , but this should only happen when user shutdown the system.
I can only think of only solution. Create a service which will check the user option and then decide whether to mount the drive or not.
Are there any other ways to go ahead with it?
If you have a main window (even an invisible one) that can process messages, you can handle the WM_ENDSESSION message.
See: http://msdn.microsoft.com/en-us/library/aa376889(v=VS.85).aspx
If you can make your app into a Windows service (or have your app communicate state with one that you provide) you can perform required actions on receipt of SERVICE_CONTROL_SHUTDOWN in your service control handler function. This would decouple your app that handles user interaction from the shutdown handling, which requires something to be running all the time (what if the user logs off?).
explorer.exe is the GUI process of windows which usually only gets shut down if Windows shuts down (exceptions have to be made for certain error conditions). You could listen on the WM_DESTROY window message for the process ID of explorer.exe and dismount then.
The way I can think of is to:
Register your program to auto Start up (when PC starts). Here's a tutorial on howto.
Store the user option (as mentioned above) in a repository or registry (if you know how). When your app would have started, you can read your registry and act accordingly.
For shutdown, your application will have to hook itself on a SystemEvent to detect shutdown (then you can act accordingly). Here's an example on howto (C#). For C++, you can listen to WM_ENDSESSION message.
I hope that my 2 cents can help you.