How to write an ActiveX or COM .NET Object to be used in Internet Explorer (IE) in csharp (C#)

D

Daniel Roth

How to write an ActiveX or COM .NET Object to be used in Internet
Explorer (IE) in csharp (C#)

1. Code the NET Class
2. regasm TestActXEvent.dll /tlb:TestActXEvent.tlb /codebase
3. Code the IE client

//////////the .NET Class
using System;
using System.Runtime.InteropServices;

namespace TestActXEvent
{

/// <summary>
/// Summary description for Class1.
/// </summary>
[Guid("EAA4976A-45C3-4BC5-BC0B-E474F4C3C83F")]
public interface IComClassCS
{
string Method();
}

public delegate void MyEventHandler();

// Events interface
[Guid("7BD20046-DF8C-44A6-8F6B-687FAA26FA71"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IComClassCSEvents
{
[DispIdAttribute(0x60020000)]
void MyEvent();
}

// Class ComClassCS
[Guid("0D53A3E8-E51A-49C7-944E-E72A2064F938"),
ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(IComClassCSEvents))]
public class ComClassCS : IComClassCS
{
public event MyEventHandler MyEvent;

public ComClassCS()
{
}
public string Method()
{
try
{
if(MyEvent != null)
{
MyEvent();
}

}
catch(Exception ex)
{
return "Error is " + ex.Message + "; " + ex.GetType().ToString();
}
return "C# COM Object return string";
}
}

}

////The IE Client
<HTML>
<HEAD>
<title>WebForm1</title>
</HEAD>
<BODY>
<OBJECT id="TestUserControl"
classid="clsid:0D53A3E8-E51A-49C7-944E-E72A2064F938"
name="TestUserControl" VIEWASTEXT>
</OBJECT>
<FORM id="Form1" method="post" runat="server">
<input type="button" value="Click me" onClick="doScript();">
</FORM>
<SCRIPT LANGUAGE="javascript">
function doScript()
{
window.alert( TestUserControl.Method() );
}
function TheEventHandler()
{
window.alert("The Event was fired");
}
</SCRIPT>
<SCRIPT LANGUAGE="javascript" for="TestUserControl" event="MyEvent">
TheEventHandler();
</SCRIPT>

</BODY>
</HTML>

Daniel Roth
MCSD.NET
 
D

Dmytro Lapshyn [MVP]

Daniel,

Do I assume right the object is non-visual (has no UI)?
Otherwise you might be relying on undocumented features of the Windows
Forms' Control class.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top