This code snippets helps you to create a c# com and call fuction in c# com from javascript code .Add a reference runtime.InteropServices.Note that this code only works with internet explorer.
C# COM :
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace TestCOM
{
[Guid("F7B3192E-8B84-4cf0-9162-95166783299A")]
public interface COM_Interface
{
int disp(int aa);
}
[Guid("879A7994-1FE9-4379-8F84-C135B59B5C4B"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface COM_Events
{
}
[Guid("E723167E-2244-4c9d-9681-F2E2108E49D3"),
ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(COM_Events))]
public class COM_Class : COM_Interface
{
public int disp(int aa)
{
return aa + 2;
}
}
}
Java script code :
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> CSHARP COM TEST </TITLE>
<script type='text/javascript' language='javascript'>
function comFunction()
{
try
{
var myobject;
myobject = new ActiveXObject("TestCOM.COM_Class");
alert(myobject.disp(2)); // Call DLL function here.
}
catch(e)
{
alert(e.message);
}
}
</SCRIPT></HEAD>
<BODY>
<INPUT TYPE=button value=DLL onClick="comFunction()">
<br>
</BODY>
</HTML>
C# COM :
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace TestCOM
{
[Guid("F7B3192E-8B84-4cf0-9162-95166783299A")]
public interface COM_Interface
{
int disp(int aa);
}
[Guid("879A7994-1FE9-4379-8F84-C135B59B5C4B"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface COM_Events
{
}
[Guid("E723167E-2244-4c9d-9681-F2E2108E49D3"),
ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(COM_Events))]
public class COM_Class : COM_Interface
{
public int disp(int aa)
{
return aa + 2;
}
}
}
Java script code :
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> CSHARP COM TEST </TITLE>
<script type='text/javascript' language='javascript'>
function comFunction()
{
try
{
var myobject;
myobject = new ActiveXObject("TestCOM.COM_Class");
alert(myobject.disp(2)); // Call DLL function here.
}
catch(e)
{
alert(e.message);
}
}
</SCRIPT></HEAD>
<BODY>
<INPUT TYPE=button value=DLL onClick="comFunction()">
<br>
</BODY>
</HTML>
No comments:
Post a Comment