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



The information in this article applies to:

  • Microsoft Visual C# .NET (2002)
  • Microsoft Internet Explorer (Programming) 5.5

This article was previously published under Q313891
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

IN THIS TASK

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

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

REFERENCES

For more information, refer to the following Microsoft Web sites: 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

Modification Type:MajorLast Reviewed:6/22/2006
Keywords:kbCOMInterop kbhowto kbHOWTOmaster kbSample KB313891 kbAudDeveloper