call JavaScript from C# ActiveX  
Author Message
Dirk Reske





PostPosted: Visual C# General, call JavaScript from C# ActiveX Top

Hello,

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

[GUID("...")]
[ProgId("Test.AX")]
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


Visual C#9  
 
 
Sheng Jiang





PostPosted: Visual C# General, call JavaScript from C# ActiveX Top

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",
BindingFlags.Instance|BindingFlags.InvokeMethod|BindingFlags.Public,
null,this,null);

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

script.GetType().InvokeMember("functionName",BindingFlags.Instance|BindingFlags.InvokeMethod|BindingFlags.Public,null,script,args);



 
 
Dirk R.





PostPosted: Visual C# General, call JavaScript from C# ActiveX Top

are there any good examples about creating events out there

thanks

 
 
Sheng Jiang





PostPosted: Visual C# General, call JavaScript from C# ActiveX Top

http://www.codeguru.com/csharp/csharp/cs_misc/com/article.php/c6747/