ActiveX (3) ActiveX calls Js and activex calls js

Source: Internet
Author: User

ActiveX (3) ActiveX calls Js and activex calls js

In the previous article, ActiveX (2) Js listens to events in ActiveX, and Js listens to events in ActiveX. Now, Js and ActiveX can implement bidirectional communication. However, this implementation method is implemented from the Js perspective. Can ActiveX actively call Js? The answer is no doubt. In this article, we will gradually unveil this layer of mystery.

 

My first contact with C # code called Js was four years ago. At that time, I was working on a Windows application and needed to use the WebBrowser control to operate js and complete some special requirements. The code at that time was roughly as follows:

// Execute JS private void ExecJs () {try {if (this. webBrowser. Document! = Null) {mshtml. IHTMLDocument2 currentDoc = (mshtml. IHTMLDocument2) this. webBrowser. Document. DomDocument; if (currentDoc! = Null) {mshtml. IHTMLWindow2 win = (mshtml. IHTMLWindow2) currentDoc. parentWindow; if (win! = Null) {// call function F and pass a width as the parameter win.exe cScript (string. format ("F ('{0}')", this. webBrowser. width), "javascript") ;}}} catch (Exception ex) {Console. writeLine (ex. message );}}

 

From the above code, we can see that the core method is mshtml.IHTMLWindow2.exe cScript. Note: Let's take a closer look at the method name. Are you familiar with it? If you haven't thought about it yet, let's take a look at the members of the mshtml. IHTMLWindow2 type.

    [Guid("332C4427-26CB-11D0-B483-00C04FD90119")]    [TypeLibType(4160)]    public interface IHTMLWindow2 : IHTMLFramesCollection2    {        [DispId(1153)]        dynamic _newEnum { get; }        [DispId(1161)]        HTMLNavigator clientInformation { get; }        [DispId(23)]        bool closed { get; }        [DispId(1101)]        string defaultStatus { get; set; }        [DispId(1151)]        IHTMLDocument2 document { get; }        [DispId(1152)]        IHTMLEventObj @event { get; }        [DispId(1169)]        dynamic external { get; }        [DispId(1100)]        FramesCollection frames { get; }        [DispId(2)]        HTMLHistory history { get; }        [DispId(1125)]        HTMLImageElementFactory Image { get; }        [DispId(1001)]        int length { get; }        [DispId(14)]        HTMLLocation location { get; }        [DispId(11)]        string name { get; set; }        [DispId(5)]        HTMLNavigator navigator { get; }        [DispId(1164)]        dynamic offscreenBuffering { get; set; }        [DispId(-2147412073)]        dynamic onbeforeunload { get; set; }        [DispId(-2147412097)]        dynamic onblur { get; set; }        [DispId(-2147412083)]        dynamic onerror { get; set; }        [DispId(-2147412098)]        dynamic onfocus { get; set; }        [DispId(-2147412099)]        dynamic onhelp { get; set; }        [DispId(-2147412080)]        dynamic onload { get; set; }        [DispId(-2147412076)]        dynamic onresize { get; set; }        [DispId(-2147412081)]        dynamic onscroll { get; set; }        [DispId(-2147412079)]        dynamic onunload { get; set; }        [DispId(4)]        dynamic opener { get; set; }        [DispId(1157)]        HTMLOptionElementFactory Option { get; }        [DispId(12)]        IHTMLWindow2 parent { get; }        [DispId(1156)]        IHTMLScreen screen { get; }        [DispId(20)]        IHTMLWindow2 self { get; }        [DispId(1102)]        string status { get; set; }        [DispId(21)]        IHTMLWindow2 top { get; }        [DispId(22)]        IHTMLWindow2 window { get; }        [DispId(1105)]        void alert(string message = "");        [DispId(1159)]        void blur();        [DispId(1163)]        void clearInterval(int timerID);        [DispId(1104)]        void clearTimeout(int timerID);        [DispId(3)]        void close();        [DispId(1110)]        bool confirm(string message = "");        [DispId(1165)]        dynamic execScript(string code, string language = "JScript");        [DispId(1158)]        void focus();        [DispId(0)]        dynamic item(ref object pvarIndex);        [DispId(7)]        void moveBy(int x, int y);        [DispId(6)]        void moveTo(int x, int y);        [DispId(25)]        void navigate(string url);        [DispId(13)]        IHTMLWindow2 open(string url = "", string name = "", string features = "", bool replace = false);        [DispId(1111)]        dynamic prompt(string message = "", string defstr = "undefined");        [DispId(8)]        void resizeBy(int x, int y);        [DispId(9)]        void resizeTo(int x, int y);        [DispId(1160)]        void scroll(int x, int y);        [DispId(1167)]        void scrollBy(int x, int y);        [DispId(1168)]        void scrollTo(int x, int y);        [DispId(1173)]        int setInterval(string expression, int msec, ref object language = Type.Missing);        [DispId(1172)]        int setTimeout(string expression, int msec, ref object language = Type.Missing);        [DispId(1155)]        void showHelp(string helpURL, object helpArg = Type.Missing, string features = "");        [DispId(1154)]        dynamic showModalDialog(string dialog, ref object varArgIn = Type.Missing, ref object varOptions = Type.Missing);        [DispId(1166)]        string toString();    }

 

Remember? If not, you can only say that you have not written a line of Js Code. That's right. The window object in js implements the mshtml. IHTMLWindow2 interface. In this case, we can pass in the window object through an initialization function to obtain the mshtml. IHTMLWindow2 interface.

 

Haha, add that the mshtml namespace comes from Microsoft. mshtml. dll.

 

When the above information is known, everything becomes 'simple:

/// <Summary> /// call the Js function /// </summary> /// <param name = "functionName"> js function name </param> /// <param name = "args"> parameter </param> // <returns> </returns> private dynamic RunJs (string functionName, string args = "") {dynamic dy = null; // the return value of the method is always null if (this. window2! = Null) {// call the function dy = this.window2.exe cScript (string. format ("{0} ('{1}')", functionName, args), "javascript") ;}return dy ;}

Through the above Code, JavaScript can be called, but there is a Bug... the return value is always Null.

What should we do? Reflection-Yes, it's just reflection calling Js. Maybe you still don't understand it. Let's take a look at the code first. After reading it, you may suddenly realize:

 

/// <Summary> /// call the Js function /// </summary> /// <param name = "functionName"> js function name </param> /// <param name = "args"> parameter </param> // <returns> </returns> private dynamic RunJs (string functionName, string args = "") {dynamic dy = null; // The second method reflection call Js // The called js function must have a parameter if (this. window2! = Null) {dy = this. window2.GetType (). invokeMember (functionName, BindingFlags. instance | BindingFlags. invokeMethod | BindingFlags. public, null, this. window2, new object [] {args});} return dy ;}

 

Did you think of it? The Global Js function actually belongs to the window object. Of course, it can be called Using Reflection.

 

 

Complete Test Project demo: TestActiveX.zip

 

Next, we will explore the powerful Microsoft. mshtml. dll.

(To be continued ...)

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.