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.