CREATING ACTIVE X OBJECTS WITH C# / CALLING C# DLL FROM JAVA SCRIPT
CALLING C# DLL FROM JAVA SCRIPT
Finally I did it ... It was a complete night mare for me and my team ..
k lemme stop about my night mare. Its very quite simple..
Creating Activex objects with C#
Here i will discuss how to create a com object using c# and how to use it in the java script. calling C# dll from java script means creating a Activex object for the com object created from our dll.
ActiveX is a Microsoft technology used for creating
re-usable components. This technology, also called Object Linking and Embedding
(OLE), along with COM is heavily used within the Microsoft environment and
application development.
re-usable components. This technology, also called Object Linking and Embedding
(OLE), along with COM is heavily used within the Microsoft environment and
application development.
For further details go through this link http://en.wikipedia.org/wiki/ActiveX
Anyone interested in creating reusable components should have some familiarity with
COM. A very good resource is “Essential COM” by Don Box.
The idea behind COM is to allow the creation of components
that can be consumed by a variety of clients. This means that I can create a
component using C++ which can then be used by an application written in VB, or
even a web page which we will be performing shortly. Traditionally, ActiveX
objects were usually written in C++ and VB but can actually be written in any
language as long as they are COM-aware.
This means that we can create COM objects with the .NET
platform. A survey of resources/tutorials on COM and .NET on the internet by and
large only tell you how to consume unmanaged COM objects via Interop. So my aim
with this guide is to show how to use this powerful feature. Also, FYI, there is
a .NET technology very similar to what will be describe below which allows
hosting Windows Forms Controls inside of IE (don’t know the official term/name
for this).
COM. A very good resource is “Essential COM” by Don Box.
The idea behind COM is to allow the creation of components
that can be consumed by a variety of clients. This means that I can create a
component using C++ which can then be used by an application written in VB, or
even a web page which we will be performing shortly. Traditionally, ActiveX
objects were usually written in C++ and VB but can actually be written in any
language as long as they are COM-aware.
This means that we can create COM objects with the .NET
platform. A survey of resources/tutorials on COM and .NET on the internet by and
large only tell you how to consume unmanaged COM objects via Interop. So my aim
with this guide is to show how to use this powerful feature. Also, FYI, there is
a .NET technology very similar to what will be describe below which allows
hosting Windows Forms Controls inside of IE (don’t know the official term/name
for this).
Thats enough blaaa blaaa now.... We will go through an example....
Step1: Creating the C# DLL WHICH WILL BE CALLED BY JAVASCRIPT
using System; using System; using System.Runtime.InteropServices; using System.ComponentModel; using System.Collections.Generic; using System.Text; namespace csharp.activex.sample { /// /// A very simple interface to test ActiveX with. /// [ Guid( "E86A9038-368D-4e8f-B389-FDEF38935B2F"), InterfaceType( ComInterfaceType.InterfaceIsDual), ComVisible( true) ] public interface IHello { [DispId(1)] string Hello(); [DispId(2)] int SayHello(string msg); }; [ Guid("873355E1-2D0D-476f-9BEF-C7E645024C32"), // This is basically the programmer friendly name // for the guid above. We define this because it will // be used to instantiate this class. I think this can be // whatever you want. Generally it is // [assemblyname].[classname] ProgId("csharpAx.CHello"), // No class interface is generated for this class and // no interface is marked as the default. // Users are expected to expose functionality through // interfaces that will be explicitly exposed by the object // This means the object can only expose interfaces we define ClassInterface(ClassInterfaceType.None), // Set the default COM interface that will be used for // Automation. Languages like: C#, C++ and VB // allow to query for interface's we're interested in // but Automation only aware languages like javascript do // not allow to query interface(s) and create only the // default one ComDefaultInterface(typeof(IHello)), ComVisible(true) ] public class CHello : IHello { public string function1() { return "Hello from CHello object"; } public int SayHello(string msg) { return 1; } }; } |
The HelloControl.cs file consists of one class and one interface.
So let’s start with the interface definition and follow up with the CHello class.
**IHello Interface**
An interface is generally described as a contract. Once an interface is designed
and published for use it should NEVER change. Let me repeat this, it
should NEVER change.
Several attributes have been defined for this interface.
1) Guid – This is a unique identifier. Use guidgen.exe to generate your own
2) InterfaceType – This determines which base class to expose to COM.
The InterfaceType attribute demands a little further explaination. Here is how it is defined.
So let’s start with the interface definition and follow up with the CHello class.
**IHello Interface**
An interface is generally described as a contract. Once an interface is designed
and published for use it should NEVER change. Let me repeat this, it
should NEVER change.
Several attributes have been defined for this interface.
1) Guid – This is a unique identifier. Use guidgen.exe to generate your own
2) InterfaceType – This determines which base class to expose to COM.
The InterfaceType attribute demands a little further explaination. Here is how it is defined.
Compiling & Registering
----------------------------
Once the code is written up go ahead an compile the ActiveX assembly.
The assembly will be called [projectname].dll inside the bin folder of the project.
The next step is to register. Do this opening up a command prompt and go to the
location of the dll. To register the assembly type:
Open the visual studio command prompt.
C:\Regasm C:\Build\[projectname].dll /codebase /tlb
Make sure replace “[projectname]” with the correct name of your dll.
You should get some output that says the assembly was exported correctly.
Something like the following.
Microsoft ® .NET Framework Assembly Registration Utility 2.0.50727.42
Copyright © Microsoft Corporation 1998-2004. All rights reserved.
Types registered successfully
Assembly exported to '[TLB location]
', and the type library was registered successfullyAfter this a tlb file will created in the path specified above( i mean in the path of the dll)
To unregister the assembly pass the “/unregister” flag to Regasm. You will need to
unregister and close IE when changes need to be made to the assembly. If you don’t
unregister your assembly prior to making code changes you may end up with bogus
registry entries and a pain to clean up. The reason for closing IE is to make sure
that all reference to your assembly have been released.
----------------------------
Once the code is written up go ahead an compile the ActiveX assembly.
The assembly will be called [projectname].dll inside the bin folder of the project.
The next step is to register. Do this opening up a command prompt and go to the
location of the dll. To register the assembly type:
Open the visual studio command prompt.
C:\Regasm C:\Build\[projectname].dll /codebase /tlb
Make sure replace “[projectname]” with the correct name of your dll.
You should get some output that says the assembly was exported correctly.
Something like the following.
Microsoft ® .NET Framework Assembly Registration Utility 2.0.50727.42
Copyright © Microsoft Corporation 1998-2004. All rights reserved.
Types registered successfully
Assembly exported to '[TLB location]
', and the type library was registered successfullyAfter this a tlb file will created in the path specified above( i mean in the path of the dll)
To unregister the assembly pass the “/unregister” flag to Regasm. You will need to
unregister and close IE when changes need to be made to the assembly. If you don’t
unregister your assembly prior to making code changes you may end up with bogus
registry entries and a pain to clean up. The reason for closing IE is to make sure
that all reference to your assembly have been released.
Creating HTML Test Page
-----------------------------
We are just about done. All we need to do now is create a test page.
This should be really simple, but here it is nonetheless. The Javascriptto create the ActiveX object is also provided.
Here i am writing down a java script example. here i wrote a function in java script where it calls the dll of c#.
function myload()
{
var myAx = new ActiveXObject("DllName");(just a dll name .dll extension should not be given)
if(myAx != null)
{
var d = myAx.fucnction1();
alert(d);
}
else
alert("NOOOO... we failed");
}
To create our ActiveX object in Javascript we use the ActiveXObject(). The parameter
-----------------------------
We are just about done. All we need to do now is create a test page.
This should be really simple, but here it is nonetheless. The Javascriptto create the ActiveX object is also provided.
Here i am writing down a java script example. here i wrote a function in java script where it calls the dll of c#.
function myload()
{
var myAx = new ActiveXObject("DllName");(just a dll name .dll extension should not be given)
if(myAx != null)
{
var d = myAx.fucnction1();
alert(d);
}
else
alert("NOOOO... we failed");
}
To create our ActiveX object in Javascript we use the ActiveXObject(). The parameter
for this is generally the PROGID that we specified in its definition. If a
PROGID was not specified in its definition then I think you can pass in
“AssemblyName.ClassName”. The rest of the javascript simply calls our ActiveX
objects methods.
That’s it!!! We should have a fully functional example. This ability to execute ActiveX
objects from javascript is very powerful. Debates and arguments about security
and safety of ActiveX objects are well known and I will not throw more fodder
to the fire. However, imagine creating a Win32 or even a Winform application
where the UI is written in HTML via IWebBrowser2 and all the UI interaction handled
with your ActiveX object, just food for thought. Though, now that I think of it,
WPF is already doing this. Oh Well.
Hopefully you found this guide on how to create an ActiveX object with C# helpful.
Cheers for going through the same...
Sandeep....
