Article Options
Recently Viewed
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 http://www.e-clips.com.au 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

Prep

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 http://www.software-dungeon.co.uk 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= Interop.Messenger.il'

4)   Now you will need to open the newly created file Interop.Messenger.il with notepad (or by typing 'edit Interop.Messenger.il' 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 Interop.Messenger.il and close it.

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

Usage

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
        (this.OnTextReceived);

    Msgr.OnUnreadEmailChanged += new
        DMsgrObjectEvents_OnUnreadEmailChangedEventHandler
        (this.OnUnreadEmailChanged);

    Msgr.OnUserStateChanged += new
        DMsgrObjectEvents_OnUserStateChangedEventHandler
        (this.OnUserStateChanged);
}

//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:4.07407407407406 out of 5
 27 people have rated this page
Article Score34751
Comments    Submit Comment

Comment #1  (Posted by doose on 07/31/2002)

it's really a good article!but if the author could go into the details it would be fantastic!
 
Comment #2  (Posted by Ellery Familia on 08/05/2002)

the author should just post a working test project.
 
Comment #3  (Posted by Mahdi Fadavi on 08/19/2002)

it's really a good article! thank you for writing it.
 
Comment #4  (Posted by Levy on 09/14/2002)

Excellent Article...and it WORKS !! I have been coding against MSN Messenger for a few years now, this is the first .NET article seen....keep it up :-) ..Thanks for posting it.
 
Comment #5  (Posted by Farman on 10/09/2002)

Excellent Example but for begginer not for advance programmer. i really appreciate yr sharpness
 
Comment #6  (Posted by Pushpendu on 10/23/2002)

The article as is very good. But I am trying to make it behave like MSN Explorer which notifies every time a new mail arrives. This piece of code notfies only for the first mail that comes in after the application starts.

How can I make it behave like MSN Messenger?

Thanks,
Pushpendu
 
Comment #7  (Posted by Stephen Gutknecht on 11/09/2002)

Note that going from MSN Messenger 4.6 ---> 5.0 breaks all my code. The libraries are no longer compatible.

The interfaces you modify (private to public) appear to have been renamed.
 
Comment #8  (Posted by an unknown user on 11/22/2002)

In delegate OnTextReceived,

textBox1.Text += " " + Text.ToString() + "\n";

should be
textBox1.Text += " " + MsgText.ToString() + "\n";

Pushpendu, you will be notify in this method when new email arrives. So do what ever you like.
 
Comment #9  (Posted by Michael Hennessy on 12/17/2002)

Ok, I followed the steps in this article, but when I attempt to run it, I continue to get the COM exception error!
I have MSN Messenger 4.6 (4.6.0083).

I did find the 2 occurances of private for sinkhelper...and changed them to public.
What am I missing?
Can I get a copy of the alterened interop.messenger.dll file?

 
Comment #10  (Posted by WillemM on 01/01/2003)

Is this dll also useable in VB.NET ?

I can't get it to work :(

The events don't expose in VB, if I do it your way
If I do it like this:

public withevents msgr as Interop.Messenger.MsgrObjectClass

then it does expose events, but does not activate them :\
 
Comment #11  (Posted by Dave on 02/06/2003)

I built this project and it builds fine and runs, but nothing shows up in the textbox...i triggered some events i sent myself an email from another computer, i tried it with messenger minimized and open no results...i am using messenger version 4.7 any suggestions?
 
Comment #12  (Posted by Nilz on 02/11/2003)

Hi,

The exampleis brilliant, works fine for a while, but after 5-6 seconds it stops working for some odd reasons. It seams to me that maybe it is disconnected or something. Anyone else experienced this problem?

I am using messenger 4.7.0105

Appreciate all comments :)
 
Comment #13  (Posted by Mostafa Fallah on 03/10/2003)

It was very good. I using it many times !!!!!!!
Thanks !
 
Comment #14  (Posted by Sumit Amar on 04/17/2003)

That is really a nice article, but not the pasteable solution as some people might expect. lol. The author has really done a good job.

Cheers
 
Comment #15  (Posted by Dude Net on 04/23/2003)

As of April 20th, This object is obselete....... too bad guys... find another way... to expose.
 
Comment #16  (Posted by drteeth on 05/24/2003)

