Use MSAgent in C #

Source: Internet
Author: User
Check the data when suddenly see MSAgent programming, find some data to write a few lines of code.

If there is no msagent runtime or SDK, you can go to www.microsoft.com download, if you use VC + +, you need to download two header files.

After you create a. NET project, add a reference, select a COM page, and then select Microsoft Agent Server 2.0. and refer to the Agentserverobject namespace where you need to use the agent.

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>

Summary description of the 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>

The required designer variable.

</summary>

Private System.ComponentModel.Container components = null;



Public Form1 ()

{

//

Required for Windows Forms Designer support

//

InitializeComponent ();



//

TODO: Add any constructor code after the InitializeComponent call

//

}



<summary>

Clean up all resources that are in use.

</summary>

protected override void Dispose (bool disposing)

{

if (disposing)

{

if (Components!= null)

{

Components. Dispose ();

}

}

Base. Dispose (disposing);

}



Code generated #region the Windows forms Designer

<summary>

Designer supports required methods-do not use the Code editor to modify

The contents of this method.

</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_Load);

This. ResumeLayout (FALSE);



}

#endregion



<summary>

The main entry point for the application.

</summary>

[STAThread]

static void Main ()

{

Application.Run (New Form1 ());

}



private void Initagent ()

{

Agentserver = new Agentserverobjects.agentserverclass ();

if (Agentserver = null)

{

MessageBox.Show ("Agent initialization failed");

}



Agentex = (Iagentex) agentserver;

Agentex.load (Strcharname, out of Dwcharid, out dwreqid);

Agentex.getcharacterex (Dwcharid, out Characterex);



Characterex.show (M, out dwreqid);

Characterex.speak ("Hello", NULL, out dwreqid);



String strName = "";

Characterex.getname (out strName);

Characterex.speak ("My name are" + StrName, NULL, out dwreqid);



sink = new Agentevent (new Speakdelegate (speaksomething));

Agentex.register (sink, out dwsinkid);

}



private void Form1_Load (object sender, System.EventArgs e)

{

Initagent ();

}



public delegate void Speakdelegate (String str);

public void speaksomething (String str)

{

if (invokerequired)

{

BeginInvoke (New Speakdelegate (speaksomething), New object[]{str});

Return

}



Characterex.wait (1000, out dwreqid);

Thread.Sleep (1000);

Characterex.speak (str, NULL, out dwreqid);

}



private void ThreadProc ()

{

Speaksomething ("Hello,this is use delegate.");

}



private void Btndelegate_click (object sender, System.EventArgs e)

{

Thread thread = new Thread (new ThreadStart (ThreadProc));

Thread. IsBackground = true;

Thread. Start ();

}

}

}

The code is not commented, because I can't find the place to comment, please forgive me.

The agent's event needs to inherit some of the interfaces under the Agengserverobject namespace.

Using System;

Using Agentserverobjects;

Namespace Helloagent

{

<summary>

///

</summary>

public class Agentevent:iagentnotifysink

{

Private form1.speakdelegate Delg = null;



Public agentevent (Form1.speakdelegate Delg)

{

//

TODO: Add constructor logic here

//

This.delg = Delg;

}

#region Iagentnotifysink Members



public void Command (int dwcommandid, Object punkuserinput)

{

TODO: Add Agentevent.command implementation

Delg ("Command");

}



public void DragStart (int dwcharid, short fwkeys, int x, int y)

{

TODO: Add Agentevent.dragstart implementation

Delg ("DragStart");

}



public void Requeststart (int dwrequestid)

{

TODO: Add Agentevent.requeststart implementation

Delg ("Requeststart");

}



public void visiblestate (int dwcharid, int bvisible, int dwcause)

{

TODO: Add agentevent.visiblestate implementation

Delg ("Visiblestate");

}



public void balloonvisiblestate (int dwcharid, int bvisible)

{

TODO: Add agentevent.balloonvisiblestate implementation

Delg ("Balloonvisiblestate");

}



public void Dragcomplete (int dwcharid, short fwkeys, int x, int y)

{

TODO: Add Agentevent.dragcomplete implementation

Delg ("Dragcomplete");

}



public void Click (int dwcharid, short fwkeys, int x, int y)

{

TODO: Add Agentevent.click implementation

Delg ("click");

}



public void DblClick (int dwcharid, short fwkeys, int x, int y)

{

TODO: Add Agentevent.dblclick implementation

Delg ("DblClick");

}



public void Shutdown ()

{

TODO: Add Agentevent.shutdown implementation

Delg ("Shutdown");

}



public void Size (int dwcharid, int lwidth, int lheight)

{

TODO: Add agentevent.size implementation

Delg ("Size");

}



public void Restart ()

{

TODO: Add Agentevent.restart implementation

Delg ("restart");

}



public void BookMark (int dwbookmarkid)

{

TODO: Add Agentevent.bookmark implementation

Delg ("BookMark");

}



public void Requestcomplete (int dwrequestid, int hrstatus)

{

TODO: Add Agentevent.requestcomplete implementation

Delg ("Requestcomplete");

}



public void Move (int dwcharid, int x, int y, int dwcause)

{

TODO: Add Agentevent.move implementation

Delg ("move");

}



public void Idle (int dwcharid, int bstart)

{

TODO: Add Agentevent.idle implementation

Delg ("Idle");

}



public void activateinputstate (int dwcharid, int bactivated)

{

TODO: Add agentevent.activateinputstate implementation

Delg ("Activateinputstate");

}



#endregion



}

}

When the event is triggered, a delegate is called inside a Form1, which is initialized when the event class is constructed. Please refer to the details: ...




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.