Traverse ie Elements

Source: Internet
Author: User
  1. # Include <atlbase. h> // ATL
  2. # Include <atlcom. h>
  3. # Include <mshtml. h> // All ihtmlxxx excuses
  4. # Include <iostream>
  5. # Include <exdisp. h>
  6. Using namespace STD;
  7. Ccommodule _ module; // to use the smart pointer of ccomdispatchdriver ATL, it must be declared
  8. Void enumie (); // enumerate browser Functions
  9. Void enumframe (ihtmldocument2 * pdoc); // Enumerate sub-FRAMEWORK Functions
  10. Void enumform (ihtmldocument2 * pdoc); // enumerated form function
  11. Int main ()
  12. {
  13. : Coinitialize (null );
  14. : Enumie ();
  15. : Couninitialize ();
  16. Return 0;
  17. }
  18. Void enumie ()
  19. {
  20. Ccomptr <ishellwindows> spshellwin;
  21. Hresult hR = s_false;
  22. Long ncount = 0;
  23. Long I = 0;
  24. Cout <"start to traverse ie instances running in the system" <Endl;
  25. HR = spshellwin. cocreateinstance (clsid_shellwindows );
  26. If (failed (HR ))
  27. {
  28. Cout <"An error occurred while obtaining the ishellwindows interface! "<Endl;
  29. Return;
  30. }
  31. Spshellwin-> get_count (& ncount );
  32. If (ncount <= 0)
  33. {
  34. Cout <"no running browser! "<Endl;
  35. Return;
  36. }
  37. For (I = 0; I <ncount; I ++)
  38. {
  39. Ccomptr <idispatch> Spie;
  40. HR = spshellwin-> item (ccomvariant (long) I), & Spie );
  41. If (failed (HR ))
  42. {
  43. Continue;
  44. }
  45. Ccomqiptr <iwebbrowser2> spbrowser = Spie;
  46. If (! Spbrowser)
  47. {
  48. Continue;
  49. }
  50. Ccomptr <idispatch> spdisp;
  51. HR = spbrowser-> get_document (& spdisp );
  52. If (failed (HR ))
  53. {
  54. Continue;
  55. }
  56. Ccomqiptr <ihtmldocument2> spdoc = spdisp;
  57. If (! Spdoc)
  58. {
  59. Continue;
  60. }
  61. Spdoc-> put_bgcolor (ccomvariant ("blue "));
  62. : Enumform (spdoc );
  63. }
  64. }
  65. Void enumframe (ihtmldocument2 * pdoc)
  66. {
  67. Hresult hR = s_false;
  68. Long ncount = 0;
  69. Long I = 0;
  70. If (! Pdoc)
  71. {
  72. Return;
  73. }
  74. Ccomptr <ihtmlframescollection2> spcollfrm;
  75. Pdoc-> get_frames (& spcollfrm );
  76. HR = spcollfrm-> get_length (& ncount );
  77. If (failed (HR) | (ncount <= 0 ))
  78. {
  79. Return;
  80. }
  81. For (I = 0; I <ncount; I ++)
  82. {
  83. Ccomvariant VT;
  84. HR = spcollfrm-> item (& ccomvariant (I), & VT );
  85. If (failed (HR ))
  86. {
  87. Continue;
  88. }
  89. Ccomqiptr <ihtmlwindow2> spwin2 = vt. pdispval;
  90. If (! Spwin2)
  91. {
  92. Continue;
  93. }
  94. Ccomptr <ihtmldocument2> spdoc2;
  95. Spwin2-> get_document (& spdoc2 );
  96. : Enumform (spdoc2 );
  97. }
  98. }
  99. Void enumform (ihtmldocument2 * pdoc)
  100. {
  101. Hresult hR = s_false;
  102. Ccombstr bstrtitle;
  103. Long ncount = 0;
  104. Long I = 0;
  105. If (! Pdoc)
  106. {
  107. Return;
  108. }
  109. Pdoc-> get_title (& bstrtitle );
  110. Uses_conversion;
  111. Cout <"Start enumeration" <ole2ct (bstrtitle) <"form" <Endl;
  112. Ccomqiptr <ihtmlelementcollection> spcollelm;
  113. HR = pdoc-> get_forms (& spcollelm );
  114. If (failed (HR ))
  115. {
  116. Cout <"An error occurred while obtaining the form set! "<Endl;
  117. Return;
  118. }
  119. HR = spcollelm-> get_length (& ncount );
  120. If (failed (HR ))
  121. {
  122. Cout <"incorrect form input! "<Endl;
  123. Return;
  124. }
  125. For (I = 0; I <ncount; I ++)
  126. {
  127. Idispatch * Pdisp = NULL;
  128. HR = spcollelm-> item (ccomvariant (I), ccomvariant (), & Pdisp );
  129. If (failed (HR ))
  130. {
  131. Continue;
  132. }
  133. Ccomqiptr <ihtmlformelement> spele = Pdisp;
  134. Pdisp-> release ();
  135. Long nelemcount = 0;
  136. Long J = 0;
  137. HR = spele-> get_length (& nelemcount );
  138. If (failed (HR ))
  139. {
  140. Continue;
  141. }
  142. For (j = 0; j <nelemcount; j ++)
  143. {
  144. Ccomdispatchdriver spinputelem;
  145. HR = spele-> item (ccomvariant (J), ccomvariant (), & spinputelem );
  146. If (failed (HR ))
  147. {
  148. Continue;
  149. }
  150. Ccomvariant vname, vval, vtype;
  151. HR = spinputelem. getpropertybyname (L "name", & vname );
  152. If (failed (HR ))
  153. {
  154. Continue;
  155. }
  156. HR = spinputelem. getpropertybyname (L "value", & vval );
  157. If (failed (HR ))
  158. {
  159. Continue;
  160. }
  161. HR = spinputelem. getpropertybyname (L "type", & vtype );
  162. If (failed (HR ))
  163. {
  164. Continue;
  165. }
  166. Lpctstr lpname = vname. bstrval? Ole2ct (vname. bstrval): "null ";
  167. Lpctstr lpval = vval. bstrval? Ole2ct (vval. bstrval): "null ";
  168. Lpctstr lptype = vval. bstrval? Ole2ct (vtype. bstrval): "null ";
  169. Cout <"[" <lptype <"]" <lpname <"=" <lpval <Endl;
  170. }
  171. }
  172. }

 

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.