Advertisement
Noseratio

Calling JavaScript "eval" out-of-proc

Aug 21st, 2013
833
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.51 KB | None | 0 0
  1. //
  2. // http://stackoverflow.com/questions/18342200/how-do-i-call-eval-in-ie-from-c/18349546//
  3. //
  4.  
  5. #include <tchar.h>
  6. #include <ExDisp.h>
  7. #include <mshtml.h>
  8. #include <dispex.h>
  9. #include <atlbase.h>
  10. #include <atlcomcli.h>
  11.  
  12. #define _S(a) \
  13.     { HRESULT hr = (a); if (FAILED(hr)) return hr; }
  14.  
  15. #define disp_cast(disp) \
  16.     ((CComDispatchDriver&)(void(static_cast<IDispatch*>(disp)), reinterpret_cast<CComDispatchDriver&>(disp)))
  17.  
  18. struct ComInit {
  19.     ComInit() { ::CoInitialize(NULL); }
  20.     ~ComInit() { CoUninitialize(); }
  21. };
  22.  
  23. int _tmain(int argc, _TCHAR* argv[])
  24. {
  25.     ComInit comInit;
  26.  
  27.     CComPtr<IWebBrowser2> ie;
  28.     _S( ie.CoCreateInstance(L"InternetExplorer.Application", NULL, CLSCTX_LOCAL_SERVER) );
  29.     _S( ie->put_Visible(VARIANT_TRUE) );
  30.     CComVariant ve;
  31.     _S( ie->Navigate2(&CComVariant(L"http://jsfiddle.net/"), &ve, &ve, &ve, &ve) );
  32.  
  33.     // wait for page to finish loading
  34.     for (;;)
  35.     {
  36.         Sleep(250);
  37.         READYSTATE rs = READYSTATE_UNINITIALIZED;
  38.         ie->get_ReadyState(&rs);
  39.         if ( rs == READYSTATE_COMPLETE )
  40.             break;
  41.     }
  42.  
  43.     // inject __execScript into the main window
  44.    
  45.     CComPtr<IDispatch> dispDoc;
  46.     _S( ie->get_Document(&dispDoc) );
  47.     CComPtr<IHTMLDocument2> htmlDoc;
  48.     _S( dispDoc->QueryInterface(&htmlDoc) );
  49.     CComPtr<IHTMLWindow2> htmlWindow;
  50.     _S( htmlDoc->get_parentWindow(&htmlWindow) );
  51.     CComPtr<IDispatchEx> dispexWindow;
  52.     _S( htmlWindow->QueryInterface(&dispexWindow) );
  53.  
  54.     CComBSTR __execScript("__execScript");
  55.     CComBSTR __execScriptCode(L"(window.__execScript = function(exp) { return eval(exp); }, window.self)");
  56.  
  57.     DISPID dispid = -1;
  58.     _S( dispexWindow->GetDispID(CComBSTR("eval"), fdexNameCaseSensitive, &dispid) );
  59.     _S( disp_cast(dispexWindow).Invoke1(dispid, &CComVariant(__execScriptCode)) );
  60.  
  61.     // inject __execScript into the child frame
  62.    
  63.     WCHAR szCode[1024];
  64.     wsprintfW(szCode, L"document.all.tags(\"iframe\")[0].contentWindow.eval(\"%ls\")", __execScriptCode.m_str);
  65.  
  66.     dispid = -1;
  67.     _S( dispexWindow->GetDispID(__execScript, fdexNameCaseSensitive, &dispid) );
  68.     CComVariant vIframe;
  69.     _S( disp_cast(dispexWindow).Invoke1(dispid, &CComVariant(szCode), &vIframe) ); // inject __execScript and return the iframe's window object
  70.     _S( vIframe.ChangeType(VT_DISPATCH) );
  71.  
  72.     CComPtr<IDispatchEx> dispexIframe;
  73.     _S( V_DISPATCH(&vIframe)->QueryInterface(&dispexIframe) );
  74.  
  75.     dispid = -1;
  76.     _S( dispexIframe->GetDispID(__execScript, fdexNameCaseSensitive, &dispid) );
  77.     _S( disp_cast(dispexIframe).Invoke1(dispid, &CComVariant("alert(document.URL)")) ); // call the code inside child iframe
  78.  
  79.     return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement