call JavaScript from C# ActiveX


  • Hello,

    I've created a C# activex control....

    public class Test
        string Test()
           return "from com component";

    now when I instantiate this component in js
    comp = new activeXObject("Test.AX");

    I can call alert(comp.Test()); and a messagebox with the text from the c# dll pops up.

    How can I call functions in the other way -> call a function in the javascript file from the dll?

    thanks and greets from germany
    Saturday, December 02, 2006 3:46 PM

All replies

  • Scipts tends to be changed more often than ActiveX. Better if you change your design. Declare some events and handle them in scripts, and in turn the event handlers call script functions.

    If you really want to call scripts from your ActiveX hosted by Internet Explorer, you need to first access the container document

    Type typeIOleObject = this.GetType().GetInterface("IOleObject",true);
    object oleClientSite = typeIOleObject.InvokeMember("GetClientSite",

    IOleClientSite oleClientSite2 = oleClientSite as IOleClientSite;
    IOleContainer pOleContainer;
    oleClientSite2.GetContainer(out pOleContainer);

    and then access its script property
    IHTMLDocument pDoc1 = (IHTMLDocument)pOleContainer;
    object script=pDoc1.Script;

    If you know the signature of the function, you can call the function with zero or more arguments

    object [] args;

    //set up the args array with arguments


    Saturday, December 02, 2006 6:45 PM
  • are there any good examples about creating events out there?

    Monday, December 04, 2006 11:34 AM
  • Hi,

    I am also trying to call javascript functions from activex dll in C#. but I am not able to.

    Can you please attach the example or point me to the example page.

    Thanks  in advance.
    Sunday, January 11, 2009 7:43 AM
  • Hi,
    I am also trying to build an ActiveX and have it call a javascript function to fire an event.
    I used the code as suggested, but I get an exception at:
    Type typeIOleObject = this.GetType().GetInterface("IOleObject",true);

    The exception is 'Object instance not set to instance of an object'.
    GetInterface returns a null.

    I am not sure if I have missed anything in implementation or does the javascript needs to have the functions declared in certain way.
    Any help is greatly appreciated.

    Tuesday, December 08, 2009 1:59 AM
  • Hi,

    I am dealing in a same way in current project and calls Javascript method from .net ActiveX. I have a question how to deal with JavaScript methods that returns value. Is there a way javascipt can send return value to the .net code?

    Any help will be much appreciated?



    Shashi Saini

    Thursday, April 11, 2013 8:01 PM
  • object oleClientSite = typeIOleObject.InvokeMember("GetClientSite", 

    Following section is giving null reference exception and not able to call javascript from activex 

    Monday, December 23, 2013 11:49 AM