Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

open an URL in a fortran program

jmloriot
Beginner
1,440 Views

When executing, my fortran program needs to automatically popup an URL

 

How can I do that ?

 

0 Kudos
1 Solution
Steve_Lionel
Honored Contributor III
1,402 Views

This is a fun one. Notice that the above program declares a function with two optional character arguments and that the function has BIND(C). If one doesn't pass an optional argument, the compiler incorrectly passes hidden length values of zero, despite BIND(C) prohibiting hidden arguments.

In most cases, you would not be able to see the effect of this, but on 32-bit Windows and a STDCALL convention, the erroneous hidden arguments change the name decoration leading to link failures. Reported as Intel ticket 05752641.

I've attached a modified version that works in both 32-bit and 64-bit, and also with IFX. 

View solution in original post

0 Kudos
5 Replies
Steve_Lionel
Honored Contributor III
1,421 Views

You can open a local HTML file, using the user's default browser, with:

call EXECUTE_COMMAND_LINE ("helloworld.html")

This could contain a redirect to a URL. The following Windows program will open the user's default browser to a URL:

    program Browser
    use, intrinsic :: iso_c_binding
    use :: ifwinty

    implicit none

    ! Windows API function AssocQueryString from SHLWAPI.LIB
    !DIR$ OBJCOMMENT LIB:"SHLWAPI.LIB"

    enum, bind(C) ! ASSOCSTR
        enumerator :: ASSOCSTR_COMMAND = 1
        enumerator :: ASSOCSTR_EXECUTABLE
        enumerator :: ASSOCSTR_FRIENDLYDOCNAME
        enumerator :: ASSOCSTR_FRIENDLYAPPNAME
        enumerator :: ASSOCSTR_NOOPEN
        enumerator :: ASSOCSTR_SHELLNEWVALUE
        enumerator :: ASSOCSTR_DDECOMMAND
        enumerator :: ASSOCSTR_DDEIFEXEC
        enumerator :: ASSOCSTR_DDEAPPLICATION
        enumerator :: ASSOCSTR_DDETOPIC
        enumerator :: ASSOCSTR_INFOTIP
        enumerator :: ASSOCSTR_QUICKTIP
        enumerator :: ASSOCSTR_TILEINFO
        enumerator :: ASSOCSTR_CONTENTTYPE
        enumerator :: ASSOCSTR_DEFAULTICON
        enumerator :: ASSOCSTR_SHELLEXTENSION
        enumerator :: ASSOCSTR_DROPTARGET
        enumerator :: ASSOCSTR_DELEGATEEXECUTE
        enumerator :: ASSOCSTR_SUPPORTED_URI_PROTOCOLS
        enumerator :: ASSOCSTR_PROGID
        enumerator :: ASSOCSTR_APPID
        enumerator :: ASSOCSTR_APPPUBLISHER
        enumerator :: ASSOCSTR_APPICONREFERENCE
        enumerator :: ASSOCSTR_MAX
    end enum
    
    interface
        function AssocQueryString (flags,str,pszAssoc,pszExtra,pszOut,pcchOut) bind(C,name="AssocQueryStringA")
        import
        !DIR$ ATTRIBUTES STDCALL :: AssocQueryString
        integer(HANDLE) :: AssocQueryString
        integer(ENUM), intent(IN), value :: flags !ASSOCF not used here
        integer(ENUM), intent(IN), value :: str ! ASSOCSTR
        character, dimension(*), intent(IN) :: pszAssoc
        character, dimension(*), intent(IN), optional :: pszExtra
        character, dimension(*), intent(OUT), optional :: pszOut
        integer(DWORD), intent(INOUT) :: pcchOut
        end function AssocQueryString
    end interface
    
    character(:), allocatable :: browserpath
    integer(DWORD) :: pathlen
    integer(HANDLE) :: ret
        
    ! Get the length of the browser path
    ret = AssocQueryString (0,ASSOCSTR_EXECUTABLE,".html"//C_NULL_CHAR,pcchOut=pathlen)
    if (ret /= S_FALSE) then
        print *, "Could not get path len. Status=",ret
        error stop
    end if
    allocate(character(pathlen)::browserpath)
    ! Get the path
    ret = AssocQueryString (0,ASSOCSTR_EXECUTABLE,".html"//C_NULL_CHAR,pszOut=browserpath,pcchOut=pathlen)
    if (ret /= S_OK) then
        print *, "Could not get path. Status=",ret
        error stop
    end if
    
    call EXECUTE_COMMAND_LINE ('"'//browserpath(1:pathlen-1)//'" https://example.com')
    
    end program Browser
Steve_Lionel
Honored Contributor III
1,413 Views

Argh - a compiler bug prevents the program from linking in 32-bit - it works in 64-bit. I'm putting together a simple reproducer for support.

0 Kudos
Steve_Lionel
Honored Contributor III
1,403 Views

This is a fun one. Notice that the above program declares a function with two optional character arguments and that the function has BIND(C). If one doesn't pass an optional argument, the compiler incorrectly passes hidden length values of zero, despite BIND(C) prohibiting hidden arguments.

In most cases, you would not be able to see the effect of this, but on 32-bit Windows and a STDCALL convention, the erroneous hidden arguments change the name decoration leading to link failures. Reported as Intel ticket 05752641.

I've attached a modified version that works in both 32-bit and 64-bit, and also with IFX. 

0 Kudos
jmloriot
Beginner
1,295 Views

Thank you very much Steve

excatly what I did need and it works fine

Jean-Marc

0 Kudos
Steve_Lionel
Honored Contributor III
1,268 Views

I'm glad to hear it. Please mark the desired response as an "answer" to your question, as that will help others.

0 Kudos
Reply