Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // http://stackoverflow.com/questions/18342200/how-do-i-call-eval-in-ie-from-c/18349546//
- //
- #include <tchar.h>
- #include <ExDisp.h>
- #include <mshtml.h>
- #include <dispex.h>
- #include <atlbase.h>
- #include <atlcomcli.h>
- #define _S(a) \
- { HRESULT hr = (a); if (FAILED(hr)) return hr; }
- struct ComInit {
- ComInit() { ::CoInitialize(NULL); }
- ~ComInit() { CoUninitialize(); }
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- ComInit comInit;
- CComPtr<IWebBrowser2> ie;
- _S( ie.CoCreateInstance(L"InternetExplorer.Application", NULL, CLSCTX_LOCAL_SERVER) );
- _S( ie->put_Visible(VARIANT_TRUE) );
- CComVariant ve;
- _S( ie->Navigate2(&CComVariant(L"http://www.example.com"), &ve, &ve, &ve, &ve) );
- MessageBox(NULL, L"pause...", NULL, MB_OK);
- CComPtr<IDispatch> dispDoc;
- _S( ie->get_Document(&dispDoc) );
- CComPtr<IHTMLDocument2> htmlDoc;
- _S( dispDoc->QueryInterface(&htmlDoc) );
- CComPtr<IHTMLWindow2> htmlWindow;
- _S( htmlDoc->get_parentWindow(&htmlWindow) );
- CComDispatchDriver dispWindow;
- _S( htmlWindow->QueryInterface(&dispWindow) );
- CComPtr<IDispatchEx> dispexWindow;
- _S( htmlWindow->QueryInterface(&dispexWindow) );
- DISPID dispidEval = -1;
- _S( dispexWindow->GetDispID(CComBSTR("eval"), fdexNameCaseSensitive, &dispidEval) );
- _S( dispWindow.Invoke1(dispidEval, &CComVariant("function DoAlert(text) { alert(text); }")) ); // inject
- DISPID dispidDoAlert = -1;
- _S( dispexWindow->GetDispID(CComBSTR("DoAlert"), fdexNameCaseSensitive, &dispidDoAlert) );
- _S( dispWindow.Invoke1(dispidDoAlert, &CComVariant("Hello, World!")) ); // call
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement