触发使用Javascript ActiveX对象C#DLL [英] Triggering C# dll using Javascript ActiveX Object

查看:152
本文介绍了触发使用Javascript ActiveX对象C#DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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



我建类和我让我复制到c一个dll文件:\DLL。下面的代码是用来注册DLL

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.

我。创建具有以下JavaScript代码的HTML文件

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>

在结果页这样得到我点击启动服务。但我没有得到任何警告,如失败或的Visual C#是真棒。

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

请帮忙

推荐答案

我解决它。还有的ActiveX需要这样做将启用安全选项。

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

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

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

这篇关于触发使用Javascript ActiveX对象C#DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