Example #1
0
STDMETHODIMP CBhoApp::Invoke(DISPID dispidMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pvarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
{
	switch(dispidMember)
	{
	case DISPID_BEFORENAVIGATE2:
		{
			//liigo 2013-6-26:
			//there are 7 args in pDispParams->rgvarg[0] to pDispParams->rgvarg[6]
			assert(pDispParams->cArgs == 7);

			//Arg0: Cancel
			VARIANTARG argCancel = pDispParams->rgvarg[0];
			assert(argCancel.vt == (VT_BYREF|VT_BOOL));
			//we can set VARIANT_TRUE to cancel the navigation
			// *(argCancel.pboolVal) = VARIANT_TRUE;

			//Arg1: Headers
			VARIANTARG argHeaders = pDispParams->rgvarg[1];
			assert(argHeaders.vt == (VT_BYREF|VT_VARIANT));
			assert(argHeaders.pvarVal->vt == VT_BSTR);
			BSTR bstrHeaders = argHeaders.pvarVal->bstrVal;
			MessageBoxW(0, bstrHeaders, L"headers", MB_OK);

			//Arg2: PostData
			VARIANTARG argPostData = pDispParams->rgvarg[2];
			assert(argPostData.vt == (VT_BYREF|VT_VARIANT));
			assert(argPostData.pvarVal->vt == (VT_BYREF|VT_VARIANT));
			if(argPostData.pvarVal->pvarVal->vt != VT_EMPTY)
			{
				assert(argPostData.pvarVal->pvarVal->vt == (VT_ARRAY|VT_UI1));
				SAFEARRAY* pSafeArray = argPostData.pvarVal->pvarVal->parray;
				assert(pSafeArray);
				assert(pSafeArray->cDims == 1 && pSafeArray->cbElements == 1); //array of VT_UI1
				unsigned int nPostDataSize = pSafeArray->rgsabound[0].cElements * pSafeArray->cbElements; //in bytes
				void* pPostData = pSafeArray->pvData;
				ShowDataMessage(pPostData, nPostDataSize, "post data binary");
				MessageBoxA(0, (char*)pPostData, "post data string", MB_OK);
			}

			//Arg3: TargetFrameName
			VARIANTARG argTargetFrameName = pDispParams->rgvarg[3];
			assert(argTargetFrameName.vt == (VT_BYREF|VT_VARIANT));
			assert(argTargetFrameName.pvarVal->vt == VT_BSTR);
			BSTR bstrTargetFrameName = argTargetFrameName.pvarVal->bstrVal;
			MessageBoxW(0, bstrTargetFrameName, L"TargetFrameName", MB_OK);

			//Arg4: Flags
			VARIANTARG argFlags = pDispParams->rgvarg[4];
			assert(argFlags.vt == (VT_BYREF|VT_VARIANT));
			assert(argFlags.pvarVal->vt == VT_I4);
			LONG lFlags = argFlags.pvarVal->lVal;

			//Arg5: Url
			VARIANTARG argUrl = pDispParams->rgvarg[5];
			assert(argUrl.vt == (VT_BYREF|VT_VARIANT));
			assert(argUrl.pvarVal->vt == VT_BSTR);
			BSTR bstrUrl = argUrl.pvarVal->bstrVal;
			MessageBoxW(0, bstrUrl, L"URL", MB_OK);

			//Arg6: WebBrowser
			VARIANTARG argWebBrowser = pDispParams->rgvarg[6];
			assert(argWebBrowser.vt == VT_DISPATCH);
			IDispatch* pDispWebBrowser = argWebBrowser.pdispVal;

			return S_OK;
		}

	case DISPID_NAVIGATECOMPLETE2: 
		{
			return S_OK;
		}

	case DISPID_DOCUMENTCOMPLETE:
		{
			IDispatch* pDocDisp = NULL;
			if( m_spWebBrowser2->get_Document(&pDocDisp) == S_OK)
			{
				IHTMLDocument2* pDoc = NULL;
				if(pDocDisp->QueryInterface(IID_IHTMLDocument2, (void**)&pDoc) == S_OK)
				{
					BSTR title;
					pDoc->get_title(&title);
					MessageBox(0, title, NULL, MB_OK);
				}
			}
			return S_OK;
		}

	} // end switch(dispidMember)

	return S_FALSE;
}
Example #2
0
bool CMyInternetExplorer::FindUsingTitle (const CString & sTitleToSearch)
{
	if (m_pWebBrowser != NULL)
	{
		m_pWebBrowser->Release ();
		m_pWebBrowser = NULL;
	}

	HRESULT hr;
	SHDocVw::IShellWindowsPtr spSHWinds; 
	hr = spSHWinds.CreateInstance (__uuidof(SHDocVw::ShellWindows)); 
	
	if (FAILED (hr))
		return false;

	ASSERT (spSHWinds != NULL);
	
	long nCount = spSHWinds->GetCount ();

	IDispatchPtr spDisp;
	
	for (long i = 0; i < nCount; i++)
	{
		_variant_t va (i, VT_I4);
		spDisp = spSHWinds->Item (va);
		
		IWebBrowser2 * pWebBrowser = NULL;
		hr = spDisp.QueryInterface (IID_IWebBrowser2, & pWebBrowser);
		
		if (pWebBrowser != NULL)
		{
			HRESULT hr;
			IDispatch* pHtmlDocDispatch = NULL;
			IHTMLDocument2 * pHtmlDoc = NULL;
			
			// Retrieve the document object.
			hr = pWebBrowser->get_Document (&pHtmlDocDispatch);
			
			if (SUCCEEDED (hr) && (pHtmlDocDispatch != NULL))
			{
				// Query for IPersistStreamInit.
				hr = pHtmlDocDispatch->QueryInterface (IID_IHTMLDocument2,  (void**)&pHtmlDoc);
				if (SUCCEEDED (hr) && (pHtmlDoc != NULL))
				{
					CString sTitle;

					HWND hWnd = NULL;
					pWebBrowser->get_HWND ((long*)(&hWnd));
					if (::IsWindow (hWnd))
					{
						int nLen = ::GetWindowTextLength (hWnd);
						::GetWindowText (hWnd, sTitle.GetBufferSetLength (nLen), nLen + 1);
						sTitle.ReleaseBuffer ();
					}
					
					// If I cannot get the window title (should never happen though)
					// So, lets just use the title of the document
					if (sTitle.IsEmpty ())
					{
						BSTR bstrTitle;
						hr = pHtmlDoc->get_title (&bstrTitle);
						if (!FAILED (hr))
						{
							sTitle = bstrTitle;
							SysFreeString (bstrTitle); 
						}
					}
					
					if (StringHelper::WildcardCompareNoCase (sTitleToSearch, sTitle))
					{
						m_pWebBrowser = pWebBrowser;
						pHtmlDoc->Release ();
						pHtmlDocDispatch->Release ();
						// Exit the method safely!
						return true;
					}
					pHtmlDoc->Release();
				}
				pHtmlDocDispatch->Release ();
			}
			pWebBrowser->Release ();
		}
	}
	
	return false;
}