ActiveX obtains the script object

Source: Internet
Author: User
When compiling IE ActiveX, you always want to get the script object in IE to facilitate Script Processing in ActiveX.

1. A widely spread method on the Internet is to obtain the script (JavaScript) object through the top-level method, which is very good at obtaining the top-level script, however, in the IFRAME framework, Javascript in IFRAME cannot be accurately obtained. The following is an example Code : 1 Iserviceprovider * ISP, * Isp2 = NULL;
2
3 Do {
4 Hresult HR = S_ OK;
5
6 M_pclientsite -> Addref ();
7 HR = M_pclientsite -> QueryInterface (iid_iserviceprovider, reinterpret_cast < Void   **> ( & ISP ));
8 M_pclientsite -> Release ();
9
10 If (Failed (HR ))
11 {
12 Break ;
13 }
14
15 ISP -> Addref ();
16 HR = ISP -> Queryservice (sid_stoplevelbrowser, iid_iserviceprovider, reinterpret_cast < Void   **> ( & Isp2 ));
17 ISP -> Release ();
18
19 If (Failed (HR ))
20 {
21 Break ;
22 }
23
24 Isp2 -> Addref ();
25 HR = Isp2 -> Queryservice (sid_swebbrowserapp, iid_iwebbrowser2, reinterpret_cast < Void   **> ( & Pwebbrowser ));
26 Isp2 -> Release ();
27
28 ISP -> Release ();
29
30 If (Failed (HR ))
31 {
32 Break ;
33 }
34
35
36 Pwebbrowser -> Addref ();
37 HR = Pwebbrowser -> Get_document (idispatch ** ) & Phtmldocument );
38 Pwebbrowser -> Release ();
39
40 Isp2 -> Release ();
41
42 If (Failed (HR ))
43 {
44 Break ;
45 }
46
47 Phtmldocument -> Addref ();
48 HR = Phtmldocument -> Get_script ( & Pscript );
49 Phtmldocument -> Release ();
50
51 Pwebbrowser -> Release ();
52
53
54 If (Failed (HR ))
55 {
56 Break ;
57 }
58
59 } While (False ); First query the service interface, and then query the service provider of the top-level browser through the service interface. Then, the server provider queries the browser, obtains the document object from the browser, and then obtains the JavaScript Object. The method is cumbersome, but the overall meaning is clear. 2. If you want to obtain JavaScript objects in the IFRAME framework, you need to use another method. The code is very simple. The key is to be familiar with IE's COM interface. 1 Ccomptr < Iolecontainer > Spcontainer;
2 Hresult HR = M_pclientsite -> Getcontainer ( & Spcontainer );
3 If (HR = S_ OK)
4 {
5 Ccomqiptr < Ihtmldocument2 > Spdoc = Spcontainer;
6 Spdoc -> Get_script ( & Pscript );
7 Theapp. pscript = Pscript;
8 } It is easy to understand. first obtain the container object, that is, the so-called ihtmldocument2 object, and then obtain the script object from the object. Note: Is there any other way to obtain the IE Script object?

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.