The following describes how to create a C# Class Library dll which you can use as an ActiveX Control in Internet Explorer It will automatically register itself on build and expose properties, methods and raise events to Internet Explorer. It implements the IObjectSafety interface to mark the control as safe for scripting within Internet Explorer. There are separate interfaces for events and properties and methods.
- Create a new C# Class Libray project in Visual Studio 2005.
- In the Project Properties for the Application click the “Assembly Information…” button and check the Make Assembly COM-Visible checkbox.
- In the Build Properties check the Register for COM Interop checkbox.
using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using System.Windows.Forms; namespace ActiveXControl { [Guid("A59B958D-B363-454b-88AA-BE8626A131FB")] [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] public interface ICOMEvents { [DispId(0x60020000)] void MyEvent(); } public delegate void MyEventHandler(); public interface ICOMControl { void myMethod(); string theTime { get; } } [Guid("CB5BDC81-93C1-11CF-8F20-00805F2CD064")] [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] public interface IObjectSafety { [PreserveSig] int GetInterfaceSafetyOptions(ref Guid riid, out int pdwSupportedOptions, out int pdwEnabledOptions); [PreserveSig] int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions); } [GuidAttribute("066700A6-4BD9-43fd-8839-E094E34E6773")] [ComSourceInterfaces(typeof(ICOMEvents))] public class Library : IObjectSafety, ICOMControl { public event MyEventHandler MyEvent; private const int INTERFACESAFE_FOR_UNTRUSTED_CALLER = 0x00000001; private const int INTERFACESAFE_FOR_UNTRUSTED_DATA = 0x00000002; private const int S_OK = 0; int IObjectSafety.GetInterfaceSafetyOptions(ref Guid riid, out int pdwSupportedOptions, out int pdwEnabledOptions) { pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA; pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA; return S_OK; } int IObjectSafety.SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions) { return S_OK; } public string theTime { get { return DateTime.Now.ToString(); } } public void myMethod() { if (MyEvent == null) { MessageBox.Show("MyEvent delegate is null"); } else { MyEvent(); } } } }Now here is the html:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> </head> <script language="jscript" type="text/javascript"> function loaded(){ alert(wembley.theTime()); } </script> <body onload="loaded()"> <object classid="clsid:066700A6-4BD9-43fd-8839-E094E34E6773" name="myControl" id="myControl"></object> <input type="button" value="Call Method" onclick="wembley.myMethod()"></input> </body> <script for="myControl" event="MyEvent"> function myControl::MyEvent(){ alert("Event Fired!"); } </script> </html>
1 comments:
this is a piece of art thank you sir you helped a lot .
Post a Comment