How to Check if Windows is Locked in Qt/C++

Here is how you can check and see if Windows is locked in Qt/C++. This same function will also let you know if the user is switched to another. Note that this involves using some Win32 API functions.

Note that you need to add the following include header to your code:

#include "Windows.h"

And also the following library to your .PRO file:

LIBS += -lUser32


Here is the function that will check for user locked state:

bool isDesktopAvailable()
{
	HDESK desktop = OpenDesktop(TEXT("Default"), 0, false, DESKTOP_SWITCHDESKTOP);
	if (desktop)
	{
		if (SwitchDesktop(desktop))
		{
			CloseDesktop(desktop);
			return true;
		}
		else
		{
			CloseDesktop(desktop);
		}
	}

	return false;
}

Or use the following snippet from my Bitbucket:

https://bitbucket.org/snippets/amahta/nMAXE/isdesktopavailable



Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.