Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C#
Article

Control MSN Messenger when the workstation is locked

Rate me:
Please Sign up or sign in to vote.
3.75/5 (6 votes)
24 Jul 20061 min read 47K   282   18   10
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 :)

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 :)

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:

C#
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


Written By
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralThanks Pin
Sai Gaurav5-Oct-07 2:17
Sai Gaurav5-Oct-07 2:17 
Generalsmall fix Pin
Oleksandr Kucherenko5-Apr-07 23:45
Oleksandr Kucherenko5-Apr-07 23:45 
GeneralRe: small fix Pin
ammar_shaker3-May-07 21:20
ammar_shaker3-May-07 21:20 
GeneralRe: small fix Pin
Oleksandr Kucherenko4-May-07 3:12
Oleksandr Kucherenko4-May-07 3:12 
GeneralOffice Communicator API Status Change Problem Pin
Black Dhalia9-Nov-06 2:53
Black Dhalia9-Nov-06 2:53 
QuestionMessengerAPI Pin
Callixte.1-Aug-06 0:07
Callixte.1-Aug-06 0:07 
AnswerRe: MessengerAPI Pin
ayalgelles1-Aug-06 2:12
ayalgelles1-Aug-06 2:12 
GeneralNice tool but... Pin
Ed.Poore31-Jul-06 21:59
Ed.Poore31-Jul-06 21:59 
What happens when you're using Win2K because you dislike XP and hence can't get the latest version of messenger? Roll eyes | :rolleyes: Have to have the status set to away then Poke tongue | ;-P


Formula 1 - Short for "F1 Racing" - named after the standard "help" key in Windows, it's a sport where participants desperately search through software help files trying to find actual documentation. It's tedious and somewhat cruel, most matches ending in a draw as no participant is able to find anything helpful. - Shog9

Ed

GeneralRe: Nice tool but... Pin
ayalgelles1-Aug-06 2:16
ayalgelles1-Aug-06 2:16 
GeneralRe: Nice tool but... Pin
Ed.Poore1-Aug-06 2:32
Ed.Poore1-Aug-06 2:32 

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

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