Shutdown Windows Messenger programatically?

  • Thread starter Thread starter Leo Havmøller
  • Start date Start date
How do I request Windows Messenger shutdown from C/C++?
Also, what is the correct way to launch it again?

Leo Havmøller.

I think you have to read something about c/c++ process (exec funtions
family) and thread (p_thrread.h header file under linux) management.
However you could try using c routine system("<name of command") for
calling the application but it is not very 'powerfull'; se also dos.h
header, it include many function corresponding to system calls such as cd ,
copy and so on.

Hi
 
Leo Havmøller said:
How do I request Windows Messenger shutdown from C/C++?

Found the solution myself. The following will gracefully shutdown both MSN
Messenger and Windows Messenger:

void ShutdownMessenger(void)
{
HWND hwnd;
UINT msgid;

// MSN Messenger.
hwnd = FindWindow("MSNHiddenWindowClass", NULL);
if (hwnd)
{
msgid = RegisterWindowMessage("TryMsnMsgrShutdown");
PostMessage(hwnd, msgid, 1, 0);
}

// Windows Messenger.
hwnd = FindWindow("HiddenWindowClass", NULL);
if (hwnd)
{
msgid = RegisterWindowMessage("TryWindowsMsgrShutdown");
PostMessage(hwnd, msgid, 1, 0);
}
}

Leo Havmøller.
 
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;
}
 
Back
Top