Skip to content

Instantly share code, notes, and snippets.

@kumatti1
Last active August 29, 2015 14:17
Show Gist options
  • Save kumatti1/d64dc0c7487997f12afe to your computer and use it in GitHub Desktop.
Save kumatti1/d64dc0c7487997f12afe to your computer and use it in GitHub Desktop.
C++でDocumentCompleteイベント
#include <Shlwapi.h>
#include <windows.h>
#include <Exdisp.h>
#import <shdocvw.dll>
#import <mshtml.tlb>
#using <System.dll>
#using <System.Windows.Forms.dll>
using namespace System;
using namespace System::Runtime::InteropServices;
using namespace System::Windows::Forms;
DWORD dwCookie = 0;
HANDLE hEvent = 0;
public ref class Hoge
{
public:
[DispId(259)]//DISPID_DOCUMENTCOMPLETE
void DocumentComplete(Object^ pDisp, Object^ pvtURL)
{
OutputDebugStringW(L"キタ━(゚∀゚)━!");
SetEvent( hEvent );
}
};
HRESULT test()
{
HRESULT hr = S_OK;
try{
SHDocVw::IWebBrowser2Ptr pBrowser;
hr = pBrowser.CreateInstance(__uuidof(SHDocVw::InternetExplorer));
if (FAILED(hr)) _com_issue_error(hr);
pBrowser->Visible = VARIANT_TRUE;
auto obj = gcnew Hoge;
IDispatchPtr pDisp;
pDisp.Attach( static_cast<IDispatch*>(Marshal::GetIDispatchForObject(obj).ToPointer() ));
hr = ConnectToConnectionPoint(pDisp, __uuidof(DWebBrowserEvents2), TRUE, pBrowser, &dwCookie, nullptr);
if (FAILED(hr)) _com_issue_error(hr);
pBrowser->Navigate( L"https://www.google.co.jp/");
SECURITY_ATTRIBUTES st={};
hEvent = CreateEventW(&st, FALSE, FALSE, nullptr);
while((MsgWaitForMultipleObjects(1, &hEvent, FALSE, INFINITE, QS_ALLINPUT)) == (WAIT_OBJECT_0 + 1))
{
Application::DoEvents();
}
hr = ConnectToConnectionPoint( nullptr, __uuidof(DWebBrowserEvents2), FALSE, pBrowser, &dwCookie, nullptr);
CloseHandle( hEvent );
}
catch (_com_error& e) {
hr = e.Error();
}
return hr;
}
[MTAThread]
int WINAPI WinMain(HINSTANCE, HINSTANCE, PSTR, int)
{
CoInitialize(NULL);
test();
CoUninitialize();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment