What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » [Release] Exit WLM

[Release] Exit WLM
Author: Message:
CookieRevised
Elite Member
*****

Avatar

Posts: 15517
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: [Release] Exit Messenger - script attached
quote:
Originally posted by SpunkyLoveMuff
If no-one finds it useful, new scripters can still learn things from it...
  1. Don't use split(" ") like that to split up a command from its parameters. Split(" ") does not take in account multiple spaces after eachother; they all will be split out in seperate array elements.
    This makes that your parameter isn't always in the second array element. I know extremely many scripts use that method, but it is really the worsed way of all...

    It also doesn't take in account other spacing characters than ascii 32 (spacebar), which can occur on the command line. To overcome all this use regular expressions.

    If you aren't up to using regular expressions you can still use a combination of substr() and IndexOf().

    Or easier, use the split() method but with the optional limit parameter:
    stringObj.split(" ", 2). This will limit your array to 2 elements, and the second element will always be the parameter.


    But also note that with any method you use (except for the regular expression method) you need to take in account additional leading and trailing spaces. Thus you also need to trim the parameter element before using it.

    (and take in account the casing of letters too)

  2. I see you used Messenger.MyUserID to avoid that a new user is sign out. This is often forgotten when timers are used, so (y)(y)... However, in this case you don't need to check upon Messenger.MyUserID if you simply cancel the timers when the user signs out.

  3. Don't use the word exit in the command. You're not exiting messenger, but signing out. This doesn't matter for private use, but if you're going to release something in public such things are to be considered.

  4. Nice idea about the "5 seconds remaining" toast.

    However, when toasts are involved with timers like this it is very tricky. Toasts don't always immediatly show up. eg: if a messenger toast is shown, no plus! toasts will be shown and they will be held back until all messenger toasts are gone. Also, there is no way to check how long a toast is going to be visible unfortunatly (hence why you probably used 5 seconds, as a toast displays longer -good thinking-).

    Maybe these are two ideas for Patchou to implement:
    - a callback for when the toast is displayed
    - a method to know how long the toast is displayed (and/or another callback for when the toast is removed)

Anyways, I've taken the idea of your script and put it in the attached script.
If you want to find the fixes for points 1 and 2 yourself, don't look at the source of the script. Otherwise, feel free to peek. ;)

For people who want to learn scripting there is one big golden rule: read the documentation! In fact, one should lookup each and every single jscript command he is using. A nice example is this split() issue; I haven't seen one script using it to split up a command line using the optional limit parameter. Do never assume that the things you see in scripts are the proper ways, even if it is used by everybody, and even if it seems ok to you. Look things up, starting with the most smallest and obvious commands, especially if you're a beginning scripter...

JScript 5.6 Scripting Documentation
Plus! Live Scripting Documentation

;)


------------------------------------------------------------------------------------------


