This article describes 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 Microsoft Internet Explorer.
For information about how to write and how to use managed types from COM, refer to the following Microsoft .NET Framework Developer's Guide documentation:
Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
- Microsoft Visual Studio .NET
- Internet Explorer (Programming) version 5.5 or 6
To Sink a Managed Event in an Internet Explorer Script
- Create a custom Windows Forms control:
- Start Visual Studio .NET.
- On the File menu, point to New, and then click Project.
- In the Add New Project dialog box, click Visual Basic Projects under Project Types, and then click Windows Control Library under Templates.
- Name the project MyVBControl.
- Define a source interface for the events that are 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 Guid type. You can use Guidgen.exe to create an unused GUID.
<GuidAttribute("1F98211C-7A71-4588-8D4A-AD85CA80BAE7"), _
Public Interface ControlEvents
Sub ClickEvent(ByVal x As Integer, ByVal y As Integer)<BR/>
End Interface
- Add an InterfaceType attribute to the source interface to expose COM as an IDispatch interface:
InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)> _
Public Interface ControlEvents
Sub ClickEvent(ByVal x As Integer, ByVal y As Integer)<BR/>
End Interface
- Add DispIdAttribute to any members in the source interface to specify the COM dispatch identifier (DISPID) of a method or a field:
Public Interface ControlEvents
'Add DisIdAttribute to any members in the source interface to specify the COM DISPID.
<DispIdAttribute(1)> _
Sub ClickEvent(ByVal x As Integer, ByVal y As Integer)
End Interface
- Create a new event type to wrap the event that is to be exposed.
- 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:
<ComSourceInterfaces(GetType(ControlEvents)), _
ClassInterface(ClassInterfaceType.None)> _
Public Class MyVBControl
- 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="MyVBControl.dll#MyVBControl.MyVBControl">
</OBJECT>
<SCRIPT LANGUAGE="JScript">
function ctrl::ClickEvent(a,b)
{
alert("MyWindowControl_ClickEvent");
}
</SCRIPT>
</BODY>
</HTML>
- On any client computer, use the .NET Framework Configuration tool (Mscorcfg.msc) to grant the assembly the individual permissions that are required. Or, follow these steps to reduce the framework security for testing:
- Start a command prompt in Visual Studio .NET.
- At the command prompt, turn the framework security off by using the caspol -s off command.
- On the Internet Explorer client, reduce the local internet security to low for testing. On the Tools menu in Internet Explorer, click Internet Options, and then click the Security tab.
Note You must place the SCRIPT block in the HTML page after the OBJECT tag for the function to work. This requirement is caused by the loading order.
Complete Sample Code Listing
Imports System
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Public Delegate Sub ClickEventHandler(ByVal x As Integer, ByVal y As Integer)
' Source interface for events to be exposed.
' Add GuidAttribute to the source interface to supply an explicit System.Guid.
' Add InterfaceTypeAttribute to indicate that the interface is an IDispatch interface.
<GuidAttribute("1F98211C-7A71-4588-8D4A-AD85CA80BAE7"), _
InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)> _
Public Interface ControlEvents
' Add DisIdAttribute to any members in the source interface to specify the COM DISPID.
<DispIdAttribute(1)> _
Sub ClickEvent(ByVal x As Integer, ByVal y As Integer)
End Interface
' Add a ComSourceInterfaces attribute to the control to identify the list of interfaces that are exposed as COM event sources.
<ComSourceInterfaces(GetType(ControlEvents)), _
ClassInterface(ClassInterfaceType.None)> _
Public Class MyVBControl
Inherits System.Windows.Forms.UserControl
Dim tx As New System.Windows.Forms.TextBox()
Public Event ClickEvent(ByVal x As Integer, ByVal y As Integer)
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
Me.Name = "MyVBControl"
initMyVBWindowControl()
'Add any initialization after the InitializeComponent() call.
End Sub
' UserControl overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
' Required by the Windows Form Designer.
Private components As System.ComponentModel.IContainer
' NOTE: The following procedure is required by the Windows Form
' Designer. You can modify it by using the Windows Form Designer.
' Do not modify it by using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
'
' MyVBControl
'
End Sub
Private Sub initMyVBWindowControl()
Size = New System.Drawing.Size(300, 50)
tx.Text = "Click on the TextBox to invoke 'ClickEvent'"
tx.Size = Me.Size
AddHandler tx.Click, New System.EventHandler(AddressOf ClickHandler)
Me.Controls.Add(tx)
End Sub
Private Sub ClickHandler(ByVal sender As Object, ByVal e As System.EventArgs)
RaiseEvent ClickEvent(0, 0)
End Sub
#End Region
End Class