Click here to Skip to main content
13,794,217 members
Click here to Skip to main content
Add your own
alternative version


18 bookmarked
Posted 24 Jul 2006

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.


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


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.


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;

    private static extern bool 
            WTSRegisterSessionNotification(IntPtr hWnd, int dwFlags);

    private void InitializeComponent()
        this.Name = "LockNotificationForm";
        this.Load += new 

    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)
            registered = false;


    Messenger messenger;

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

    protected virtual void OnSessionLock()
            messenger.MyStatus = MISTATUS.MISTATUS_INVISIBLE;
    protected virtual void OnSessionUnlock()
            messenger.MyStatus = MISTATUS.MISTATUS_ONLINE;

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

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


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


About the Author

Israel Israel
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralThanks Pin
Sai Gaurav5-Oct-07 3:17
memberSai Gaurav5-Oct-07 3:17 
Generalsmall fix Pin
Oleksandr Kucherenko6-Apr-07 0:45
memberOleksandr Kucherenko6-Apr-07 0:45 
GeneralRe: small fix Pin
ammar_shaker3-May-07 22:20
memberammar_shaker3-May-07 22:20 
GeneralRe: small fix Pin
Oleksandr Kucherenko4-May-07 4:12
memberOleksandr Kucherenko4-May-07 4:12 

please take Reflector and search in Windows.Forms code DestroyHandle calls. NET can destroy/recreate handles in several cases, like: Right-To-Left (RTL) applying, changes of window styles and etc.


Good Luck
Alex Kucherenko

GeneralOffice Communicator API Status Change Problem Pin
Black Dhalia9-Nov-06 3:53
memberBlack Dhalia9-Nov-06 3:53 
QuestionMessengerAPI Pin
Callixte.1-Aug-06 1:07
memberCallixte.1-Aug-06 1:07 
AnswerRe: MessengerAPI Pin
ayalgelles1-Aug-06 3:12
memberayalgelles1-Aug-06 3:12 
GeneralNice tool but... Pin
Ed.Poore31-Jul-06 22:59
memberEd.Poore31-Jul-06 22:59 
GeneralRe: Nice tool but... Pin
ayalgelles1-Aug-06 3:16
memberayalgelles1-Aug-06 3:16 
GeneralRe: Nice tool but... Pin
Ed.Poore1-Aug-06 3:32
memberEd.Poore1-Aug-06 3: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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.181207.3 | Last Updated 24 Jul 2006
Article Copyright 2006 by ayalgelles
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid