End Google Ads 201810 - BS.net 01 --> I'm trying to automate IE. That piece of code works fine on my VC++ desktop computer with vista and IE8. The problem is it's not working on my laptop (vista + IE7). There is no error message. According to the messageboxes, the code stops when trying to get the READYSTATE statement. I mean I got the first messagebox ("navigation ...") but not the second one ("readystate ..."). Plus the browser doesn't close as it is supposed to do.

I don't know if it's related but I compiled the project using the /MT command and I didn't install the Visual C++ 2008 Redistributable Package (x86) on the laptop. This is because I want to run this code as a single exe.


Here is my code :

#include "stdafx.h"
#include "Exdisp.h"
#include "mshtml.h"
#include "windows.h"
#include "winerror.h"



int _tmain(int argc, _TCHAR* argv[])
{
if (SUCCEEDED(OleInitialize(NULL)))
{
IWebBrowser2* pBrowser2;
IHTMLDocument2* Doc;
IDispatch* pHtmlDispatch;
READYSTATE rState;
DWebBrowserEvents2* pEvents;
HRESULT hr;

CoCreateInstance(CLSID_InternetExplorer, NULL, CLSCTX_LOCAL_SERVER,
IID_IWebBrowser2, (void**)&pBrowser2);
if (pBrowser2)
{

VARIANT vEmpty;
VariantInit(&vEmpty);

BSTR bstrURL = SysAllocString(L"http://www.google.com");

hr = pBrowser2->Navigate(bstrURL, &vEmpty, &vEmpty, &vEmpty, &vEmpty);
if (SUCCEEDED(hr))
{
MessageBox(NULL, TEXT("Navigation successful"),TEXT("Status"),MB_OK);
pBrowser2->put_Visible(VARIANT_TRUE);



do
{
hr = pBrowser2->get_ReadyState(&rState);
}while(hr == S_OK && rState != READYSTATE_COMPLETE);

MessageBox(NULL, TEXT("ReadyState Complete"),TEXT("Status"),MB_OK);
}

SysFreeString(bstrURL);


hr = pBrowser2->get_Document(&pHtmlDispatch);
if (SUCCEEDED(hr) && (pHtmlDispatch != NULL))
{
MessageBox(NULL, TEXT("Dispatch obtained"),TEXT("Status"),MB_OK);
hr = pHtmlDispatch->QueryInterface(IID_IHTMLDocument2, (void**)&Doc);
if (SUCCEEDED (hr) && (Doc != NULL))
{
MessageBox(NULL, TEXT("Document obtained"),TEXT("Status"),MB_OK);





Doc->Release();
}
else
{
return hr;
}
pHtmlDispatch->Release();
}
else
{
return hr;
}


pBrowser2->Quit();
pBrowser2->Release();
}

OleUninitialize();
}

return 0;
}