"If at first you don't succeed; call it version 1.0" :-Unknown

Pages

Sunday, April 21, 2013

call C# dll using Javascript ActiveX Object

call C# dll using Javascript ActiveX Object

I got a chance to work with siebel crm . The Version they were using is working with ActiveX Object. My objective is we had a dll which has been created in .net class library output.I need to call that library using javascript and do some functionality. Here i'm sharing how i communicate dll with ActiveX using javascript.


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


using System.Runtime.InteropServices;
using System.Windows.Forms;

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

<html>
       <head>
              <meta http-equiv="X-UA-Compatible" content="IE=edge">
              <script type='text/javascript'>
                  var myAx1;
                  function startService() {
                    //  debugger;
                      myAx1 = new ActiveXObject("CardReader");
                      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>




For security reason now IE default prevent executing AciveX without signature to override this we have to change the option.


Tool (from menu) ---> Internet option ---> Security tab---> select Trusted sites---> custom level--->  ActiveX Contol & plugin ---> 
Initialize and script ActiveX controls not marked as safe for scripting   Mark Enable
After setting  the above settings in Internet Explorer, close the Internet Explorer.



If u had any trouble just ask, Happy to help u :)
Stay Tune...
Have a nice day... 'N happy Coding :)

No comments: