Jump to content
JohnLM

Where to put an app in Windows startup and shutdown and sleep mode?

Recommended Posts

specs:  Win7 and Win10

 

In all my casual searches, I've never seen any kind of request for this. 

 

I have an app that writes the date/time down.  Now, I would like to utilize it every time Windows startup or shutdown, but also when I put Windows in sleep mode and wake up from sleep, and also for Windows logon/logoff mode. 

 

I will mostly be using it during Windows sleep modes, but I need it for shutdown as well. 

 

Where do I put my app to make this happen? 

Share this post


Link to post

Unclear what you are asking. You can't do anything "during" sleep modes as the system is asleep.

 

If you just want dates and times for those events read the Windows Event Log where the Kernel-Power source logs events for all of those. 

Edited by Brian Evans

Share this post


Link to post
13 hours ago, JohnLM said:

every time Windows startup or shutdown, but also when I put Windows in sleep mode and wake up from sleep, and also for Windows logon/logoff mode.

Maybe you should write a program to filter Windows event log files which contains all the events you mentioned.

Or maybe there is something you don't tell us...

Share this post


Link to post
Posted (edited)

You can put your program in AUTOSTART folder, and every time the program start filter the Windows events log (like others told). Sometimes (for example every day changes) filter again it.

Take care that if you want to start the program for every accounts you should write some records in the Windows registry.

Alternatives is that you capture the message events (like QueryEndSession and similar) to capture in realtime whats happens

You must take care of user multisessions and also terminal server (or RDS).

 

Bye

Edited by DelphiUdIT

Share this post


Link to post

Putting your program into the autostart folder will ensure that it is started when windows starts (or actually, when the user logs on). You can then let it run with an invisible window and handle messages that announce the other events:

  • Like 1

Share this post


Link to post
Posted (edited)
On 12/31/2023 at 11:52 AM, JohnLM said:

I have an app that writes the date/time down.  Now, I would like to utilize it every time Windows startup or shutdown, but also when I put Windows in sleep mode and wake up from sleep, and also for Windows logon/logoff mode. 

 

Where do I put my app to make this happen? 

There is nowhere you can "put the app" that will make the OS run your app during sleeps/wakeups.  Your app needs to already be running in order to catch those events in real-time in your app's code.

 

As for Startup specifically, you could use the "Startup" group in the Start Menu, or the "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" key in the Registry, to run your app during startup/logon.

 

There are also startup/shutdown and logon/logoff policy scripts that you can edit to run your app:

 

Working with startup, shutdown, logon, and logoff scripts using the Local Group Policy Editor

Edited by Remy Lebeau

Share this post


Link to post
Posted (edited)

Remy, unfortunately, Windows 7 Home Premium does not come with any group policy editors, so that is out of the question.  The gpedit.msc app does not exist on this laptop. 

 

Edited by JohnLM

Share this post


Link to post
1 hour ago, Remy Lebeau said:

There is nowhere you can "put the app" that will make the OS run your app during sleeps/wakeups.

You could execute the app with a Windows Task Scheduler event triggered by the different conditions. The tricky part is to find the appropriate event ids, where The Kernel-Power Event Provider can be helpful. It also is possible that the app doesn't get the time to execute its task before the system goes to sleep and finishes it after waking up.

Share this post


Link to post

I would personally write such a program as a service application and let it monitor messages like WM_POWERBROADCAST to log the events. This way you can be sure that the application is always running and will capture the events when they happen. 

  • Like 1

Share this post


Link to post

I was also thinking about a service, but it might be more trouble (rights wise) to get one installed than a simple standalone app.

On 1/2/2024 at 12:03 AM, Uwe Raabe said:

app doesn't get the time to execute its task before the system goes to sleep

Isn't there messages to respond to that can prevent/postpone the sleep?

Share this post


Link to post
3 minutes ago, Lars Fosdal said:

Isn't there messages to respond to that can prevent/postpone the sleep?

AFAIK, that is only sent to applications already running. I have not tested, but when the scheduler executes the task the sleep probably cannot be avoided anymore. It should be doable to check this, but I refused to invest that time.

Share this post


Link to post

Another thought - would it not be just as convenient to search the Windows Eventlog for the various events for start/logon/logoff/sleep/reboot/shutdown?

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×