You are currently offline, waiting for your internet to reconnect

HOW TO: Sink Managed C# Events in Internet Explorer Script

This article was previously published under Q313891
This article has been archived. It is offered "as is" and will no longer be updated.
For a Microsoft Visual Basic .NET version of this article, see 316516.

This article refers to the following Microsoft .NET Framework Class Library namespace:
  • System.Runtime.InteropServices


This step-by-step article shows you how to sink managed events from Component Object Model (COM) clients (unmanaged code) when you write .NET Windows controls. For example, you sink managed events from COM clients when you run script in Internet Explorer.

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
back to the top


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
back to the top

Steps to Sink Managed Event in Internet Explorer Script

  1. Create a custom Windows Forms control:
    1. Start Microsoft Visual Studio .NET.
    2. On the File menu, point to New, and then click Project.
    3. In the New Project dialog box, click Visual C# Projects under Project Types, and then click Windows Control Library under Templates.
  2. Define a source interface for the events to be exposed.
  3. 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.    }					
  4. Add an InterfaceType attribute to the source interface to expose COM as an IDispatch interface:
    [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)]    public interface ControlEvents 		    {               // Insert code here.    }					
  5. 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);    }					
  6. Create a new event type to wrap the desired event to expose.
  7. Implement the source interface on the custom Windows Forms control.
  8. 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.    }					
  9. Compile the control as a dynamic-link library (DLL) file.
  10. 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>					
  11. On any client system, use the .NET Framework Configuration tool (Mscorcfg.msc) to grant the assembly the individual permissions that are required.
back to the top

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);			}		}	}}				
back to the top
For more information, refer to the following Microsoft Web sites:
Handling and Raising Events

.NET Framework Configuration Tool (Mscorcfg.msc)

Interoperating with Unmanaged Code

Host Secure, Lightweight Client-Side Controls in Microsoft Internet Explorer
For additional information, click the article number below to view the article in the Microsoft Knowledge Base:
316510 BUG: Security Exception When You Use Event Handlers in Internet Explorer
back to the top

Article ID: 313891 - Last Review: 12/07/2015 08:17:30 - Revision: 3.4

Microsoft Visual C# .NET 2002 Standard Edition, Microsoft Internet Explorer 5.5

  • kbnosurvey kbarchive kbcominterop kbhowto kbhowtomaster kbsample KB313891