static HRESULT LoadURLFromMoniker(IHTMLDocument2Ptr spIHTMLDocument2, _bstr_t bstrURL)
{
HRESULT hr = S_OK;
try
{
IMonikerPtr spMoniler;
HRESULT hr = ::CreateURLMoniker(NULL, bstrURL, &spMoniler);
if (FAILED(hr))
{
return hr;
}
IBindCtxPtr spBindCtx;
hr = ::CreateBindCtx(0, &spBindCtx);
if (FAILED(hr))
{
return hr;
}
IPersistMonikerPtr spPersistMoniker = spIHTMLDocument2;
if (spPersistMoniker == NULL)
{
return E_FAIL;
}
hr = spPersistMoniker->Load(FALSE, spMoniler, spBindCtx, STGM_READ);
}
catch (...)
{
return E_FAIL;
}
return hr;
}
static HRESULT LoadURLFromFile(IHTMLDocument2Ptr spIHTMLDocument2, _bstr_t bstrFileName)
{
HRESULT hr = S_OK;
try
{
IPersistFilePtr spPersistFile = spIHTMLDocument2;
if (spPersistFile == NULL)
{
return E_FAIL;
}
hr = spPersistFile->Load(bstrFileName, 0);
}
catch (...)
{
return E_FAIL;
}
return hr;
}
---------- MSHTMLの生成&読み込みのサンプル ----------
#include <comdef.h>
#include <MSHTML.H>
HRESULT hr = S_OK;
IHTMLDocument2Ptr spIHTMLDocument2;
hr = spIHTMLDocument2.CreateInstance(CLSID_HTMLDocument);
// URLからの読み込み
hr = LoadURLFromMoniker(spIHTMLDocument2, _T("http://www.kanabo.net/"));
// ファイルからの読み込み
hr = LoadURLFromFile(spIHTMLDocument2, _T("c:\\temp\\sample.html"));
---------- 全てのタグを列挙するサンプル ----------
HRESULT hr = S_OK;
IHTMLElementCollectionPtr spHTMLElementCollection;
hr = m_spIHTMLDocument2->get_all(&spHTMLElementCollection);
long lLength = 0;
hr = spHTMLElementCollection->get_length(&lLength);
for (long i = 0; i < lLength; i++)
{
IDispatchPtr spDispatchForHTMLElement;
hr = spHTMLElementCollection->item(_variant_t(i), _variant_t(), &spDispatchForHTMLElement);
if (SUCCEEDED(hr))
{
IHTMLElementPtr spHTMLElement = spDispatchForHTMLElement;
BSTR bstrTagName = NULL;
spHTMLElement->get_tagName(&bstrTagName);
TRACE("TagName : %s\n", (LPCTSTR)CString(bstrTagName));
}
}