Advertisement
Guest User

C++ eval test

a guest
Aug 21st, 2013
1,010
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 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. struct ComInit {
  16. ComInit() { ::CoInitialize(NULL); }
  17. ~ComInit() { CoUninitialize(); }
  18. };
  19.  
  20. int _tmain(int argc, _TCHAR* argv[])
  21. {
  22. ComInit comInit;
  23.  
  24. CComPtr<IWebBrowser2> ie;
  25. _S( ie.CoCreateInstance(L"InternetExplorer.Application", NULL, CLSCTX_LOCAL_SERVER) );
  26.  
  27. _S( ie->put_Visible(VARIANT_TRUE) );
  28.  
  29. CComVariant ve;
  30. _S( ie->Navigate2(&CComVariant(L"http://www.example.com"), &ve, &ve, &ve, &ve) );
  31. MessageBox(NULL, L"pause...", NULL, MB_OK);
  32.  
  33. CComPtr<IDispatch> dispDoc;
  34. _S( ie->get_Document(&dispDoc) );
  35. CComPtr<IHTMLDocument2> htmlDoc;
  36. _S( dispDoc->QueryInterface(&htmlDoc) );
  37. CComPtr<IHTMLWindow2> htmlWindow;
  38. _S( htmlDoc->get_parentWindow(&htmlWindow) );
  39.  
  40. CComDispatchDriver dispWindow;
  41. _S( htmlWindow->QueryInterface(&dispWindow) );
  42.  
  43. CComPtr<IDispatchEx> dispexWindow;
  44. _S( htmlWindow->QueryInterface(&dispexWindow) );
  45.  
  46. DISPID dispidEval = -1;
  47. _S( dispexWindow->GetDispID(CComBSTR("eval"), fdexNameCaseSensitive, &dispidEval) );
  48. _S( dispWindow.Invoke1(dispidEval, &CComVariant("function DoAlert(text) { alert(text); }")) ); // inject
  49.  
  50. DISPID dispidDoAlert = -1;
  51. _S( dispexWindow->GetDispID(CComBSTR("DoAlert"), fdexNameCaseSensitive, &dispidDoAlert) );
  52. _S( dispWindow.Invoke1(dispidDoAlert, &CComVariant("Hello, World!")) ); // call
  53.  
  54. return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement