The source files for this module are listed below. You can also download the module C_DesktopWaiter as a zip archive; this archive contains all the source files and documentation.
CDesktopWaiter can make your code wait until the desktop that your application is running on is active again (e.g. the user logged back in from the Windows login screen).
This source code also uses our C_Timer and C_SignalSlot classes. You can also download these at our website.
Windows 2000 and higher has the concept of multiple desktops. Generally all applications run on one desktop, and the Windows logon prompt and screensavers also run in their own private desktops. Sometimes it is necessary to know what desktop the user is looking at; is the user busy in the logon desktop or the screensaver desktop, or is he active on the desktop our application runs on? The class CDesktopWaiter can wait for the desktop of the currently running application (actually thread) to become active.
You can consult with the method OurDesktopActive anytime to check if our desktop is active. In case you do need to wait for our desktop to become active and do not want to use a polling loop, you can call the StartWaiting method. This method will return immediately if our desktop is the active one, but starts a poll on a timer (500 msec.) if it isn't and then returns as well. Once our desktop becomes active again, the CDesktopWaiter object will raise the CUserOnOurDesktopEvent event to notify you our desktop is active.
Each file belonging to this source code module is listed below.
/*******************************************************************************
Version: 4
Author: Carl Colijn, TwoLogs
Contact: c.colijn@twologs.com
Source: https://www.twologs.com/sourcecode
This code is freely distributable, as long as this comment remains intact.
If you find this source useful, you may use this code in your own projects
free of charge, but some acknowledgement to the author of this code is always
appreciated :)
The source is however distributed 'as is' without waranty and/or support, and
may not be fit for each and every application. Use it at your own discretion
and at your own risk.
The source already has undergone testing. This doesn't mean however that all
bugs are removed from this piece of code. If you find one of them, please
contact me about it. I can however not guarantee when and if the bug will be
fixed.
More information about this module can be found in the accompanying HTML file.
*******************************************************************************/
#ifndef INCLUDE_TWOLOGS_COMMON_DESKTOPWAITER_H
#define INCLUDE_TWOLOGS_COMMON_DESKTOPWAITER_H
#include <windows.h>
#include "..\C_Timer\Timer.h"
// Notifies when the user is back on our desktop
struct CUserOnOurDesktopEvent: public CEventTypeNoResult {
};
// The logon waiter
class CDesktopWaiter:
public CSlot<CTimerWentOffEvent>,
public CSignal<CUserOnOurDesktopEvent> {
public:
// Con- & destructor
CDesktopWaiter();
virtual ~CDesktopWaiter();
// Starts waiting till the user is on our desktop, if not doing so already
// A timer will be attached to the given window for polling reasons
// When waiting is done, the CUserOnOurDesktopEvent will be raised
// Returns if we started waiting (if our desktop is not active yet)
bool StartWaiting(HWND hWindow);
// Returns if our own desktop is active
bool OurDesktopActive();
private:
// Whether we're waiting
bool m_bWaiting;
// Our polling timer
CTimer m_oTimer;
// Gets the name of the given desktop
std::wstring GetDesktopName(HDESK hDesktop);
// Events
bool OnEvent(CTimerWentOffEvent& oEvent);
};
#endif // INCLUDE_TWOLOGS_COMMON_DESKTOPWAITER_H
/*******************************************************************************
Version: 4
Author: Carl Colijn, TwoLogs
Contact: c.colijn@twologs.com
Source: https://www.twologs.com/sourcecode
This code is freely distributable, as long as this comment remains intact.
If you find this source useful, you may use this code in your own projects
free of charge, but some acknowledgement to the author of this code is always
appreciated :)
The source is however distributed 'as is' without waranty and/or support, and
may not be fit for each and every application. Use it at your own discretion
and at your own risk.
The source already has undergone testing. This doesn't mean however that all
bugs are removed from this piece of code. If you find one of them, please
contact me about it. I can however not guarantee when and if the bug will be
fixed.
More information about this module can be found in the accompanying HTML file.
*******************************************************************************/
#include "DesktopWaiter.h"
// Con- & destructor
CDesktopWaiter::CDesktopWaiter():
m_bWaiting(false) {
// Connect to our timer
CSlot<CTimerWentOffEvent>::Connect(&m_oTimer);
}
CDesktopWaiter::~CDesktopWaiter() {
// Kill any timer we're waiting on
m_oTimer.Stop(false);
}
// Starts waiting till the user is on our desktop, if not doing so already
// A timer will be attached to the given window for polling reasons
// When waiting is done, the CUserOnOurDesktopEvent will be raised
// Returns if we started waiting (if our desktop is not active yet)
bool CDesktopWaiter::StartWaiting(HWND hWindow) {
// Look if our desktop is active
bool bNeedToWait = !OurDesktopActive();
// And look if to start waiting
if (bNeedToWait && !m_bWaiting) {
// Yes -> init our timer on this window
m_oTimer.Initialize(hWindow);
// And start it
m_oTimer.Start(500);
m_bWaiting = true;
}
// And return if we're waiting
return bNeedToWait;
}
// Gets the name of the given desktop
std::wstring CDesktopWaiter::GetDesktopName(HDESK hDesktop) {
// Get it's name using a small buffer first
std::wstring sDesktopName;
sDesktopName.resize(256);
DWORD nNeededNameSize;
BOOL bInfoGotten = GetUserObjectInformation(
hDesktop,
UOI_NAME,
const_cast<wchar_t*>(sDesktopName.c_str()),
255,
&nNeededNameSize
);
if (bInfoGotten) {
// Done -> return the name
sDesktopName.at(nNeededNameSize) = L'\0';
sDesktopName.resize(nNeededNameSize);
} else {
// Couldn't -> get the full size name
sDesktopName.resize(nNeededNameSize + 1);
bInfoGotten = GetUserObjectInformation(
hDesktop,
UOI_NAME,
const_cast<wchar_t*>(sDesktopName.c_str()),
nNeededNameSize,
&nNeededNameSize
);
if (bInfoGotten) {
// Done -> make sure the name is correctly truncated
sDesktopName.at(nNeededNameSize) = L'\0';
sDesktopName.resize(nNeededNameSize);
}
}
// And return the found name
return sDesktopName;
}
// Returns if our own desktop is active
bool CDesktopWaiter::OurDesktopActive() {
// Get the active desktop
SetLastError(0);
HDESK hActiveDesktop = OpenInputDesktop(0, FALSE, DESKTOP_READOBJECTS);
bool bOurDesktopActive = true;
if (hActiveDesktop == NULL && GetLastError() == 0) {
// Couldn't, but no error -> must be the Winlogon desktop
bOurDesktopActive = false;
} else if (hActiveDesktop != NULL) {
// Done -> get our desktop
HDESK hOurDesktop = GetThreadDesktop(GetCurrentThreadId());
// Get the names of the desktops
std::wstring sOurDesktopName = GetDesktopName(hOurDesktop);
std::wstring sActiveDesktopName = GetDesktopName(hActiveDesktop);
// Look if our desktop is active
bOurDesktopActive =
0 == wcscmp(sOurDesktopName.c_str(), sActiveDesktopName.c_str());
// And close the active desktop, if needed
CloseDesktop(hActiveDesktop);
}
// And return if our desktop is active
return bOurDesktopActive;
}
// Events
bool CDesktopWaiter::OnEvent(CTimerWentOffEvent& oEvent) {
// Look if our desktop is active now
if (OurDesktopActive()) {
// Yes -> stop the timer
m_oTimer.Stop(false);
m_bWaiting = false;
// And notify everyone who needs to know
CUserOnOurDesktopEvent oEvent;
CSignal<CUserOnOurDesktopEvent>::RaiseEvent(oEvent);
}
return true;
}