ActiveX C# : acces to method of C# file

2019-09-21 00:23发布

问题:

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



标签: c# .net activex