title: Run JavaScript function from C++ without MFC
body: <p>I'm trying to run a JavaScript function in C++ without any MFC or GUI. </p>
  
  <p>I tried to create a webbrowser pointer and after to get the document from it. </p>
  
  <pre><code>CoCreateInstance(CLSID_WebBrowser, NULL, CLSCTX_SERVER, 
                     IID_IWebBrowser2, (void**)&amp;pBrowser2);
  if (pBrowser2)
  {
     VARIANT vEmpty;
     VariantInit(&amp;vEmpty);
  
     BSTR bstrURL = SysAllocString(L"file://D:/file.html");
  
     HRESULT hr = pBrowser2-&gt;Navigate(bstrURL, &amp;vEmpty, &amp;vEmpty, &amp;vEmpty, &amp;vEmpty);
     if (SUCCEEDED(hr))
     {
         IDispatch *pDisp=NULL;
         hr=pBrowser2-&gt;get_Document(&amp;pDisp);   &lt;- This is NULL
         hr=pDisp-&gt;QueryInterface(IID_IHTMLDocument,(void**)&amp;pDoc);
  
     }
  </code></pre>
  
  <p>}</p>
  
  <p>Basically, I need a pointer to a IHtmlDocument2 structure, because on this pointer I can make 'get_script' and 'invoke'. Do you know how to achieve this, or what I'm doing wrong?</p>
  
  <p>Also do you know another method to run a JS function without MFC and GUI?</p>
  
  <p>Thank you,</p>
  
  <p>P.S Using MFC, I'm able to run JS functions using <a href="http://www.codeproject.com/Articles/2352/JavaScript-call-from-C" rel="nofollow">http://www.codeproject.com/Articles/2352/JavaScript-call-from-C</a></p>