/*********************************************************************
Copyright (C) 2002 Mustafa Demirhan
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
emindemirhan@yahoo.com
**********************************************************************/
// IEHelper.cpp: implementation of the IEHelper class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "IEHelper.h"
#include "StringHelper.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CMyInternetExplorer::CMyInternetExplorer (LPCTSTR sBrowserID) :
m_pWebBrowser (NULL)
{
if (FAILED (CoInitialize (NULL)))
{
MessageBox (NULL, _T ("Failed to initialize the COM Library to create the Internet Explorer object.\r\n\r\nPlease contact to support@macroangel.com for more information."), _T ("Macro Angel"), MB_ICONERROR);
ASSERT (FALSE);
}
m_sBrowserID = sBrowserID;
m_sBrowserID.MakeLower ();
}
CMyInternetExplorer::~CMyInternetExplorer ()
{
if (m_pWebBrowser != NULL)
{
m_pWebBrowser->Release ();
m_pWebBrowser = NULL;
}
CoUninitialize ();
}
CString CMyInternetExplorer::GetBrowserID () const
{
return m_sBrowserID;
}
bool CMyInternetExplorer::CreateNewInstance ()
{
if (m_pWebBrowser != NULL)
{
m_pWebBrowser->Release ();
m_pWebBrowser = NULL;
}
HRESULT hr;
IWebBrowser2* pWebBrowser = NULL;
hr = CoCreateInstance (CLSID_InternetExplorer, NULL, CLSCTX_SERVER, IID_IWebBrowser2, (LPVOID*)&pWebBrowser);
if (SUCCEEDED (hr) && (pWebBrowser != NULL))
{
m_pWebBrowser = pWebBrowser;
m_pWebBrowser->put_Visible (VARIANT_TRUE);
return true;
}
else
{
if (pWebBrowser)
pWebBrowser->Release ();
return false;
}
return false;
}
bool CMyInternetExplorer::IsValid ()
{
if (m_pWebBrowser == NULL)
return false;
// TODO: I dont know how to check whether it is still valid or not!
return true;
}
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;
}
CString CMyInternetExplorer::GetFullName() const
{
ASSERT (m_pWebBrowser != NULL);
if (m_pWebBrowser == NULL)
return CString ();
BSTR bstr;
m_pWebBrowser->get_FullName (&bstr);
CString retVal (bstr);
SysFreeString (bstr);
return retVal;
}
CString CMyInternetExplorer::GetType() const
{
ASSERT (m_pWebBrowser != NULL);
if (m_pWebBrowser == NULL)
return CString ();
BSTR bstr;
m_pWebBrowser->get_Type (&bstr);
CString retVal(bstr);
SysFreeString(bstr);
return retVal;
}
CString CMyInternetExplorer::GetLocationName() const
{
ASSERT(m_pWebBrowser != NULL);
if (m_pWebBrowser == NULL)
return CString ();
BSTR bstr;
m_pWebBrowser->get_LocationName (&bstr);
CString retVal (bstr);
SysFreeString (bstr); // Added this line to prevent leak.
return retVal;
}
CString CMyInternetExplorer::GetLocationURL() const
{
ASSERT (m_pWebBrowser != NULL);
if (m_pWebBrowser == NULL)
return CString ();
BSTR bstr;
m_pWebBrowser->get_LocationURL (&bstr);
CString retVal (bstr);
SysFreeString (bstr); // Added this line to prevent leak.
return retVal;
}
void CMyInternetExplorer::Navigate(LPCTSTR lpszURL, DWORD dwFlags /* = 0 */,
LPCTSTR lpszTargetFrameName /* = NULL */ ,
LPCTSTR lpszHeaders /* = NULL */, LPVOID lpvPostData /* = NULL */,
DWORD dwPostDataLen /* = 0 */)
{
CString strURL (lpszURL);
BSTR bstrURL = strURL.AllocSysString ();
COleSafeArray vPostData;
if (lpvPostData != NULL)
{
if (dwPostDataLen == 0)
dwPostDataLen = lstrlen ((LPCTSTR) lpvPostData);
vPostData.CreateOneDim (VT_UI1, dwPostDataLen, lpvPostData);
}
m_pWebBrowser->Navigate (bstrURL, COleVariant ((long) dwFlags, VT_I4), COleVariant (lpszTargetFrameName, VT_BSTR),
vPostData, COleVariant (lpszHeaders, VT_BSTR));
SysFreeString (bstrURL);
}
bool CMyInternetExplorer::WaitTillLoaded (int nTimeout)
{
READYSTATE result;
DWORD nFirstTick = GetTickCount ();
do
{
m_pWebBrowser->get_ReadyState (&result);
if (result != READYSTATE_COMPLETE)
Sleep (250);
if (nTimeout > 0)
{
if ((GetTickCount () - nFirstTick) > nTimeout)
break;
}
} while (result != READYSTATE_COMPLETE);
if (result == READYSTATE_COMPLETE)
return true;
else
return false;
}
void CMyInternetExplorer::Refresh ()
{
ASSERT (m_pWebBrowser != NULL);
if (m_pWebBrowser == NULL)
return;
m_pWebBrowser->Refresh ();
}
void CMyInternetExplorer::Stop ()
{
ASSERT (m_pWebBrowser != NULL);
if (m_pWebBrowser == NULL)
return;
m_pWebBrowser->Stop ();
}
void CMyInternetExplorer::GoBack ()
{
ASSERT (m_pWebBrowser != NULL);
if (m_pWebBrowser == NULL)
return;
m_pWebBrowser->GoBack ();
}
void CMyInternet
没有合适的资源?快使用搜索试试~ 我知道了~
使用VC的Com接口操作IE的例子
共4个文件
h:2个
cpp:2个
4星 · 超过85%的资源 需积分: 10 242 下载量 36 浏览量
2009-06-28
16:26:08
上传
评论 2
收藏 8KB ZIP 举报
温馨提示
使用VC的Com接口操作IE的例子. 说明IWebBrowser2, IHTMLDocument2 and IHTMLElement 等的使用方法。
资源推荐
资源详情
资源评论
收起资源包目录
IEHelper_src.zip (4个子文件)
StringHelper.cpp 4KB
IEHelper.cpp 26KB
IEHelper.h 3KB
StringHelper.h 2KB
共 4 条
- 1
goodwinds
- 粉丝: 66
- 资源: 25
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页