Click here to Skip to main content
Click here to Skip to main content
Go to top

Control MSN Messenger when the workstation is locked

, 24 Jul 2006
Rate this:
Please Sign up or sign in to vote.
Identify when the workstation is locked/unlocked, and set the MSN Messenger status to offline/online, respectively.

Introduction

Came home one day, opened MSN Messenger, all my friends were online, but "Away"..

Some 30 minutes later, a friend of mine sent me a message. He was already online for 20 minutes, yet I had no way of knowing this. Then, it struck me - "Away" is evil.

A friend of mine commented that my tool sucks because while offline (in my case, when the workstation is locked) you cannot receive messages (which you can do when "Away"). The new version of the MSN Messenger now enables receiving messages when offline, so I consider this problem solved, and my friend - a very jealous guy Smile | :)

This article combines knowledge from different CodeProject articles to create a very useful tool that satisfies a very personal need, however I hope you can relate Smile | :)

Code

The code uses a simple Windows form to intercept window messages (you may replace this by any Windows service, if needed). On loading, the form is set to not be visible and not show in the taskbar.

"Session Change" messages arrive to the form when using the WTSRegisterSessionNotification method and is identified by constants.

Using the Messenger API, the status is set to invisible (appear offline) or online considering the session state.

Enjoy:

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using MessengerAPI;

public class MSNLockForm : Form
{
    private const int NotifyForThisSession = 0;
    private const int SessionChangeMessage = 0x02B1;
    private const int SessionLockParam = 0x7;
    private const int SessionUnlockParam = 0x8;

    [DllImport("wtsapi32.dll")]
    private static extern bool 
            WTSRegisterSessionNotification(IntPtr hWnd, int dwFlags);

    private void InitializeComponent()
    {
        this.Name = "LockNotificationForm";
        this.Load += new 
             System.EventHandler(this.LockNotificationForm_Load);
    }

    [DllImport("wtsapi32.dll")]
    private static extern bool 
            WTSUnRegisterSessionNotification(IntPtr hWnd);

    private bool registered = false;

    public static void Main(string[] args)
    {
        Application.Run(new MSNLockForm());
    }

    protected override void Dispose(bool disposing)
    {
        if(registered)
        {
            WTSUnRegisterSessionNotification(Handle);
            registered = false;
        }

        base.Dispose(disposing);
        return;
    }

    Messenger messenger;

    protected override void OnHandleCreated(EventArgs e)
    {
        base.OnHandleCreated(e);
        bool haveXp = Environment.OSVersion.Platform == 
             PlatformID.Win32NT && 
             (Environment.OSVersion.Version.Major > 5 || 
             (Environment.OSVersion.Version.Major == 5 &&
             Environment.OSVersion.Version.Minor >= 1));
        if(haveXp)
            registered = WTSRegisterSessionNotification(Handle, 
                                              NotifyForThisSession);
        messenger = new MessengerClass();
        IMessengerServices services = 
           (IMessengerServices) messenger.Services;
        IMessengerService service = 
           (IMessengerService) services.PrimaryService;

        return;
    }
    protected virtual void OnSessionLock()
    {
        try
        {
            messenger.MyStatus = MISTATUS.MISTATUS_INVISIBLE;
        }
        catch
        {
        }
    }
    protected virtual void OnSessionUnlock()
    {
        try
        {
            messenger.MyStatus = MISTATUS.MISTATUS_ONLINE;
        }
        catch
        {
        }
    }

    protected override void WndProc(ref Message m)
    {
        if(m.Msg == SessionChangeMessage)
        {
            if(m.WParam.ToInt32() == SessionLockParam)
                OnSessionLock();
            else if(m.WParam.ToInt32() == SessionUnlockParam)
                OnSessionUnlock();
        }
        else
            this.Visible = false;
        base.WndProc(ref m);
        return;
    }

    private void LockNotificationForm_Load(object sender, 
                                      System.EventArgs e)
    {
        this.ShowInTaskbar = false;
    }
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

ayalgelles

Israel Israel
No Biography provided

Comments and Discussions

 
GeneralThanks PinmemberSai Gaurav5-Oct-07 2:17 
Generalsmall fix PinmemberAlex Kucherenko5-Apr-07 23:45 
GeneralRe: small fix Pinmemberammar_shaker3-May-07 21:20 
GeneralRe: small fix PinmemberAlex Kucherenko4-May-07 3:12 
GeneralOffice Communicator API Status Change Problem PinmemberBlack Dhalia9-Nov-06 2:53 
QuestionMessengerAPI PinmemberCallixte.1-Aug-06 0:07 
AnswerRe: MessengerAPI Pinmemberayalgelles1-Aug-06 2:12 
GeneralNice tool but... PinmemberEd.Poore31-Jul-06 21:59 
GeneralRe: Nice tool but... Pinmemberayalgelles1-Aug-06 2:16 
GeneralRe: Nice tool but... PinmemberEd.Poore1-Aug-06 2:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 24 Jul 2006
Article Copyright 2006 by ayalgelles
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid