For information about how to write and to use managed types from COM, refer to the following Microsoft .NET Framework Developer's Guide documentation:
Exposing .NET Framework Components to COM
http://msdn2.microsoft.com/en-us/library/zsfww439(vs.71).aspx
http://msdn2.microsoft.com/en-us/library/zsfww439(vs.71).aspx
Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:- Microsoft Visual Studio .NET
- Microsoft Internet Explorer (Programming) version 5.5 or later
Steps to Sink Managed Event in Internet Explorer Script
- Create a custom Windows Forms control:
- Start Microsoft Visual Studio .NET.
- On the File menu, point to New, and then click Project.
- In the New Project dialog box, click Visual C# Projects under Project Types, and then click Windows Control Library under Templates.
- Define a source interface for the events to be exposed.
- Add a GuidAttribute class to the source interface. You must format the string that you pass to the attribute as an acceptable constructor argument for the type Guid. You can use the Guidgen.exe file to create an unused GUID.
[GuidAttribute("0422D916-C11A-474e-947D-45A107038D12") ] public interface ControlEvents { // Insert code here. }
- Add an InterfaceType attribute to the source interface to expose COM as an IDispatch interface:
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)] public interface ControlEvents { // Insert code here. }
- Add a DispIdAttribute to any members in the source interface to specify the COM dispatch identifier (DISPID) of a method or a field:
public interface ControlEvents { [DispIdAttribute(0x60020000)] void ClickEvent(int x, int y); }
- Create a new event type to wrap the desired event to expose.
- Implement the source interface on the custom Windows Forms control.
- Add a ComSourceInterfaces attribute to the control to identify the list of interfaces that are exposed as COM event sources:
[ClassInterface(ClassInterfaceType.None),ComSourceInterfaces(typeof(ControlEvents))] public class MyWindowControl : System.Windows.Forms.UserControl { // Insert code here. }
- Compile the control as a dynamic-link library (DLL) file.
- Create a script block on the HTML page to hook the event. For example:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=iso-8859-1' /> <HTML> <HEAD> <TITLE>Sink managed event in Internet Explorer</TITLE> </HEAD> <BODY> <OBJECT id="ctrl" classid="YourDllName.dll#ActiveXSourcing.MyWindowControl"> </OBJECT> <SCRIPT LANGUAGE="JScript"> function ctrl::ClickEvent(a,b) { alert("MyWindowControl_ClickEvent"); } </SCRIPT> </BODY> </HTML>
- On any client system, use the .NET Framework Configuration tool (Mscorcfg.msc) to grant the assembly the individual permissions that are required.
Complete Code Listing
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace ActiveXSourcing
{
public delegate void ClickEventHandler(int x, int y);
//Source interface for events to be exposed.
//Add GuidAttribute to the source interface to supply an explicit System.Guid.
//Add InterfaceTypeAttribute to indicate that interface is IDispatch interface.
[GuidAttribute("0422D916-C11A-474e-947D-45A107038D12") ]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)]
public interface ControlEvents
//Add a DisIdAttribute to any members in the source
//interface to specify the COM DispId.
{
[DispIdAttribute(0x60020000)]
void ClickEvent(int x, int y);
}
//Add a ComSourceInterfaces attribute to the control to
//identify the list of interfaces that are exposed as COM event sources.
[ClassInterface(ClassInterfaceType.None),ComSourceInterfaces(typeof(ControlEvents))]
public class MyWindowControl : System.Windows.Forms.UserControl
//, ComInteropControlInterface
{
System.Windows.Forms.TextBox tx = new TextBox();
private void InitializeComponent()
{
this.Name = "MyWindowControl";
}
event ActiveXSourcing.ClickEventHandler ClickEvent;
public MyWindowControl() : base()
{
initMyWindowControl();
}
private void initMyWindowControl()
{
Size = new System.Drawing.Size(300, 50);
tx.Text = "Click on the TextBox to invoke 'ClickEvent'";
tx.Size = this.Size;
tx.Click += new System.EventHandler(ClickHandler);
this.Controls.Add(tx);
}
private void ClickHandler(object sender, System.EventArgs e)
{
if (ClickEvent != null)
{
ClickEvent(0, 0);
}
}
}
}