Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  Windows Development  »  Interop  »  Expose MSN Messenger Events Using C#
Expose MSN Messenger Events Using C#
by James Jenkins | Published  07/24/2002 | Interop | Rating:
James Jenkins

I have been developing now since 1997. I am experienced in VB6, ASP, SQL and the new C# language. I have mainly spent my time as a contractor creating interactive applications for animation companies such as using Microsoft Agent technology. I am always on the look out for work!


View all articles by James Jenkins...
Expose MSN Messenger Events Using C#

Dependencies: MSN Messenger/Windows Messenger


If you have tried to work with the Messenger object then you will have come across a serious ComException error. The reason for this exception lies within the Interop process that creates the dll. There are two events that are declared as private as apposed to public within the Interop Messenger dll. First this needs to be changed using ILDasm and ILasm. I personally keep my working Interop.Messenger.dll in a separate folder (\Program files\Messenger\Interop) and only reference the corrected dll.

1)   Use the 'Command Prompt From here' system add-in available at by right clicking the folder object from the \Program Files\Messenger folder

2)   Run the command 'tlbimp msmsgs.exe /out:Interop.Messenger.dll'. This creates an assembly to reference in your project. The problem is that you cannot use this assembly as it will reject any attempt at accessing the events that are exposed by it, by throwing a ComException. The reason for this is that the assembly is trying to protect 2 *.SinkHelper events within it. It does this by using the private scope as apposed to public scope in its declaration.

3)   First we need to use ILDasm to disassemble the assembly. Again at the command prompt type 'ildasm Interop.Messenger.dll /out='

4)   Now you will need to open the newly created file with notepad (or by typing 'edit' at the command line) and find the two (2) occurences of SinkHelper where the scope is declared as private and change there decalaration scopes from private to public. Now save the and close it.

5)   We now need to assemble the into the Interop.Messenger.dll. This is done using the 'ilasm /dll' command.


6)   Now we are ready to reference the Assembly in a C# Windows Application. For our simple example you will need to logon to MSN/Windows Messenger before running the sample.

7)   Create a new C# Windows Application and add the following code to your project. Also add a TextBox to the form and set its MultiLine property to true

8)   Firstly reference the newly working assembly and add the 'using Interop.Messenger;' to the top of your code

using Interop.Messenger;

private void Form1_Load(object sender, System.EventArgs e)
    //This next line creates our Messenger Class
    MsgrObjectClass Msgr = new MsgrObjectClass();

    //These 3 lines create the events to be used
    Msgr.OnTextReceived += new DMsgrObjectEvents_OnTextReceivedEventHandler

    Msgr.OnUnreadEmailChanged += new

    Msgr.OnUserStateChanged += new

//The TextReceived Event
private void OnTextReceived(IMsgrIMSession Session, IMsgrUser User, 
    string Header, string MsgText, ref bool Enabled)
    //Simply write this info to the textbox
    textBox1.Text += User.ToString() + " " + Header.ToString();
    textBox1.Text += " " + Text.ToString();

//The OnUnreadEmailChanged event 
private void OnUnreadEmailChanged(Interop.Messenger.MFOLDER Folder, 
    int UnReadEmails, ref bool Enabled)
    //Simply write this info to the textbox
    textBox1.Text += "MFOLDER " + Folder.ToString();
    textBox1.Text += "a " + UnReadEmails.ToString();

//The OnUserStateChanged event
private void OnUserStateChanged(Interop.Messenger.IMsgrUser User, 
    Interop.Messenger.MSTATE PreviousState, ref bool Enabled)
    //Again simply write this info to the textbox
    textBox1.Text += User.FriendlyName + "'s state has changed to " 
        + User.State;

    textBox1.Text += User.FriendlyName + "'s previous state was " 
        + PreviousState;

9)   Now we have a working model you can see that by exposing the events within Messenger you have as much control over it as MSN Messenger itself. Run this for a while and watch as user states change, new email arrives and as text is received to appreciate the power you now have.

How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent
Tell us why you rated this way (optional):

Article Rating
The average rating is: No-one else has rated this article yet.

Article rating:3.88888888888888 out of 5
 54 people have rated this page
Article Score42128
Sponsored Links