3

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

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.

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>

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

Please help

4
  • Make sure you're using IE. Also check the console for errors (assume IE8+) Commented Dec 7, 2012 at 6:52
  • @AlvinWong in Console I get an error. SCRIPT429: Automation server can't create object Commented Dec 7, 2012 at 7:00
  • Try use your COM object from vb6 first (for ex. from winword/excel vba) to be sure that your COM works as expected Commented Dec 7, 2012 at 7:05
  • @SalientBrain how about VBScript Commented Dec 7, 2012 at 7:22

2 Answers 2

4

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

For more details see this forum post.

Sign up to request clarification or add additional context in comments.

1 Comment

Link is dead. In the future, please include the key information from the article in addition to the link.
2

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.