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

2
Hi,

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:
http://blogs.msdn.com/jaimer/archive/2006/10/02/writing-a-.net-activex-control-for-your-sidebar-gadget_2E002E00_.aspx
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
{

[Guid("56726927-833B-4cfb-94D3-D1501DFD30D2")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatc h)]
public interface IGadgetControlEvents
{
[DispId(1)]
void RightClickRelay(int x, int y);

}

[Guid("341F46AA-0EA0-4b97-A294-E131D8CFF7E6")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatc h)]
public interface IGadgetIncoming
{
void SetVariableName(string name, string value);
}

[Guid("A4B9C3C2-8DAC-451a-AEC1-DE3B042FB311")]
[ProgId("WindowsFormsUserControl.UserControl")]
[ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(IGadgetControlEvents))]
[ComVisible(true)]
public class Class1
{
public Class1()
{
MessageBox.Show("Calss1");

}

[ComVisible(true)]
public delegate void MouseRightClickHandler(int x, int y);
public event MouseRightClickHandler RightClickRelay;

[ComVisible(true)]
public void open()
{
MessageBox.Show("open");
if (RightClickRelay != null)
{
RightClickRelay(11, 23);
}
else
{
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>
[ComRegisterFunction()]
public static void RegisterClass ( string key )
{
// Strip off HKEY_CLASSES_ROOT\ from the passed key as I don't need
it
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
GACced.
RegistryKey inprocServer32 = k.OpenSubKey ( "InprocServer32" ,
true ) ;
inprocServer32.SetValue ( "CodeBase" ,
Assembly.GetExecutingAssembly().CodeBase ) ;
inprocServer32.Close ( ) ;
// Finally close the main key
k.Close ( ) ;
MessageBox.Show("Registered");
}

/// <summary>
/// Called to unregister the control
/// </summary>
/// <param name="key">Tke registry key</param>
[ComUnregisterFunction()]
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 ( ) ;
MessageBox.Show("UnRegistered");
}

}
}

Html file:

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

<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">
<tr>
<td>
<object id="sample"
classid="clsid:A4B9C3C2-8DAC-451a-AEC1-DE3B042FB311"
height="126"
width="126" style="background-color:Transparent;"
onclick="alert('clicked');" VIEWASTEXT>
</object>

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

function sample::RightClickRelay( x, y )
{
// ShowContextMenu ();
alert(1);
}
</script>

<script language="jscript.encode">

document.sample.open();


</script>
</td>
</tr>
</table>


</body>
</html>


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.

Cheers,
Kret
May 9 '07 #1
1 7994
kret
2
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.

Cheers,
Michał
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.