Kevin Pastor
Kevin Pastor

Reputation: 801

How to pause execution until the user logs in for startup app on Win32

I am making an app in C++ using Win32 that needs to pause its execution when the session is locked. I've been using WM_WTSSESSION_CHANGE to detect WTS_SESSION_LOCK and WTS_SESSION_UNLOCK and update my app state which works great. The app needs to start on startup, so I've placed a shortcut of the executable here: C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp.

The problem I have is that the app starts its execution before the user logged in since WTS_SESSION_LOCK or WTS_SESSION_UNLOCK have not yet been sent. I haven't found anything useful regarding this problem. Any ideas on how I could check that the program is running while the user has not logged in?

Upvotes: 1

Views: 273

Answers (1)

Rita Han
Rita Han

Reputation: 9710

The problem I have is that the app starts its execution before the user logged in since WTS_SESSION_LOCK or WTS_SESSION_UNLOCK have not yet been sent.

When you restart (or turn off then turn on) Windows, startup apps run after the user logs on. So you will not receive WTS_SESSION_UNLOCK. It is expected because the user has already logged on before your app runs.

To solve this issue, you can query the session information via WTSQuerySessionInformation to check if the current session is active or not when your app starts. Active indicate the user is logged on, so you can start execution of your app.

WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSSessionInfo, &sInfo, &BytesReturned);

After the Windows started up, you will receive WTS_SESSION_LOCK and WTS_SESSION_UNLOCK normally.

Upvotes: 1

Related Questions