Is there an event for when the user types text? Meaning an event for when the local user hits the send button?
 
Comment #17  (Posted by Hamid.Reza Kazempour on 06/28/2003)

hi
tanks for your code it's really good code
 
Comment #18  (Posted by hamidreza kazempour on 07/16/2003)

this good
 
Comment #19  (Posted by Jimmy Chang on 04/21/2004)

It seems like there is no reference for MFOLDER and IMsgrUser ?
 
Comment #20  (Posted by Anand Shinde on 09/06/2004)

I am not able to use Tlbimp on smsgs.exe I get an Invalid type library error. can I get the Interop.Messenger file?
 
Comment #21  (Posted by Timothy on 10/06/2004)

Hi,
This article is in fact about Windows Messenger,
what changes need to be made to make it work with MSN Messenger (v6 or higher)
It does not seem to expose the same objects

Thnx.
 
Comment #22  (Posted by david on 02/21/2005)
Rating
i've been using this way of accesing the messenger object, but i have one problem. All the events are received ok, but there are one that is cacthed by the Microsoft messenger and not by my app:
mo.OnAppInviteReceived+=new DMsgrObjectEvents_OnAppInviteReceivedEventHandler(mo_OnAppInviteReceived);

the OnAppInviteReceived event.
are there any way to capture this event?
 
Comment #23  (Posted by sudhakar on 03/08/2005)
Rating
Excellent Article this is the first .NET article seen....keep it up :-) ..Thanks for posting it. ..sudhakar_devalla@yahoo.com

 
Comment #24  (Posted by GNM Raju on 03/08/2005)
Rating
Hai i fould this article excellent and it uses to all who are .NET Programmers, Anyway thanks for positing this type of articles. g_n_m_raju@yahoo.com
 
Comment #25  (Posted by M.Smitha on 03/08/2005)
Rating
It works fine and useful to all who are .NET Professionals and VC++ Programmers also, anyway keep these types of articles in the net so that everyone will use that, thanks
 
Comment #26  (Posted by G.Ch.Apparao on 03/08/2005)
Rating
Sorry, I can't understand.
 
Comment #27  (Posted by an unknown user on 05/31/2005)
Rating
It is good for help the students.
 
Comment #28  (Posted by an unknown user on 07/20/2005)
Rating
At Step 1, I think it would be a lot easier to just go to start->all programs->accesories->Command Prompt. Then type in 'cd %programfiles%\Messenger'.
 
Comment #29  (Posted by an unknown user on 04/22/2006)
Rating
ahh! lol well windows messenger works ok but people are using v6 or 7 or above and i need to develop the same type of app for the newer verions. I have most of the bugs worked out except for the MFOLDER. Any help would be great
SpugMan@gmail.com
http://www.spugman.com
 
Comment #30  (Posted by Adnan on 09/14/2006)
Rating
It requres MSN messenger to be installed on machine.what I am looking for a BOT based system
 
Comment #31  (Posted by an unknown user on 08/13/2007)
Rating
well
 
Comment #32  (Posted by an unknown user on 10/01/2007)
Rating
This saqmple doesn' t work on my pc :(
 
Comment #33  (Posted by Fabricio on 12/12/2007)
Rating
if you have a COM exception you must to install Windows Messenger 5.1 (from Microsoft). Note Windows Messenger is not MSN Messenger. Next, login in Windows Messenger an all events will run perfect. textreceived event doesn't works with MSN Messenger
 
Comment #34  (Posted by an unknown user on 12/19/2007)
Rating
wonderful (jungle1986@gmail.com)
 
Comment #35  (Posted by an unknown user on 01/26/2008)
Rating
Why "Interop.Messenger.il /dll" is wrong?
 
Comment #36  (Posted by an unknown user on 11/12/2008)
Rating
1erz6r8npo4yn82k
 
Comment #37  (Posted by an unknown user on 11/13/2008)
Rating
1erz6r8npo4yn82k
 
Comment #38  (Posted by an unknown user on 12/01/2008)
Rating
aldellibasc
 
Comment #39  (Posted by no1 on 04/14/2009)
Rating
Does this work on Windows Live Messenger 2009?
 
Comment #40  (Posted by Adobe OEM Software on 03/07/2012)
Rating
I49PWQ Thanks again for the blog. Much obliged.
 
Sponsored Links