サーバレス練習帳

着眼大局着手小局

IHTMLDocument3 で getElementsByTagName

複数の答えが出てくる可能性があるため、
IHTMLElementCollectionを使う必要があり、ちょっと工夫が必要です。



使い方は、いつも通り、このサイト。
http://eternalwindows.jp/browser/mshtml/mshtml02.html

	for (i = 0; i < lImgCount; i++) {
		VariantInit(&varName);
		varName.vt = VT_I4;
		varName.lVal = i;

		VariantInit(&varIndex);
		varIndex.vt = VT_I4;
		varIndex.lVal = 0;

		pImgCollection->item(varName, varIndex, &pDispatch);
		pDispatch->QueryInterface(IID_PPV_ARGS(&pImgElement));
		pDispatch->Release();

上記のリンク先をもとに、私がIHTMLDocument3の getElementsByTagNameで書き直しました。
例えば、すべてのpタグのinnerTextを取得したければ、こんな感じです。

#include <windows.h>
#include <exdisp.h>
#include <mshtml.h>
#include <oleacc.h>
#include <shlwapi.h>

#pragma comment (lib, "oleacc.lib")
#pragma comment (lib, "urlmon.lib")
#pragma comment (lib, "shlwapi.lib")

BOOL GetImages(IHTMLDocument3 *pDocument3);
BOOL GetDocumentFromIE(IHTMLDocument3 **pp);
BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hinstPrev, LPSTR lpszCmdLine, int nCmdShow)
{

	IHTMLDocument3 *pDocument3;

	CoInitialize(NULL);

	if (!GetDocumentFromIE(&pDocument3)) {
		CoUninitialize();
		return 0;
	}

	GetImages(pDocument3);

	CoUninitialize();

	return 0;
}

BOOL GetImages(IHTMLDocument3 *pDocument3)
{
	BSTR                   bstrText;
	LONG                   i, lTitleCount;
	VARIANT                varName, varIndex;
	IDispatch              *pDispatch;
	IHTMLElement        *pTitleElement;
	IHTMLElementCollection *pImgCollection;
	LPWSTR                 lpszFileName;


	BSTR                   bstrTag;
	bstrTag = SysAllocString(L"p");
	pDocument3->getElementsByTagName(bstrTag, &pImgCollection);
	SysFreeString(bstrTag);

	//pDocument2->get_images(&pImgCollection);

	pImgCollection->get_length(&lTitleCount);
	if (lTitleCount == 0) {
		MessageBox(NULL, TEXT("該当タグがありません。"), TEXT("OK"), MB_OK);
		return FALSE;
	}

	for (i = 0; i < lTitleCount; i++) {
		VariantInit(&varName);
		varName.vt = VT_I4;
		varName.lVal = i;

		VariantInit(&varIndex);
		varIndex.vt = VT_I4;
		varIndex.lVal = 0;

		pImgCollection->item(varName, varIndex, &pDispatch);
		pDispatch->QueryInterface(IID_PPV_ARGS(&pTitleElement));
		pDispatch->Release();

		pTitleElement->get_innerText(&bstrText);
		MessageBoxW(NULL, bstrText, L"抽出されたinnerText", MB_OK);



		pTitleElement->Release();
	}

	MessageBox(NULL, TEXT("これで全てです!"), TEXT("OK"), MB_OK);

	pImgCollection->Release();

	return TRUE;
}

BOOL GetDocumentFromIE(IHTMLDocument3 **pp)
{
	HWND    hwnd;
	UINT    uMsg;
	LRESULT lResult;
	HRESULT hr;

	EnumChildWindows(FindWindow(TEXT("IEFrame"), NULL), EnumChildProc, (LPARAM)&hwnd);
	if (hwnd == NULL)
		return FALSE;

	uMsg = RegisterWindowMessage(TEXT("WM_HTML_GETOBJECT"));
	if (!SendMessageTimeout(hwnd, uMsg, 0, 0, SMTO_ABORTIFHUNG, 1000, (LPDWORD)&lResult))
		return FALSE;

	hr = ObjectFromLresult(lResult, IID_IHTMLDocument3, 0, (void **)pp);
	if (FAILED(hr))
		return FALSE;

	return TRUE;
}


BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM lParam)
{
	TCHAR szClassName[256];

	GetClassName(hwnd, szClassName, sizeof(szClassName) / sizeof(TCHAR));
	if (lstrcmp(szClassName, TEXT("Internet Explorer_Server")) == 0) {
		*((HWND *)lParam) = hwnd;
		return FALSE;
	}
	else
		return TRUE;
}