日期:2009-06-10  浏览次数:20552 次

查资料的时候突然看到MSAgent编程,找了些资料动手写了几行代码。

如果没有MSAgent runtime或者SDK的话,可以去www.microsoft.com下载,如果用VC++,则需要下载两个头文件。


创建.net项目以后,添加一个引用,选择COM页,然后选择Microsoft Agent Server 2.0。并在需要使用Agent的地方引用AgentServerObject的名字空间。

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using AgentServerObjects;

using System.Threading;

namespace HelloAgent

{

/// <summary>

/// Form1 的摘要说明。

/// </summary>

public class Form1 : System.Windows.Forms.Form

{

private System.Windows.Forms.Button btnDelegate;



private AgentServer agentServer = null;

private IAgentEx agentEx = null;

private IAgentCharacterEx characterEx = null;

private IAgentNotifySink sink = null;



private int dwCharID = 0;

private int dwReqID = 0;

private int dwSinkID = 0;

private string strCharName = "C:\\WINDOWS\\msagent\\chars\\merlin.acs";

/// <summary>

/// 必需的设计器变量。

/// </summary>

private System.ComponentModel.Container components = null;



public Form1()

{

//

// Windows 窗体设计器支持所必需的

//

InitializeComponent();



//

// TODO: 在 InitializeComponent 调用后添加任何构造函数代码

//

}



/// <summary>

/// 清理所有正在使用的资源。

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}



#region Windows 窗体设计器生成的代码

/// <summary>

/// 设计器支持所需的方法 - 不要使用代码编辑器修改

/// 此方法的内容。

/// </summary>

private void InitializeComponent()

{

this.btnDelegate = new System.Windows.Forms.Button();

this.SuspendLayout();

//

// btnDelegate

//

this.btnDelegate.Location = new System.Drawing.Point(96, 32);

this.btnDelegate.Name = "btnDelegate";

this.btnDelegate.TabIndex = 0;

this.btnDelegate.Text = "delegate";

this.btnDelegate.Click += new System.EventHandler(this.btnDelegate_Click);

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

this.ClientSize = new System.Drawing.Size(292, 273);

this.Controls.Add(this.btnDelegate);

this.Name = "Form1";

this.Text = "Form1";

this.Load += new System.EventHandler(this.Form1_L