ScreensaverNotifier::ScreensaverNotifier() :
alreadyNotified_(false),
+ isRegistered_(false),
hookDll_(nullptr),
hWnd_(nullptr),
hNotifyWnd_(nullptr),
bool ScreensaverNotifier::InitWindow()
{
- hWnd_ = CreateWindowEx(WS_EX_TOOLWINDOW,
- SCRSVER_NOTIFY_WND_CLASS,
- nullptr,
- WS_POPUP,
- 0, 0, 0, 0,
- HWND_MESSAGE,
- 0,
- hglobInstance,
- this);
+ hWnd_ = CreateWindowEx(
+ WS_EX_TOOLWINDOW,
+ SCRSVER_NOTIFY_WND_CLASS,
+ nullptr,
+ WS_POPUP,
+ 0, 0, 0, 0,
+ HWND_MESSAGE,
+ 0,
+ hglobInstance,
+ this);
if (hWnd_ == nullptr) {
PrintWindowsError(_T("CreateWindow"));
return false;
void ScreensaverNotifier::Unload()
{
if (hookDll_) {
+ if (isRegistered_ && unregHook_) {
+ unregHook_();
+ }
FreeLibrary(hookDll_);
regHook_ = nullptr;
unregHook_ = nullptr;
+ hookDll_ = nullptr;
}
}
{
hNotifyWnd_ = hNotifyWnd;
if (regHook_ != nullptr) {
- return regHook_(hWnd_, hookWndMsg_) != 0;
+ if (regHook_(hWnd_, hookWndMsg_)) {
+ isRegistered_ = true;
+ return true;
+ }
}
return false;
}
void ScreensaverNotifier::ClearNotifications()
{
hNotifyWnd_ = nullptr;
- if (unregHook_ != nullptr) {
+ if (isRegistered_ && unregHook_ != nullptr) {
unregHook_();
}
}
bool ScreensaverNotifier::IsScreensaverRunning()
{
BOOL isRunning = FALSE;
- if (SystemParametersInfo(SPI_GETSCREENSAVERRUNNING,
- 0,
- &isRunning,
- FALSE) == FALSE) {
+ if (SystemParametersInfo(
+ SPI_GETSCREENSAVERRUNNING,
+ 0,
+ &isRunning,
+ FALSE) == FALSE) {
PrintWindowsError(_T("SystemParametersInfo"));
return false;
}
/* Window Proc */
/* ========================================================================== */
-LRESULT CALLBACK ScreensaverNotifier::WindowProc(HWND hWnd,
- UINT msg,
- WPARAM wParam,
- LPARAM lParam)
+LRESULT CALLBACK ScreensaverNotifier::WindowProc(
+ HWND hWnd,
+ UINT msg,
+ WPARAM wParam,
+ LPARAM lParam)
{
static UINT uTaskbarRestart = 0;
switch (msg) {