In C++, a connection can be accomplished in roughly the same way. Visual C++ Native Com Support is used here for the sake of brevity. Add references to Shdocvw.dll and Mshtml.dll to the project: #import <mshtml.dll> // Internet Explorer 4.0x #import <mshtml.tlb> // Internet Explorer 5
#import <mshtml.tlb> // Internet Explorer 5 这一段我早就加进去了IXMLDOMElement可不是这里面定义的哦(我导入了这个tlb文件仔细看过)。BEGIN_EVENTSINK_MAP(CHtmlView, CFormView) ON_EVENT(CHtmlView, AFX_IDW_PANE_FIRST, 259 /* DocumentComplete */, DocumentComplete, VTS_DISPATCH VTS_PVARIANT)END_EVENTSINK_MAP()我把这一段抄进去,并加入了DocumentComplete函数,编译说未定义_GetSinkmap
我包含了 #include "mshtml.h" #import "C:\winnt\system32\mshtml.tlb" // location of mshtml.tlb 编译时这一句 // Get the top-level window spDisp = spDoc->Script; 说 error C2039: 'Script' : is not a member of 'IHTMLDocument2' 怎么回事啊?
我包含了 #include "mshtml.h" #import "C:\winnt\system32\mshtml.tlb" // location of mshtml.tlb 编译时这一句 // Get the top-level window spDisp = spDoc->Script; 说 error C2039: 'Script' : is not a member of 'IHTMLDocument2' 怎么回事啊?
IHTMLDocument::Script PropertyRetrieves an interface pointer that provides access to the scripting object via a handle to the script engine. SyntaxHRESULT IHTMLDocument::get_Script(IDispatch **p); spDoc是IHTMLDocument2Ptr吗?
能把工程发给我看看吗?谢谢了,解决了一定加分我这里问题很奇怪,spDoc后面打->后List Member里面有Scripts,编译却说error C2039: 'Script' : is not a member of 'IHTMLDocument2'IHTMLDocument2Ptr spDoc;照你的代码抄的,不会有错啊。还有IXMLDOMElement我也不知道怎么定义。
修改MFCIE示例的mfcieVw.cpp #include "stdafx.h" #include "mfcie.h" #include "MainFrm.h"#include "mfcieDoc.h" #include "mfcieVw.h"#pragma warning(disable : 4192) #pragma warning(disable : 4049) #pragma warning(disable : 4146) #import <mshtml.tlb> #import <msxml4.dll> #pragma warning(default: 4192) #pragma warning(default: 4049) #pragma warning(default: 4146) // Include Microsoft HIML definitions...#include <dispex.h>//You may derive a class from CComModule and use it if you want to override //something, but do not change the name of _Module #include <atlbase.h>//for atlcom.h extern CComModule _Module;//for atlbase.h #include <atlcom.h>//for OLE2T…… END_MESSAGE_MAP()BEGIN_EVENTSINK_MAP(CMfcieView, CHtmlView) ON_EVENT(CMfcieView, AFX_IDW_PANE_FIRST, 259 /* DocumentComplete */, DocumentComplete, VTS_DISPATCH VTS_PVARIANT) END_EVENTSINK_MAP()///////////////////////////////////////////////////////////////////////////// // CMfcieView construction/destruction…… //END OF MFC MEMORY LEAK BUG FIXvoid CMfcieView::DocumentComplete(LPDISPATCH pDisp, VARIANT* URL) { ASSERT(V_VT(URL) == VT_BSTR); IDispatchPtr spDisp; HRESULT hr; hr = m_pBrowserApp->QueryInterface(IID_IDispatch, (void**) &spDisp); // Is the IDispatch* passed to us for the top-level window ? if (pDisp == spDisp) { MSHTML::IHTMLDocument2Ptr spDoc; // Get the active document spDoc = GetHtmlDocument(); if ( spDoc ) { MSHTML::IHTMLWindow2Ptr spWin; // Get the top-level window spDisp = spDoc->Script; spWin = spDisp; if ( spWin ) { // Get the document spDoc = spWin->document; if ( spDoc ) { IDispatchExPtr spDispEx; // Get the document's IDispatchEx spDoc->QueryInterface( IID_IDispatchEx, (void**)&spDispEx ); if ( spDispEx ) { _bstr_t bstrName("XMLDocument"); DISPID dispid; // Get the XMLDocument expando property spDispEx->GetDispID( bstrName, fdexNameCaseSensitive, &dispid ); if ( SUCCEEDED(hr) && dispid != DISPID_UNKNOWN ) { VARIANT var; DISPPARAMS dpNoArgs = {NULL, NULL, 0, 0}; // Get the XMLDocument value hr = spDispEx->Invoke( dispid, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, &dpNoArgs, &var, NULL, NULL ); if ( SUCCEEDED(hr) && var.vt == VT_DISPATCH ) { MSXML2::IXMLDOMDocument* pXMLDoc=NULL; // Get the IXMLDOMDocument interface var.pdispVal->QueryInterface( IID_IXMLDOMDocument, (void**)&pXMLDoc ); VariantClear( &var ); if ( pXMLDoc ) { // Get the root element MSXML2::IXMLDOMElement* pXMLElem=NULL; pXMLDoc->get_documentElement( &pXMLElem ); if ( pXMLElem ) { BSTR bstr; USES_CONVERSION; // Get/display the tag name pXMLElem->get_tagName( &bstr ); AfxMessageBox( OLE2T(bstr) ); pXMLElem->Release(); } pXMLDoc->Release(); } } } } } } } } } //end new code
IDispatch* pDisp = NULL;
IHTMLDocument2* pDoc = NULL;
pDisp = GetHtmlDocument(); if(SUCCEEDED(hr = pDisp->QueryInterface(IID_IHTMLDocument2,(void**)&pDoc)))
{
IPersistFile* pFile = NULL;
if(SUCCEEDED(pDoc->QueryInterface(IID_IPersistFile,(void**)&pFile)))
{
LPCOLESTR file = L"c:\\test1.htm";
pFile->Save(file,TRUE);
}
}
{
IDispatchPtr spDisp;
HRESULT hr; hr = m_pBrowserApp->QueryInterface(IID_IDispatch, (void**) &spDisp);
// Is the IDispatch* passed to us for the top-level window ?
if (pDisp == spDisp)
{
IHTMLDocument2Ptr spDoc; // Get the active document
spDoc = GetHtmlDocument();
if ( spDoc )
{
IHTMLWindow2Ptr spWin; // Get the top-level window
spDisp = spDoc->Script;
spWin = spDisp;
if ( spWin )
{
// Get the document
spDoc = spWin->document;
if ( spDoc )
{
IDispatchExPtr spDispEx; // Get the document's IDispatchEx
spDoc->QueryInterface( IID_IDispatchEx,
(void**)&spDispEx );
if ( spDispEx )
{
_bstr_t bstrName("XMLDocument");
DISPID dispid; // Get the XMLDocument expando property
spDispEx->GetDispID( bstrName,
fdexNameCaseSensitive,
&dispid );
if ( SUCCEEDED(hr) && dispid != DISPID_UNKNOWN )
{
VARIANT var;
DISPPARAMS dpNoArgs = {NULL, NULL, 0, 0}; // Get the XMLDocument value
hr = spDispEx->Invoke( dispid,
IID_NULL,
LOCALE_USER_DEFAULT,
DISPATCH_PROPERTYGET,
&dpNoArgs,
&var,
NULL,
NULL );
if ( SUCCEEDED(hr) && var.vt == VT_DISPATCH )
{
IXMLDOMDocument* pXMLDoc=NULL; // Get the IXMLDOMDocument interface
var.pdispVal->QueryInterface(
IID_IXMLDOMDocument,
(void**)&pXMLDoc );
VariantClear( &var );
if ( pXMLDoc )
{
// Get the root element
IXMLDOMElement* pXMLElem=NULL; pXMLDoc->get_documentElement( &pXMLElem );
if ( pXMLElem )
{
BSTR bstr;
USES_CONVERSION; // Get/display the tag name
pXMLElem->get_tagName( &bstr );
AfxMessageBox( OLE2T(bstr) );
pXMLElem->Release();
}
pXMLDoc->Release();
}
}
}
}
}
}
}
}
}
{
IDispatchPtr spDisp;
HRESULT hr; hr = m_pBrowserApp->QueryInterface(IID_IDispatch, (void**) &spDisp);
// Is the IDispatch* passed to us for the top-level window ?
if (pDisp == spDisp)
{
IHTMLDocument2Ptr spDoc; // Get the active document
spDoc = GetHtmlDocument();
if ( spDoc )
{
IHTMLWindow2Ptr spWin; // Get the top-level window
spDisp = spDoc->Script;
spWin = spDisp;
if ( spWin )
{
// Get the document
spDoc = spWin->document;
if ( spDoc )
{
IDispatchExPtr spDispEx; // Get the document's IDispatchEx
spDoc->QueryInterface( IID_IDispatchEx,
(void**)&spDispEx );
if ( spDispEx )
{
_bstr_t bstrName("XMLDocument");
DISPID dispid; // Get the XMLDocument expando property
spDispEx->GetDispID( bstrName,
fdexNameCaseSensitive,
&dispid );
if ( SUCCEEDED(hr) && dispid != DISPID_UNKNOWN )
{
VARIANT var;
DISPPARAMS dpNoArgs = {NULL, NULL, 0, 0}; // Get the XMLDocument value
hr = spDispEx->Invoke( dispid,
IID_NULL,
LOCALE_USER_DEFAULT,
DISPATCH_PROPERTYGET,
&dpNoArgs,
&var,
NULL,
NULL );
if ( SUCCEEDED(hr) && var.vt == VT_DISPATCH )
{
IXMLDOMDocument* pXMLDoc=NULL; // Get the IXMLDOMDocument interface
var.pdispVal->QueryInterface(
IID_IXMLDOMDocument,
(void**)&pXMLDoc );
VariantClear( &var );
if ( pXMLDoc )
{
// Get the root element
IXMLDOMElement* pXMLElem=NULL; pXMLDoc->get_documentElement( &pXMLElem );
if ( pXMLElem )
{
BSTR bstr;
USES_CONVERSION; // Get/display the tag name
pXMLElem->get_tagName( &bstr );
AfxMessageBox( OLE2T(bstr) );
pXMLElem->Release();
}
pXMLDoc->Release();
}
}
}
}
}
}
}
}
}
ON_EVENT(CHtmlView, AFX_IDW_PANE_FIRST, 259 /* DocumentComplete */, DocumentComplete, VTS_DISPATCH VTS_PVARIANT)END_EVENTSINK_MAP()void CHtmlView::DocumentComplete(LPDISPATCH pDisp, VARIANT* URL)
{
UNUSED_ALWAYS(pDisp);
ASSERT(V_VT(URL) == VT_BSTR); CString str(V_BSTR(URL));
OnDocumentComplete(str);
}
在你的类里面照抄就可以了
#import <mshtml.tlb> // Internet Explorer 5
这一段我早就加进去了IXMLDOMElement可不是这里面定义的哦(我导入了这个tlb文件仔细看过)。BEGIN_EVENTSINK_MAP(CHtmlView, CFormView)
ON_EVENT(CHtmlView, AFX_IDW_PANE_FIRST, 259 /* DocumentComplete */, DocumentComplete, VTS_DISPATCH VTS_PVARIANT)END_EVENTSINK_MAP()我把这一段抄进去,并加入了DocumentComplete函数,编译说未定义_GetSinkmap
#include "mshtml.h"
#import "C:\winnt\system32\mshtml.tlb" // location of mshtml.tlb
编译时这一句
// Get the top-level window
spDisp = spDoc->Script;
说
error C2039: 'Script' : is not a member of 'IHTMLDocument2'
怎么回事啊?
#include "mshtml.h"
#import "C:\winnt\system32\mshtml.tlb" // location of mshtml.tlb
编译时这一句
// Get the top-level window
spDisp = spDoc->Script;
说
error C2039: 'Script' : is not a member of 'IHTMLDocument2'
怎么回事啊?
spDoc是IHTMLDocument2Ptr吗?
#include "stdafx.h"
#include "mfcie.h"
#include "MainFrm.h"#include "mfcieDoc.h"
#include "mfcieVw.h"#pragma warning(disable : 4192)
#pragma warning(disable : 4049)
#pragma warning(disable : 4146)
#import <mshtml.tlb>
#import <msxml4.dll>
#pragma warning(default: 4192)
#pragma warning(default: 4049)
#pragma warning(default: 4146)
// Include Microsoft HIML definitions...#include <dispex.h>//You may derive a class from CComModule and use it if you want to override
//something, but do not change the name of _Module
#include <atlbase.h>//for atlcom.h
extern CComModule _Module;//for atlbase.h
#include <atlcom.h>//for OLE2T……
END_MESSAGE_MAP()BEGIN_EVENTSINK_MAP(CMfcieView, CHtmlView)
ON_EVENT(CMfcieView, AFX_IDW_PANE_FIRST, 259 /* DocumentComplete */, DocumentComplete, VTS_DISPATCH VTS_PVARIANT)
END_EVENTSINK_MAP()/////////////////////////////////////////////////////////////////////////////
// CMfcieView construction/destruction……
//END OF MFC MEMORY LEAK BUG FIXvoid CMfcieView::DocumentComplete(LPDISPATCH pDisp, VARIANT* URL)
{
ASSERT(V_VT(URL) == VT_BSTR);
IDispatchPtr spDisp;
HRESULT hr; hr = m_pBrowserApp->QueryInterface(IID_IDispatch, (void**) &spDisp);
// Is the IDispatch* passed to us for the top-level window ?
if (pDisp == spDisp)
{
MSHTML::IHTMLDocument2Ptr spDoc; // Get the active document
spDoc = GetHtmlDocument();
if ( spDoc )
{
MSHTML::IHTMLWindow2Ptr spWin; // Get the top-level window
spDisp = spDoc->Script;
spWin = spDisp;
if ( spWin )
{
// Get the document
spDoc = spWin->document;
if ( spDoc )
{
IDispatchExPtr spDispEx; // Get the document's IDispatchEx
spDoc->QueryInterface( IID_IDispatchEx,
(void**)&spDispEx );
if ( spDispEx )
{
_bstr_t bstrName("XMLDocument");
DISPID dispid; // Get the XMLDocument expando property
spDispEx->GetDispID( bstrName,
fdexNameCaseSensitive,
&dispid );
if ( SUCCEEDED(hr) && dispid != DISPID_UNKNOWN )
{
VARIANT var;
DISPPARAMS dpNoArgs = {NULL, NULL, 0, 0}; // Get the XMLDocument value
hr = spDispEx->Invoke( dispid,
IID_NULL,
LOCALE_USER_DEFAULT,
DISPATCH_PROPERTYGET,
&dpNoArgs,
&var,
NULL,
NULL );
if ( SUCCEEDED(hr) && var.vt == VT_DISPATCH )
{
MSXML2::IXMLDOMDocument* pXMLDoc=NULL; // Get the IXMLDOMDocument interface
var.pdispVal->QueryInterface(
IID_IXMLDOMDocument,
(void**)&pXMLDoc );
VariantClear( &var );
if ( pXMLDoc )
{
// Get the root element
MSXML2::IXMLDOMElement* pXMLElem=NULL; pXMLDoc->get_documentElement( &pXMLElem );
if ( pXMLElem )
{
BSTR bstr;
USES_CONVERSION; // Get/display the tag name
pXMLElem->get_tagName( &bstr );
AfxMessageBox( OLE2T(bstr) );
pXMLElem->Release();
}
pXMLDoc->Release();
}
}
}
}
}
}
}
}
}
//end new code
void DocumentComplete(LPDISPATCH pDisp, VARIANT* URL);
……
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
DECLARE_EVENTSINK_MAP()
};