開発環境 Microsoft Visual C++ 2010 Express (SP1)
実行環境 Microsoft Windows XP Home Edition (SP3)
プロジェクトの種類 Win32 コンソール アプリケーション
プロジェクト名 getbody
アプリケーションの種類 コンソール アプリケーション
追加のオプション 空のプロジェクト


getbody.cpp
#include <ExDisp.h>
#include <MsHTML.h>
#include <locale.h>
#include <stdio.h>
 
int main()
{
	IWebBrowser2 *pBrowser2 = NULL;
	IDispatch *pDisp = NULL;
	IHTMLDocument2 *pDocument2 = NULL;
	IHTMLElement *pElement = NULL;
	BSTR bstrURL = NULL;
	BSTR p;
	VARIANT vEmpty;
	VARIANT_BOOL vb;
	HRESULT hr;
 
	_wsetlocale(LC_CTYPE, L"");
 
	hr = OleInitialize(NULL);
	if (FAILED(hr)) return 1;
 
	hr = CoCreateInstance(CLSID_InternetExplorer, NULL, CLSCTX_LOCAL_SERVER,
		IID_PPV_ARGS(&pBrowser2));
	if (FAILED(hr)) goto Exit;
 
	hr = pBrowser2->put_Left(200);
	hr = pBrowser2->put_Top(200);
	hr = pBrowser2->put_Width(800);
	hr = pBrowser2->put_Height(600);
	hr = pBrowser2->put_Visible(VARIANT_TRUE);
 
	bstrURL = SysAllocString(L"http://www.google.co.jp/");
	VariantInit(&vEmpty);
	hr = pBrowser2->Navigate(bstrURL, &vEmpty, &vEmpty, &vEmpty, &vEmpty);
	while (1) {
		pBrowser2->get_Busy(&vb);
		if (vb == VARIANT_FALSE) break;
		Sleep(100);
	}
	hr = pBrowser2->get_Document(&pDisp);
	hr = pDisp->QueryInterface(IID_PPV_ARGS(&pDocument2));
	hr = pDocument2->get_body(&pElement);
	hr = pElement->get_outerHTML(&p);
	wprintf(L"%s", p);
Exit:
	SysFreeString(bstrURL);
	if (pElement) pElement->Release();
	if (pDocument2) pDocument2->Release();
	if (pDisp) pDisp->Release();
	if (pBrowser2) {
		pBrowser2->Quit();
		pBrowser2->Release();
	}
	OleUninitialize();
	return 0;
}
 
最終更新:2012年09月12日 17:44