July 5th, 2018

How can I detect whether the user is logging off?

There may be cases where you want to know whether the user is logging off. For example, your program might be responding to external events by (say) launching a helper program, but you don’t want to do that if a logoff is in progress.

The way to find out is hidden in the Get­System­Metrics function, which is admittedly a rather non-obvious place to put it.

if (GetSystemMetrics(SM_SHUTTINGDOWN)) {
 // user is logging off, don't start anything new
}

Compounding the confusion is that the flag is called “shutting down”, but in fact it reports whether the user is logging off (which in today’s parlance is called signing out). The logout may be part of a shutdown, or it might not.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.