1,定义接口
(1)在接口上方加上[Guid("1EA4DBF0-3C3B-11CF-810C-00AA00389B71")]
(2)在每个方法上加上[DispId(i)]
using System;
using System.Runtime.InteropServices;
namespace Leading.Mis.NewsMonitor
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
......{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**//**//// <summary>
/// 消息监控接口的定义
/// </summary>
[Guid("1EA4DBF0-3C3B-11CF-810C-00AA00389B71")]
public interface INewsMonitor
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
......{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
发送文件时,记录发送者,接收者和发送的文件名发送文件时,记录发送者,接收者和发送的文件名#region 发送文件时,记录发送者,接收者和发送的文件名
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**//**//// <summary>
/// 当发送文件时,记录发送者,接收者和发送的文件名
/// </summary>
/// <param name="strSendID">文件发送者</param>
/// <param name="strTargetID">文件接受者</param>
/// <param name="strFileName">文件名</param>
[DispId(1)]
void SendFile(string strSendID,string strTargetID,string strFileName);
#endregion
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
接收文件时,记录文件接收者,发送者和发送的文件名接收文件时,记录文件接收者,发送者和发送的文件名#region 接收文件时,记录文件接收者,发送者和发送的文件名
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**//**//// <summary>
/// 接受文件时,记录文件接收者,发送者和发送的文件名
/// </summary>
/// <param name="strReceiveID">文件接收者</param>
/// <param name="strSendID">文件发送者</param>
/// <param name="strFileName">文件名</param>
[DispId(2)]
void RecvFile(string strReceiveID,string strSendID,string strFileName);
#endregion
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
文件发送成功后,记录发送者,接收者和发送的文件名文件发送成功后,记录发送者,接收者和发送的文件名#region 文件发送成功后,记录发送者,接收者和发送的文件名
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**//**//// <summary>
/// 文件发送成功后,记录发送者,接收者和发送的文件名
/// </summary>
/// <param name="strSendID">文件发送者</param>
/// <param name="strTargetID">文件接收者</param>
/// <param name="strFileName">文件名</param>
[DispId(3)]
void FileSendOK(string strSendID,string strTargetID,string strFileName);
#endregion
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
取消文件发送时,记录发送者,接收者和发送的文件名取消文件发送时,记录发送者,接收者和发送的文件名#region 取消文件发送时,记录发送者,接收者和发送的文件名
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**//**//// <summary>
/// 取消文件发送时,记录发送者,接收者和发送的文件名
/// </summary>
/// <param name="strCancelID">取消文件发送者</param>
/// <param name="strTargetID">文件发送对方</param>
/// <param name="strFileName">文件名</param>
[DispId(4)]
void FileSendCancel(string strCancelID,string strTargetID,string strFileName);
#endregion
} // end interface INewsMonitor
} //end namespace Leading.Mis.NewsMonitor
2,类的实现
(1)在类的上方加上 [Guid("1EA4DBF0-3C3B-11CF-810C-00AA00389B71")]
[ClassInterface(ClassInterfaceType.None)]
using System;
using System.IO;
using System.Runtime.InteropServices;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
namespace Leading.Mis.NewsMonitor
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
......{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**//**//// <summary>
/// 消息监控类的实现
/// </summary>
[Guid("1EA4DBF0-3C3B-11CF-810C-00AA00389B71")]
[ClassInterface(ClassInterfaceType.None)]
public class NewsMonitor : INewsMonitor
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
......{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
私有字段#region 私有字段
private string filePath = string.Empty;
private string writeLine = string.Empty;
#endregion
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
私有方法#region 私有方法
private void NewsMsg()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
......{
StreamWriter sw = null;
if(File.Exists(filePath))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
......{
sw = File.AppendText(filePath);
}
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
......{
FileStream fs = File.Create(filePath);
sw = new StreamWriter(fs);
}
sw.WriteLine(writeLine);
sw.Close();
}
#endregion
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
共有方法#region 共有方法
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
发送文件时,记录发送者,接收者和发送的文件名#region 发送文件时,记录发送者,接收者和发送的文件名
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 当发送文件时,记录发送者,接收者和发送的文件名
/// </summary>
/// <param name="strSendID">文件发送者</param>
/// <param name="strTargetID">文件接受者</param>
/// <param name="strFileName">文件名</param>
public void SendFile(string strSendID,string strTargetID,string strFileName)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
......{
filePath = @"c:MsgMonitorSendFile.txt";
writeLine = strSendID + " 向 " + strTargetID + " 发送 < " + strFileName + " > 文件";
NewsMsg();
}
#endregion
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
接收文件时,记录文件接收者,发送者和发送的文件名#region 接收文件时,记录文件接收者,发送者和发送的文件名
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 接受文件时,记录文件接收者,发送者和发送的文件名
/// </summary>
/// <param name="strReceiveID">文件接收者</param>
/// <param name="strSendID">文件发送者</param>
/// <param name="strFileName">文件名</param>
public void RecvFile(string strReceiveID,string strSendID,string strFileName)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
......{
filePath = @"c:MsgMonitorRecvFile.txt";
writeLine = strReceiveID + " 接收到 " + strSendID + " 发送的 < " + strFileName + " > 文件";
NewsMsg();
}
#endregion
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
文件发送成功后,记录发送者,接收者和发送的文件名#region 文件发送成功后,记录发送者,接收者和发送的文件名
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 文件发送成功后,记录发送者,接收者和发送的文件名
/// </summary>
/// <param name="strSendID">文件发送者</param>
/// <param name="strTargetID">文件接收者</param>
/// <param name="strFileName">文件名</param>
public void FileSendOK(string strSendID,string strTargetID,string strFileName)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
......{
filePath = @"c:MsgMonitorFileSendOK.txt";
writeLine = strSendID + " 已经成功向 " + strTargetID + " 发送 < " + strFileName + " > 文件";
NewsMsg();
}
#endregion
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
取消文件发送时,记录发送者,接收者和发送的文件名#region 取消文件发送时,记录发送者,接收者和发送的文件名
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 取消文件发送时,记录发送者,接收者和发送的文件名
/// </summary>
/// <param name="strCancelID">取消文件发送者</param>
/// <param name="strTargetID">文件发送对方</param>
/// <param name="strFileName">文件名</param>
public void FileSendCancel(string strCancelID,string strTargetID,string strFileName)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
......{
filePath = @"c:MsgMonitorFileSendCancel.txt";
writeLine = strCancelID + " 取消和 " + strTargetID + " 发送 < " + strFileName + " > 文件";
NewsMsg();
}
#endregion
#endregion
} // end class NewsMonitor
} //end namespace Leading.Mis.NewsMonitor
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
3,用 Regasm 工具进行注册
Regasm NewsMsg.dll / tlb: NewsMsg.tlb