vb.net 使用 Javascript ActiveX 对象触发 C# dll

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13758205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:28:36  来源:igfitidea点击:

Triggering C# dll using Javascript ActiveX Object

c#javascriptvb.netactivex

提问by Geo Paul

I have a c# class library which I need to call using Javascript. Below is the code of C# class.

我有 ac# 类库,我需要使用 Javascript 调用它。下面是 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!");
            }
    }
}

I build the class and I get a dll file which I copied to c:\DLL. Below code is used to register the DLL

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

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

I get the message types registered successfully.

我成功注册了消息类型。

I create a html file with following javascript code.

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

<!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".

在如此获得的结果页面上,我单击启动服务。但是我没有收到任何警报,例如“失败”或“Visual C# 很棒”。

Please help

请帮忙

回答by Geo Paul

I solved it. There is a Security option for activex that needs to be enabled for doing this.

我解决了。需要启用 Activex 的安全选项才能执行此操作。

For more details check this link http://www.aras.com/Community/forums/p/2527/7698.aspx

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

回答by Giulio Caccin

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.com/2010/08/creating-an-activex-control-in-net-using-c/

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