Improved version: When both WPARAM and LPARAM are set to 1, Messenger fires
the DMessengerEvents::OnAppShutdown event to applications and it doesnt show
the "There are applications using features" dialog.
// Returns TRUE if Messengers shutdown successfull (or were not running).
// Returns FALSE if Messengers didnt shutdown within the timeout period.
// Suggested Timeout=5000 (ms).
BOOL ShutdownMessenger(ULONG Timeout)
{
ULONG t = GetTickCount();
for (;
{
BOOL anyrunning = FALSE;
HWND hwnd;
UINT msgid;
// MSN Messenger.
hwnd = FindWindow("MSNHiddenWindowClass", NULL);
if (hwnd)
{
msgid = RegisterWindowMessage("TryMsnMsgrShutdown");
PostMessage(hwnd, msgid, 1, 1);
anyrunning = TRUE;
}
// Windows Messenger.
hwnd = FindWindow("HiddenWindowClass", NULL);
if (hwnd)
{
msgid = RegisterWindowMessage("TryWindowsMsgrShutdown");
PostMessage(hwnd, msgid, 1, 1);
anyrunning = TRUE;
}
if (!anyrunning)
{
// Success.
return TRUE;
}
// Timeout check.
if (GetTickCount()-t > Timeout)
{
break;
}
Sleep(100);
}
return FALSE;
}