荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: Mic (酷鱼), 信区: DotNET
标  题: Expose MSN Messenger Events Using C#
发信站: 荔园晨风BBS站 (Mon Jul 29 11:02:20 2002), 站内信件

Expose MSN Messenger Events Using C#
James Jenkins
7/24/2002


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;


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店