ActiveX C#:访问C#文件的方法 [英] ActiveX C# : acces to method of C# file

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

问题描述

我编写了一个简单的Activex(只是显示警报Hello World),但是当我在JavaScript函数中调用ActiveX时,我无法访问C#程序的HelloWorld方法。

I write a simple Activex (just show alert Hello World) but I can't acces to my method HelloWorld of my C# program when I call ActiveX in a my JavaScript function

这是我的C#程序

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace DemoCSharpActiveX
{
    [ProgId("DemoCSharpActiveX.HelloWorld")]
    [ClassInterface(ClassInterfaceType.AutoDual)]
    [Guid("1c61c720-ce70-40e5-9e88-714469911fb3")]
    [ComVisible(true)]
    public class HelloWorld
    {
        [ComVisible(true)]
        public String SayHello()
        {
            return "Hello World!";
        }
    }
}

我的html文件

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > 

<html>
  <head>
    <title>WebForm1</title>
  </head>
   <body>
       <OBJECT id="DemoCSharpActiveX" classid="clsid:1c61c720-ce70-40e5-9e88-714469911fb3" VIEWASTEXT></OBJECT>  

        <script type="text/javascript">
            try {
                var obj = document.DemoCSharpActiveX;
                if (obj) {
                    alert(obj.SayHello());
                } else {
                    alert("Object is not created!");
                }
            } catch (Err) {
                alert(Err.description);
            }

        </script>
   </body>
</html>

如果执行我的html文件,则会出现此错误:

If I execute my html file I get this error :

  Object does not support this property or method


推荐答案

您确定您的ActiveX已在系统中正确注册并且已正确初始化吗?

Are you sure that your ActiveX is properly registered in system and that it is properly initialized?

您的activeX不会实现 IObjectSafety 接口,因此IE不会正常运行它。检查演示页面进入的安全区域,并设置此设置

Your activeX does not implement IObjectSafety interface so IE will not run it normaly. Check what security zone does your demo page land in and set this setting

此外,您可能希望查看此示例,如果您要从CAB安装ActiveX,也请这个问题

Also, you might want to look at this example and if you want install ActiveX from CAB then also this SO question

这篇关于ActiveX C#:访问C#文件的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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