quote:
Originally posted by Mattike
code:
function ExitWindowsLiveMessenger() {
         /* Exit the process */
function ExitWindowsLiveMessenger() {
         /* Exit the process */
Not difficult if you repeat things... :P
Though, this will not crash messenger. It can't because that is simply a syntax error, hence the script wouldn't even start but simply give a friendly syntax error message that "} is expected on line x"...

quote:
Originally posted by Mattike
Spunky, try this one:
code:
function OnEvent_ChatWndSendMessage(pChatWnd, sMessage){
         /* Check if the message sent is to exit Windows Live Messenger */
        if (sMessage === '/exit'){
                ExitWindowsLiveMessenger();
                return ""; //Not that should do anything... ^o)
        }
}

function ExitWindowsLiveMessenger() {
         /* Exit the process */
         Interop.Call('kernel32', 'ExitProcess', Interop.Call('kernel32', 'GetExitCodeProcess', Interop.Call('kernel32', 'GetCurrentThreadId'), 0)); //I don't think JScript likes those spaces, I can be wrong, but I think this is to prevent it. :P
}

Have you tried that before you posted it? Because the spaces have got nothing todo with this. In fact, if you look around a bit in other scripts, spaces are constantly used to make scripts more readable...

Try not to post such things (and especially not fixes) if you don't know what you're talking about though. That's harsh I know, but still... it makes things more confusing for people and makes things much harder to fix and explain.


------------------------------------------------------------------------------------------


Nevertheless, Matty indeed screwed up too (must have been drunk :p). The first (and only) parameter of the ExitProcess API is an exit value you assign yourself. Not an exit value which is gotten from a process which isn't yet closed. And you can certainly not write a value (that exit value which doesn't exist, thus 0) to the memory offset of a pointer (the deepest nested API he used), which is what he did with the GetExitCodeProcess API... Hence crashes...

If ExitProcess is being used, it should be like this:
    Interop.Call('kernel32', 'ExitProcess', whatever value you like)

eg:
    Interop.Call('kernel32', 'ExitProcess', 1234)

Whatever application which monitoring the process you just ended, can now check by the exit value (use GetExitCodeProcess API) if the process was ended by you. In that case the process' exit value will be 1234.


------------------------------------------------------------------------------------------


Anyways, using the ExitProcess API isn't going to work to end Messenger (Messenger is a special case, just like mothers in law :p)...

But, there is a build in method (which I forgot about it too when writing my previous post) which you can use the close Messenger in a proper way. Thus without forcing anything, and thus without possible crashes, data loss, etc...

It comes down to sending the registered windows message "TryMsnMsgrShutdown" to the hidden messenger window, that's all it takes.

Note 1: If you have any conversation windows open you will get a "all conversation windows will be closed" message first. To overcome this you can close all conversation windows first (do this also gracefully). Or set the Plus! registry value DisableConfirmSignout (see registry help).

Note 2: If any applications are using messenger, you still will get a "first close all applications using Messenger" message. But this can be suppressed by setting a parameter in the API call.

code:
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//    This function will attempt to gracefully close Messenger (polygamy safe)
//    parameters:
//        bSuppressCloseMsg (optional // default: false)
//        If this is true, it will not show the message "first close all applications using Messenger".
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function ExitWindowsLiveMessenger(bSuppressCloseMsg) {
    if (typeof(bShowCloseMsg) === 'undefined') bSuppressCloseMsg = false;
    if (CloseAllConversations() === 0) {
        var hWnd = GetMSNHiddenWindowHandle();
        var lMsg = Interop.Call('User32', 'RegisterWindowMessageW', 'TryMsnMsgrShutdown');
        Interop.Call('User32', 'SendMessageW', hWnd, lMsg, bSuppressCloseMsg?1:0, 0);
    }
    /*
    If you come here, the gracefull closing of Messenger has failed.
    Either because the user has cancelled the closing of some conversations,
    or either because another application is still using Messenger.
    */
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//    This function will attempt to gracefully close all conversation windows (polygamy safe)
//    Return value: 0 upon success; otherwise the returned value is the number of still open conversations
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function CloseAllConversations() {
    // Loop thru all the open conversation windows
    for (var e = new Enumerator(Messenger.CurrentChats); !e.atEnd(); e.moveNext())
        // Gracefully close the conversation window
        Interop.Call('User32', 'PostMessageW', e.item().Handle, 0x0111, 40017, 0);

    // Return the number of still open conversation windows
    return Messenger.CurrentChats.Count
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//    This function will find our own hidden Messenger window (polygamy safe)
//    Return value: handle to our own hidden Messenger window
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function GetMSNHiddenWindowHandle() {
    // Get our own thread id
    var tIDCurrent = Interop.Call('Kernel32', 'GetCurrentThreadId');
    var hWnd = 0;
    // Loop through the hidden Messenger windows to find the one that belongs to our process
    while (hWnd = Interop.Call('User32', 'FindWindowExW', 0, hWnd, 'MSNHiddenWindowClass', 0))
        // Compare the thread id of the window with our own thread id
        if (Interop.Call('User32', 'GetWindowThreadProcessId', hWnd, 0) === tIDCurrent)
            // Return the window handle if the thread id is the same
            return hWnd;
}




The full script which adds the commands:

/quit
and /exit                   to exit Messenger
/closeall                             to close all current conversation windows
/signout <optional delay>   to signout after an optional delay in seconds (between 10 and 86400 seconds)
/stopsignout                     to stop the delayed signout countdown


http://www.msgpluslive.net/scripts/view/241-Exit-Messenger/

Previous downloads from forum: 20


This post was edited on 03-25-2011 at 02:06 AM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
11-15-2006 12:14 AM
Profile PM Find Quote Report
« Next Oldest Return to Top Next Newest »

Messages In This Thread
[Release] Exit WLM - by Spunky on 11-13-2006 at 03:07 AM
RE: [Release] Exit WLM - by NanaFreak on 11-13-2006 at 03:12 AM
RE: [Release] Exit WLM - by MicroWay on 11-13-2006 at 03:16 AM
RE: [Release] Exit WLM - by markee on 11-13-2006 at 03:19 AM
RE: [Release] Exit WLM - by Spunky on 11-13-2006 at 03:21 AM
RE: [Release] Exit WLM - by matty on 11-13-2006 at 03:51 AM
RE: [Release] Exit WLM - by markee on 11-13-2006 at 03:58 AM
RE: [Release] Exit WLM - by CookieRevised on 11-13-2006 at 04:06 AM
RE: [Release] Exit WLM - by matty on 11-13-2006 at 05:22 AM
RE: [Release] Exit WLM - by Spunky on 11-13-2006 at 07:48 PM
RE: [Release] Exit WLM - by Matti on 11-13-2006 at 08:16 PM
RE: [Release] Exit WLM - by Spunky on 11-13-2006 at 08:23 PM
RE: [Release] Exit Messenger - script attached - by CookieRevised on 11-15-2006 at 12:14 AM
RE: RE: [Release] Exit Messenger - script attached - by Jesus on 12-18-2006 at 01:37 PM
RE: RE: RE: [Release] Exit Messenger - script attached - by markee on 12-18-2006 at 02:06 PM
RE: RE: RE: [Release] Exit Messenger - script attached - by CookieRevised on 12-19-2006 at 12:27 AM
RE: RE: RE: RE: [Release] Exit Messenger - script attached - by markee on 12-19-2006 at 02:02 AM
RE: [Release] Exit WLM - by Spunky on 11-15-2006 at 12:29 AM
RE: [Release] Exit WLM - by CookieRevised on 11-15-2006 at 01:17 AM
RE: [Release] Exit WLM - by Aristide on 12-17-2006 at 01:56 PM
Re; [Release] Exit WLM - by BraydenP on 12-18-2006 at 12:37 PM
RE: [Release] Exit WLM - by CookieRevised on 12-19-2006 at 02:06 AM
RE: [Release] Exit WLM - by Spunky on 12-19-2006 at 02:08 AM
RE: [Release] Exit WLM - by warmth on 12-22-2006 at 11:38 PM
RE: [Release] Exit WLM - by Spunky on 12-23-2006 at 01:27 AM
RE: [Release] Exit WLM - by warmth on 12-23-2006 at 08:33 AM


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