  ! This software IWEB.F90 is part of the Compaq Visual Fortran kit.
  !
  ! 
  ! This software is provided as an example that demonstrates a particular
  ! feature/function of the licensed product. The software is not intended to
  ! provide a complete solution to any application problem but rather is provided
  ! as a teaching mechanism.
  ! 
  ! Users may incorporate any part of this sample into their own source after 
  ! making appropriate changes to make it suitable for the intended application.
  !
  ! FUNCTIONS:
  !
  !	IWEB         - Entry point of console application
  ! Check_Status - Checks the status of a COM operation
  !
  ! This program uses the COM interfaces in SHDOCVW.DLL to allow this
  ! Fortran example to start a Web Browser and direct the browser to
  ! navigate to a specified URL.
  !
  ! The program is invoked with the following command line,
  !	    iweb url
  !	where url is the URL to display.
  !
  ! Any arguments not provided on the command line will be prompted for.
  !

  !****************************************************************************
  !
  !  PROGRAM: IWEB
  !
  !  PURPOSE:  Entry point for the console application.
  !
  !****************************************************************************

	program IWEB

	  USE DFLIB
	  USE DFWIN
	  USE DFCOMTY
	  USE DFCOM
	  USE DFAUTO
	  USE IWEBOBJS
	  USE IE

 	  implicit none

	! Command line arguments
	  CHARACTER (LEN = MAX_PATH) :: url

	! Variables
	  INTEGER*4 status
	  INTEGER*4 bstr

	  TYPE (VARIANT) :: vURL	
	
   	  TYPE (GUID), PARAMETER :: CLSID_IE = &
			GUID(#0002DF01, #0000, #0000, &
			  CHAR('C0'X)//CHAR('00'X)//CHAR('00'X)//CHAR('00'X)// &
			  CHAR('00'X)//CHAR('00'X)//CHAR('00'X)//CHAR('46'X))

	! Body of IWEB

	! Determine if program arguments have been specified on the command line
	! If not, prompt the user
	  IF (NARGS() > 1) THEN
		  CALL GETARG(1_2, url)
	  ELSE
		  PRINT '(A30)', ' Enter the URL to display:'
		  READ 	(*,*) url
	  END IF

	! Initialize object pointers;  This is necessary in order for the
	! program to release all objects when it is done
	  CALL COMINITIALIZE(status)
	  CALL INITOBJECTS()

	! Create a Browser object
      CALL COMCREATEOBJECTBYGUID (CLSID_IE, CLSCTX_SERVER, IID_IWebBrowserApp, browser, status)
	  CALL Check_Status(status, " Unable to create Browser object")
 	  status = $IWebBrowserApp_SetVisible(browser, .TRUE.)
      CALL COMQUERYINTERFACE (browser, IID_IWebBrowser2, browser2, status)
	  CALL Check_Status(status, " Unable to get IWebBrowser2 interface")

	! Display the URL
	  CALL VariantInit(vURL)
	  vURL%VT = VT_BSTR
	  bstr = ConvertStringToBSTR(url)
	  vURL%VU%PTR_VAL = bstr
 	  status = $IWebBrowser2_Navigate2(browser2, vURL)
	  CALL Check_Status(status, " Unable to display URL")

	  PAUSE "Press Enter to exit..."

	! Release all bstrs & objects
	  status = VariantClear(vURL)
	  CALL RELEASEOBJECTS()
	  CALL COMUNINITIALIZE()
	
	end program IWEB


	SUBROUTINE Check_Status(olestatus, errorMsg)
	  USE IWEBOBJS
      IMPLICIT NONE                       

	  INTEGER*4 olestatus
	  CHARACTER (LEN = *) :: errorMsg

	  IF (olestatus >= 0) THEN
		  RETURN
	  END IF

	! Error handling code
	  CALL RELEASEOBJECTS()
	  WRITE (*, '(A, "; OLE error status = 0x", Z8.8, "; Aborting")') TRIM(errorMsg), olestatus
	  CALL SLEEPQQ(5000)
	  CALL EXIT(-1)

    END SUBROUTINE
