使用Javascript ActiveX对象触发C#dll

我有一个需要用Javascript调用的#class库。 下面是C#类的代码。

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!"); } } } 

我构建了类,我得到一个dll文件,我将其复制到c:\ DLL。 下面的代码用于注册DLL

 regasm C:\DLL\ActiveXClass.dll /codebase /tlb 

我成功注册了消息类型。

我用以下javascript代码创建一个html文件。

      var myAx1; function startService(){ myAx1 = new ActiveXObject("csharpAx.CHello"); if(myAx1 != null) { myAx1.showDialog(); } else{ alert("failed"); } return false; }    
StartService

在这样获得的结果页面上,我点击开始服务。 但我没有得到任何警告,如“失败”或“Visual C#很棒”。

请帮忙

我解决了 为执行此操作,需要启用activex的安全选项。

有关详细信息,请访问此链接http://www.aras.com/Community/forums/p/2527/7698.aspx

我认为最好的解决方案是实现IObjectSafety,如果您信任activex,则不需要让用户检查互联网选项。 第5点的这个链接解释了如何做到这一点: http : //www.olavaukan.com/2010/08/creating-an-activex-control-in-net-using-c/