VC6下CHtmlView中最简单最全面的程序与网页交互方法
简单来说,终极目标------VC6和网页相互调用对方的数据和方法;而调用方法时重点要捕获返回值。
VC6下远没有VC7及其之后提供的操作丰富方便。一些深度应用的破解方法,钻进应用的原理中,解释起来很繁琐;同时操作起来也非常麻烦。能够达到同样的功能,但是代码越少,使用起来越方便,可能这就是追求目标。通过如下的一些小技巧来达成目标,可以成系列的解决所有问题。
1. VC6获取网页DOM
网页的模型就是DOM。主要就是对ChtmlView的功能进行增强,增强对DOM元素的取设、查询等操作。
说明:要识别IHTMLDocument2,必须 #include “comdef.h”
1.1 获取 document.body
IHTMLElementPtr CHtmlViewEx::sIE_GetBody() { IHTMLElementPtr ipBody; IHTMLDocument2Ptr ipDocument = GetHtmlDocument(); if(ipDocument==NULL) return ipBody; ipDocument->get_body(&ipBody); return ipBody; }
1.2 实现该功能 document.getElementByID()
// js中的这个可以用 // 是因为dispinterface DispHTMLDocument 中有getElementByID 该方法 // IHTMLDocument2 中没有而IHTMLDocument3有。 // 考虑到我们最简单的使用,我们不会引入IHTMLDocument3,而直接实现 IHTMLElementPtr CHtmlViewEx::sIE_GetElementByID(LPCTSTR strID) { IHTMLElementPtr ipElement; IHTMLDocument2Ptr ipDocument = GetHtmlDocument(); if(ipDocument==NULL) return ipElement; IHTMLElementCollectionPtr ipSet; ipDocument->get_all(&ipSet); if(ipSet==NULL) return ipElement; IDispatchPtr ipDispatch; _variant_t vtIndex; _bstr_t bstrName(strID); _variant_t vtName = bstrName; ipSet->item(vtName, vtIndex, &ipDispatch); ipElement = ipDispatch; return ipElement; }
1.3 获取特定值,其实都是com的方法调用
IHTMLElementPtr ipBody = sIE_GetBody(); _bstr_t bstrName(L"title"); _variant_t vtValue; ipBody->getAttribute(bstrName, 0, &vtValue); CString strTitle(vtValue.bstrVal);
2. VC6调用网页JS函数和方法
2.1 获取脚本总的对象
IDispatchPtr CHtmlViewEx::sIE_GetScript() { IDispatchPtr ipDispatch; IHTMLDocument2Ptr ipDocument = GetHtmlDocument(); if(ipDocument==NULL) return ipDispatch; HRESULT hr = ipDocument->get_Script(&ipDispatch); return ipDispatch; }
2.2 调用方法,注意方法可能存在参数的先后顺序
// CDispatchObj方法实际上就是对IDispatch的invoke的简单封装 CDispatchObj obj(ipDispatch.GetInterfacePtr()); _variant_t vt(bstrValue); obj.Invoke1_S(L"funcA", vt, 0);
3. 网页调用VC6方法:协议扩展
网页脚本中要调用VC6的方法时,都通过扩展协议来实现;然后VC6来解析该协议。
3.1 网页调用协议iitp:
var trm_view = function() // 查看 { var strUrl = "iitp://mydo/view.do"; document.location.href = strUrl; }
3.2 VC6:截获链接并特殊处理协议iitp
注意*pbCancel = TRUE避免链接进行了跳转
void CHtmlViewEx::OnBeforeNavigate2(LPCTSTR lpszURL, DWORD nFlags, LPCTSTR lpszTargetFrameName, CByteArray& baPostedData, LPCTSTR lpszHeaders, BOOL* pbCancel) { CString strUrl = lpszURL; CString strProtcl = _T("iitp://mydo/"); if(strUrl.CompareNoCase(strProtcl)>0 ) { CString strCmd = _T("iitp://mydo/view.do"); // 查看 if(strUrl.CompareNoCase(strCmd)==0) { // --todo my function-- UpdateWindow(); } // 注意避免跳转页面 *pbCancel = TRUE; } else { CHtmlView::OnBeforeNavigate2(lpszURL, nFlags, lpszTargetFrameName, baPostedData,lpszHeaders, pbCancel); } }
4. 网页调用VC6的数据和方法:功能扩展
4.1 直接将数据绑定在某一DOM上
_RecordsetPtr ipRst; _variant_t vtRst(ipRst, true); ipBody->setAttribute(L"rst", vtRst, 0);
以后网页脚本中就可以使用document.body.rst来调用ipRst的各种方法了。
4.2.1 给网页扩展一个IDispatch对象
如果是MFC,则可以直接使用MFC 自动化对象,避免写组件
class CMyHtmlView : public ChtmlView { …… //{{AFX_DISPATCH(CMyHtmlView) afx_msg BSTR GetVersion(); afx_msg void SetVersion(LPCTSTR lpszNewValue); afx_msg BSTR SayIt(long num); //}}AFX_DISPATCH DECLARE_DISPATCH_MAP() }; CMyHtmlView::CMyHtmlView() { EnableAutomation(); } BEGIN_DISPATCH_MAP(CMyHtmlView, CHtmlView) //{{AFX_DISPATCH_MAP(CMyHtmlView) DISP_PROPERTY_EX(CMyHtmlView, "version", GetVersion, SetVersion, VT_BSTR) DISP_FUNCTION(CMyHtmlView, "SayIt", SayIt, VT_BSTR, VTS_I4) //}}AFX_DISPATCH_MAP END_DISPATCH_MAP() BSTR CMyHtmlView::SayIt(long num) { CString strResult; strResult.Format("num = %d", num*2); return strResult.AllocSysString(); } void CMyHtmlView::DocumentComplete(LPDISPATCH pDisp, VARIANT* URL) { CHtmlView::DocumentComplete(pDisp, URL); IHTMLDocument2Ptr ipDocument = GetHtmlDocument(); if(ipDocument) { IHTMLElementPtr ipBody; ipDocument->get_body(&ipBody); LPDISPATCH pDispatch = GetIDispatch(TRUE); HRESULT hr = ipBody->setAttribute(L"external", _variant_t(pDispatch, false), 0); } }
4.2.2 脚本中就可以调用:
function test() { var rtn = document.body.external.SayIt(6); alert(rtn); }
虽然最通用的方法是使用window.external,但是需要改写。此种方法压根就不需要进行特殊的扩展。