472,097 Members | 1,068 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,097 software developers and data experts.

Creating ActiveX events in C# that can be used by javascript


this is my first post so first of all I would like to say hello :)

Now getting to my problem.

In my job I have to create an ActiveX control in .NET 1.1 that can be
lunched from IE.
This part is done but next requirement is that after closing ActiveX I
have to redirect parent page to location X or Y depending on how the
ActiveX control has been closed. This looks like a job for event
raised in ActiveX and captured in javascript. After search in Internet
I have found this page:
This tutorial is written in .NET 2.0.

Using this example I have wrote my own.

.cs file:

using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace ActiveXEventsTest

[InterfaceType(ComInterfaceType.InterfaceIsIDispatc h)]
public interface IGadgetControlEvents
void RightClickRelay(int x, int y);


[InterfaceType(ComInterfaceType.InterfaceIsIDispatc h)]
public interface IGadgetIncoming
void SetVariableName(string name, string value);

public class Class1
public Class1()


public delegate void MouseRightClickHandler(int x, int y);
public event MouseRightClickHandler RightClickRelay;

public void open()
if (RightClickRelay != null)
RightClickRelay(11, 23);
MessageBox.Show("Empty event");


/// <summary>
/// Register the class as a control and set it's CodeBase entry
/// </summary>
/// <param name="key">The registry key of the control</param>
public static void RegisterClass ( string key )
// Strip off HKEY_CLASSES_ROOT\ from the passed key as I don't need
StringBuilder sb = new StringBuilder ( key ) ;

sb.Replace(@"HKEY_CLASSES_ROOT\","") ;
// Open the CLSID\{guid} key for write access
RegistryKey k =
Registry.ClassesRoot.OpenSubKey(sb.ToString(),true );

// And create the 'Control' key - this allows it to show up in
// the ActiveX control container
RegistryKey ctrl = k.CreateSubKey ( "Control" ) ;
ctrl.Close ( ) ;

// Next create the CodeBase entry - needed if not string named and
RegistryKey inprocServer32 = k.OpenSubKey ( "InprocServer32" ,
true ) ;
inprocServer32.SetValue ( "CodeBase" ,
Assembly.GetExecutingAssembly().CodeBase ) ;
inprocServer32.Close ( ) ;
// Finally close the main key
k.Close ( ) ;

/// <summary>
/// Called to unregister the control
/// </summary>
/// <param name="key">Tke registry key</param>
public static void UnregisterClass ( string key )
StringBuilder sb = new StringBuilder ( key ) ;
sb.Replace(@"HKEY_CLASSES_ROOT\","") ;

// Open HKCR\CLSID\{guid} for write access
RegistryKey k =
Registry.ClassesRoot.OpenSubKey(sb.ToString(),true );

// Delete the 'Control' key, but don't throw an exception if it
does not exist
k.DeleteSubKey ( "Control" , false ) ;

// Next open up InprocServer32
RegistryKey inprocServer32 = k.OpenSubKey ( "InprocServer32" ,
true ) ;

// And delete the CodeBase key, again not throwing if missing
k.DeleteSubKey ( "CodeBase" , false ) ;

// Finally close the main key
k.Close ( ) ;


Html file:

<html dir="ltr">
<meta http-equiv="Content-Type" content="text/html;" />
<link href="css/main.css" rel="stylesheet" type="text/css"/>
<script src="js/generic.js" type="text/javascript"
<script src="js/custom.js" type="text/javascript"

<body class="DockedModeDisplayArea" >

<!-- Border is useful for dragging around, sidebar only hit tests on
HTML objects, so we put the table as border ->
<table border="1" cellpadding="0" cellspacing="0">
<object id="sample"
width="126" style="background-color:Transparent;"
onclick="alert('clicked');" VIEWASTEXT>

<script FOR="sample" EVENT="RightClickRelay(x,y)"

function sample::RightClickRelay( x, y )
// ShowContextMenu ();

<script language="jscript.encode">




This does not work. Code is checking "RightClickRelay != null" and
returns that this event is null.

If you know how to solve this problem please let me know.

May 9 '07 #1
1 7994
Hi again,

I have finally found a solution for this problem. A simple one actually. This was just a matter of signing dll with a strong name.

Thanks anyway.

May 10 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

12 posts views Thread by A.M. | last post: by
5 posts views Thread by jen_designs | last post: by
15 posts views Thread by Carlos Lozano | last post: by
5 posts views Thread by Andrew | last post: by
3 posts views Thread by Chad Johnson | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.