I have a c# class library which I need to call using Javascript. Below is the code of C# class.
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms; //required for message box.
namespace csharp.activex.sample
{
[Guid("0D53A3E8-E51A-49C7-944E-E72A2064F938"),
InterfaceType(ComInterfaceType.InterfaceIsDual),
ComVisible(true)]
public interface IHello
{
[DispId(1)]
int ShowDialog();
};
[
Guid("873355E1-2D0D-476f-9BEF-C7E645024C32"),
ProgId("csharpAx.CHello"),
ClassInterface(ClassInterfaceType.None),
ComDefaultInterface(typeof(IHello)),
ComVisible(true)
]
public class CHello : IHello
{
#region [IHello implementation]
public string Hello()
{
return "Hello from CHello object";
}
public int ShowDialog()
{
System.Windows.Forms.MessageBox.Show("C# is awesome");
return 0;
}
#endregion
};
public class Class1
{
public void showDialog() {
MessageBox.Show("Visual c# is awesome!");
}
}
}
I build the class and I get a dll file which I copied to c:\DLL. Below code is used to register the DLL
regasm C:\DLL\ActiveXClass.dll /codebase /tlb
I get the message types registered successfully.
I create a html file with following javascript code.
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<script type='text/javascript'>
var myAx1;
function startService(){
myAx1 = new ActiveXObject("csharpAx.CHello");
if(myAx1 != null)
{
myAx1.showDialog();
}
else{
alert("failed");
}
return false;
}
</script>
</head>
<body class="sapUiBody" role="application">
<div id="content"></div>
<a href='#' onclick='return startService()'>StartService</a><br />
</body>
</html>
On the result page thus obtained I click on start service. But I do not get any alerts like "failed" or "Visual C# is awesome".
Please help
I have a c# class library which I need to call using Javascript. Below is the code of C# class.
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms; //required for message box.
namespace csharp.activex.sample
{
[Guid("0D53A3E8-E51A-49C7-944E-E72A2064F938"),
InterfaceType(ComInterfaceType.InterfaceIsDual),
ComVisible(true)]
public interface IHello
{
[DispId(1)]
int ShowDialog();
};
[
Guid("873355E1-2D0D-476f-9BEF-C7E645024C32"),
ProgId("csharpAx.CHello"),
ClassInterface(ClassInterfaceType.None),
ComDefaultInterface(typeof(IHello)),
ComVisible(true)
]
public class CHello : IHello
{
#region [IHello implementation]
public string Hello()
{
return "Hello from CHello object";
}
public int ShowDialog()
{
System.Windows.Forms.MessageBox.Show("C# is awesome");
return 0;
}
#endregion
};
public class Class1
{
public void showDialog() {
MessageBox.Show("Visual c# is awesome!");
}
}
}
I build the class and I get a dll file which I copied to c:\DLL. Below code is used to register the DLL
regasm C:\DLL\ActiveXClass.dll /codebase /tlb
I get the message types registered successfully.
I create a html file with following javascript code.
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<script type='text/javascript'>
var myAx1;
function startService(){
myAx1 = new ActiveXObject("csharpAx.CHello");
if(myAx1 != null)
{
myAx1.showDialog();
}
else{
alert("failed");
}
return false;
}
</script>
</head>
<body class="sapUiBody" role="application">
<div id="content"></div>
<a href='#' onclick='return startService()'>StartService</a><br />
</body>
</html>
On the result page thus obtained I click on start service. But I do not get any alerts like "failed" or "Visual C# is awesome".
Please help
Share Improve this question asked Dec 7, 2012 at 6:42 Geo PaulGeo Paul 1,8176 gold badges26 silver badges52 bronze badges 4- Make sure you're using IE. Also check the console for errors (assume IE8+) – Alvin Wong Commented Dec 7, 2012 at 6:52
- @AlvinWong in Console I get an error. SCRIPT429: Automation server can't create object – Geo Paul Commented Dec 7, 2012 at 7:00
- Try use your COM object from vb6 first (for ex. from winword/excel vba) to be sure that your COM works as expected – SalientBrain Commented Dec 7, 2012 at 7:05
- @SalientBrain how about VBScript – Alvin Wong Commented Dec 7, 2012 at 7:22
2 Answers
Reset to default 4I solved it. There is a Security option for activex that needs to be enabled for doing this.
For more details see this forum post.
I think the best solution is to implement IObjectSafety, if you trust you activex you don't need to make your user to check internet options. This link at point 5 explain how to do it: http://www.olavaukan./2010/08/creating-an-activex-control-in-net-using-c/
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742390483a4434914.html
评论列表(0条)