VC uses com to operate IE

Source: Internet
Author: User

After reading the introduction to com over the past few days, I have figured out something. To tell the truth, Ms is really troublesome. I 've been bored with it before, but I still need to figure it out in the end. So we have the following.

I remember that I used to use VB to operate COM, which is very simple and quick. Do you still remember to control the shellwindows objects of IE? Yes, we will use VC to implement it today.

 

# Include <iostream> <br/> # import <shdocvw. DLL> <br/> # include "shlwapi. H "<br/> # include <mshtml. h> <br/> # include <string> <br/> using namespace STD; <br/> main () {<br/> coinitialize (0 ); <br/> shdocvw: ishellwindowsptr m_spshwinds; </P> <p> If (m_spshwinds = NULL) {<br/> If (m_spshwinds.createinstance (_ uuidof (shdocvw :: shellwindows ))! = S_ OK) {</P> <p> couninitialize (); </P> <p >}< br/> If (m_spshwinds) {<br/> int n = m_spshwinds-> getcount (); <br/> for (INT I = 0; I <n; I ++) {<br/> _ variant_t v = (long) I; <br/> idispatchptr spdisp = m_spshwinds-> item (v); </P> <p> shdocvw :: iwebbrowser2ptr spbrowser (spdisp); // generate a smart pointer to an IE window <br/> If (spbrowser) {<br/> _ bstr_t bsname = spbrowser-> getlocationurl (); // window name </P> <p> spbrowser-> addref (); <br/> void * pdata = spbrowser; <br/> string S = bsname; <br/> cout <S <Endl; <br/> If (S = "http://www.baidu.com /") {<br/> idispatchptr P = spbrowser-> getdocument (); <br/> ihtmldocument2ptr D; <br/> P. queryInterface (iid_ihtmldocument2, & D); <br/> ihtmlelementptr E; <br/> D-> get_body (& E); <br/> BSTR text; <br/> E-> get_innerhtml (& text); <br/> char * s = _ com_util: convertbstrtostring (text ); <br/> cout <S <Endl; <br/>}</P> <p> couninitialize (); </P> <p >}< br/>

 

 

I found that # import is a good stuff, which is very useful. Besides the complicated operation details of COM, if you want to further simplify the operation, you also need to intervene in ccomptr.

I hope you can correct the shortcomings in this 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.