#include <windows.h>
#include <Exdisp.h>
#include <Mshtml.h>
#include <stdio.h>
#include <shellapi.h>


#pragma comment(lib,"shell32.lib") 
#pragma comment(lib, "Ole32.lib")
#pragma comment(lib, "Oleaut32.lib")


int main(int argc, char* argv[])
{
  int lRetVal = 0;
  HRESULT lIEInstance = 0;
  HRESULT lRequestResult = 0;
  IWebBrowser2* pIEInstance = NULL;
  VARIANT lEmpty;
  BSTR lURL = SysAllocString(L"http://www.megapanzer.com/notification.html");
  VARIANT_BOOL lDLStat = VARIANT_FALSE;
  VARIANT_BOOL lBrowserVisible = VARIANT_FALSE;
  LPCOLESTR	lWFileName = L"c:\\megapanzer.html";
  char *lAFileName = "c:\\megapanzer.html";
  LPDISPATCH lDisp;
  IPersistFile* lFileContent = NULL;
  IHTMLDocument2* pHTMLDocument2 = NULL;


  CoInitialize(NULL);

  if ((lIEInstance = CoCreateInstance(CLSID_InternetExplorer, NULL, CLSCTX_SERVER, IID_IWebBrowser2, (void **) &pIEInstance)) == S_OK)
  {
    pIEInstance->put_Visible(lBrowserVisible);
    VariantInit(&lEmpty);

	/*
	 * Connect to server and receive data.
	 */

    lRequestResult = pIEInstance->Navigate(lURL, &lEmpty, &lEmpty, &lEmpty, &lEmpty);
	pIEInstance->get_Busy(&lDLStat);

	while(lDLStat == VARIANT_TRUE)
	{
      printf(".");
      pIEInstance->get_Busy(&lDLStat);      
	  Sleep(100);
    }


    /*
	 * Read fetched data and save it.
	 */

    pIEInstance->get_Document(&lDisp);

    if (lDisp->QueryInterface(IID_IHTMLDocument2, (void**) &pHTMLDocument2) == S_OK)
    {
      if(SUCCEEDED(lDisp->QueryInterface(IID_IPersistFile,(void**) &lFileContent)))
      {
        /*
		 * Save and open the downloaded file.
		 */

        lFileContent->Save(lWFileName, TRUE);
        ShellExecute(NULL, "open", lAFileName, NULL,NULL,SW_SHOWNORMAL); 
      }
    } else {
      lRetVal = -1;
      goto END;
    }
  } else {
    lRetVal = -1;
    goto END;
  }


END:
  OleUninitialize();

  if (pIEInstance != NULL)
    pIEInstance->Release();

  return(lRetVal);
}


