What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » Plug-Ins » [VB6] Closing MSN messenger

3 votes - 2.67 average   [VB6] Closing MSN messenger
Author: Message:
CookieRevised
Elite Member
*****

Avatar

Posts: 15517
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: [VB6] Closing MSN messenger
Shutting Down MSN Messenger
Posted by daniel on 2003-09-16 05:57:20

At some point in most MSN Messenger applications, you may want to shut down Messenger, or restart it. This could be to apply a patch, or to make Messenger run its shut down routine, like updating the list cache, or removing the systray icon. MSN Messenger has a cross application window message method to make it exit, this is commonly used by its installer. When you install an update the current version is closed so it can overwrite the files.

An obvious method of shutting Messenger down would be to just use TerminateProcess, this method is very forceful and Messenger won't perform its shutdown routine. Before you perform this kind of termination, its best to try the cross application windows message method. This involves sending a custom window message to the Messenger system window "MSNHiddenWindowClass", which is just an always open window for things like receiving events for the systray icon.

You may have before seen a message pop up telling you that you need to close applications that are using MSN Messenger, like Hotmail in Internet Explorer, Outlook Express. The window message method cannot ignore this, therefore you WILL need to forcefully terminate Messenger. You should kill all instances of the Messenger typelib to prevent the message beforehand, otherwise you can get the user to close those applications.

First, lets look at the window message method. You need to register the message "TryMsnMsgrShutdown", and send it to the window with the class name "MSNHiddenWindowClass".
code:
Private Sub GracefullShutdown(bShowCloseMsg As Boolean)
Dim hWnd As Long, lMsg As Long

    hWnd = FindWindow("MSNHiddenWindowClass", vbNullString)
    lMsg = RegisterWindowMessage("TryMsnMsgrShutdown")
    Call SendMessage(hWnd, lMsg, CLng(bShowCloseMsg) + 1, 0)

End Sub
First it gets the handle to the hidden window, then it gets the ID for the custom window message, and last of all it sends the message to the hidden window. If wParam is zero then, if applicable, the message saying to close all applications using Messenger will be shown, if wParam is one then the message will not be shown. Unfortunatly, I dont see a way of it letting us know if the shutdown was successfull, so you then should forcefully shut it down. Its best to wait a second or two to let it do what it needs to do - if it shutdown successfully.

Here is how to forcefully shut it down, by terminating the Messenger process.
code:
Private Sub ForcefullShutdown()
Dim hWnd As Long, lProcessID As Long, lProcess As Long

    hWnd = FindWindow("MSNHiddenWindowClass", vbNullString)
    Call GetWindowThreadProcessId(hWnd, lProcessID)
    lProcess = OpenProcess(0, False, lProcessID)
    Call TerminateProcess(lProcess, 0)
    Call CloseHandle(lProcess)

End Sub
Again, it finds the hidden window, because it is always open. It then puts the process ID for that window into a var, creates a process handle to use to be terminated, terminates the process. And closes the process handle, freeing up resources.

Here are the API declarations for those used.
code:
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Declare Function RegisterWindowMessage Lib "user32" Alias "RegisterWindowMessageA" (ByVal lpString As String) As Long

Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long

Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long

Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long

Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
.-= A 'frrrrrrrituurrr' for Wacky =-.
07-02-2004 02:42 PM
Profile PM Find Quote Report
« Next Oldest Return to Top Next Newest »

Messages In This Thread
[VB6] Closing MSN messenger - by J-Thread on 07-02-2004 at 12:45 PM
RE: [VB6] Closing MSN messenger - by Wouter on 07-02-2004 at 12:59 PM
RE: [VB6] Closing MSN messenger - by Dempsey on 07-02-2004 at 01:11 PM
RE: RE: [VB6] Closing MSN messenger - by J-Thread on 07-02-2004 at 03:15 PM
RE: [VB6] Closing MSN messenger - by Choli on 07-02-2004 at 01:32 PM
RE: [VB6] Closing MSN messenger - by J-Thread on 07-02-2004 at 01:44 PM
RE: [VB6] Closing MSN messenger - by CookieRevised on 07-02-2004 at 01:56 PM
RE: RE: [VB6] Closing MSN messenger - by J-Thread on 07-02-2004 at 02:28 PM
RE: [VB6] Closing MSN messenger - by CookieRevised on 07-02-2004 at 02:31 PM
RE: [VB6] Closing MSN messenger - by J-Thread on 07-02-2004 at 02:33 PM
RE: [VB6] Closing MSN messenger - by CookieRevised on 07-02-2004 at 02:42 PM
RE: [VB6] Closing MSN messenger - by J-Thread on 07-02-2004 at 02:45 PM
RE: [VB6] Closing MSN messenger - by matty on 07-02-2004 at 03:04 PM
RE: [VB6] Closing MSN messenger - by matty on 07-02-2004 at 04:56 PM
RE: [VB6] Closing MSN messenger - by J-Thread on 07-02-2004 at 05:29 PM
RE: [VB6] Closing MSN messenger - by Choli on 07-02-2004 at 05:33 PM
RE: [VB6] Closing MSN messenger - by matty on 07-02-2004 at 05:34 PM
RE: [VB6] Closing MSN messenger - by J-Thread on 07-03-2004 at 08:29 AM
RE: [VB6] Closing MSN messenger - by Choli on 07-03-2004 at 09:21 AM
RE: RE: [VB6] Closing MSN messenger - by J-Thread on 07-03-2004 at 10:49 AM
RE: [VB6] Closing MSN messenger - by Choli on 07-03-2004 at 12:12 PM
RE: [VB6] Closing MSN messenger - by J-Thread on 07-03-2004 at 12:57 PM
RE: [VB6] Closing MSN messenger - by J-Thread on 07-05-2004 at 05:46 PM


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On