ActiveX C# : acces to method of C# file

社会主义新天地 提交于 2019-12-26 03:32:51

问题


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

This is my C# program

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

My html file

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

If I execute my html file I get this error :

  Object does not support this property or method

回答1:


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

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

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



来源:https://stackoverflow.com/questions/23106745/activex-c-sharp-acces-to-method-of-c-sharp-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!