Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

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


View products that this article applies to.

Summary

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
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

  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.

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


References

For more information, refer to the following Microsoft Web sites:
Handling and Raising Events
http://msdn2.microsoft.com/en-us/library/edzehd2t(vs.71).aspx

.NET Framework Configuration Tool (Mscorcfg.msc)
http://msdn2.microsoft.com/en-us/library/2bc0cxhc(vs.71).aspx

Interoperating with Unmanaged Code
http://msdn2.microsoft.com/en-us/library/sd10k43k(vs.71).aspx

Host Secure, Lightweight Client-Side Controls in Microsoft Internet Explorer
http://msdn.microsoft.com/msdnmag/issues/02/01/UserCtrl/default.aspx
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


Keywords: KB313891, kbsample, kbhowtomaster, kbhowto, kbcominterop

↑ Back to the top

Article Info
Article ID : 313891
Revision : 7
Created on : 5/17/2007
Published on : 5/17/2007
Exists online : False
Views : 371