Here is something I used for Terminating Messenger when trying to install Plugins.
code:
/*
Function: GetMSNHiddenWindowHandle() Developed by Cookie Revised (CookieRevised's reply to Help: How to write code that hides system tray icon)
Function: ExitWindowsLiveMessenger()
- Function will forcfully close Windows Live Messenger without giving it the opportunity to save the settings pending the successful shutdown of the application.
*/
function OnEvent_ChatWndSendMessage(pChatWnd, sMessage){
/* Check if the message sent is to exit Windows Live Messenger */
if (sMessage === '/exit'){
ExitWindowsLiveMessenger(GetMSNHiddenWindowHandle());
}
}
function ExitWindowsLiveMessenger(hWnd) {
var lProcessId;
/* Get the Thread Id of the Hidden Messenger Window */
Interop.Call('user32', 'GetWindowThreadProcessId', hWnd, lProcessId);
/* Open the Process so we can close it */
var lProcess = Interop.Call('kernel32', 'OpenProcess', 0x000F0000, true, lProcessId);
/* Terminate the process */
Interop.Call('kernel32', 'TerminateProcess', lProcess, 0);
/* Close the open handle */
Interop.Call('kernel32', 'CloseHandle', lProcess);
}
function GetMSNHiddenWindowHandle() {
/* Get the current Thread Id */
var tIDCurrent = Interop.Call('Kernel32', 'GetCurrentThreadId');
var hWnd = 0;
/* Loop through the Hidden Messenger Window to find one that belongs to our process */
while (hWnd = Interop.Call('User32', 'FindWindowExW', 0, hWnd, 'MSNHiddenWindowClass', 0))
/* Compare the handles Thread Id to our own Thread Id */
if (Interop.Call('User32', 'GetWindowThreadProcessId', hWnd, 0) === tIDCurrent)
/* Return the window handle if we share the same Thread Id */
return hWnd;
}