Announcement

Collapse
No announcement yet.

Synchronous browser operation

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Synchronous browser operation

    I just discovered this piece of code posted by Cliff Nicholls in 2014

    Code:
     #COMPILE EXE
    #DIM ALL
    #INCLUDE ONCE "Win32Api.inc"  
    SUB OpenWebPage(PageToOpen AS STRING)    
    ShellExecute(BYVAL %NULL, "open", PageToOpen + CHR$(0), BYVAL %NULL, BYVAL %NULL,%SW_SHOWNORMAL)
    END SUB  
    FUNCTION PBMAIN () AS LONG      
    OpenWebPage "http://www.powerbasic.com/support/pbforums/"
    END FUNCTION
    It does just what I want to do. Namely open a web page when I may not know the default browser.

    However, it operates asynchronously ie hands control back to the program immediately.

    Is there any easy way that I can change this to acting synchronously? ie not handing control back to the program until the browser program terminates?

    I use PBCC, Windows 10

    Thanks

    Kerry
    [I]I made a coding error once - but fortunately I fixed it before anyone noticed[/I]
    Kerry Farmer

  • #2
    Hi Kerry,


    "SHELL" (as a statement) should do what you want.


    JK

    Comment


    • #3
      Originally posted by Juergen Kuehlwein View Post

      "SHELL" (as a statement) should do what you want.
      Nope, but thanks for trying

      Shell gets me error 53

      Code:
      FUNCTION PBMAIN () AS LONG
           SHELL "http://www.powerbasic.com/support/pbforums/"
           PRINT ERR
           PRINT "zz"
           WAITKEY$
      END FUNCTION
      You can SHELL 'browser_name (including path) url' and that works. But I want to do it where I do not know the browser name. Actually, I want to do it on lots of computers where the browser could be any browser.

      [I]I made a coding error once - but fortunately I fixed it before anyone noticed[/I]
      Kerry Farmer

      Comment


      • #4
        Looks to me like you had SHELL Statement in post 3. So that's not it.

        What is the default program (on your PC) for file type .htm, .html. etc (web types)? For example on my PC opening a .htm file or URL opens it in IE without specifying IE first. Opening a .txt uses NotePad, etc, etc.

        Cheers,
        Dale

        Comment


        • #5
          Just realized your URL is incomplete. A browser (or the server) might add index.htm invisibly, Windows won't.

          Cheers,
          Dale

          Comment


          • #6
            Is there any easy way that I can change this to acting synchronously? ie not handing control back to the program until the browser program terminates?

            Easy? Define 'Easy!' And how about asking for a "good" way? Or an "accepted" way? Or maybe even "a way which offers more control?"

            Sheesh, everyone wants either "easy", "fast" or "cheap."

            Whatever happened to those who wanted to do things The Right Way?

            That said (off my chest) .... instead of ShellExecute, use ShellExecuteEx() as per this demo: Win 32: Monitor Process with ShellExecuteEx June 22, 2001 (That's pretty new, so you may have missed it). You will have to detect when the launched program ends (WaitForSingleObject()) but at least you will be doing that under full control of your program.

            FWIW, that's a replacement for SHELL, so a properly-constructed SHELL statement or function can be made to work.
            Michael Mattias
            Tal Systems (retired)
            Port Washington WI USA
            [email protected]
            http://www.talsystems.com

            Comment


            • #7
              Originally posted by Michael Mattias View Post
              Sheesh, everyone wants either "easy", "fast" or "cheap."
              I always tell my customers (users at work) -- "you can have Good, Fast, Cheap. Pick two"

              Real programmers use a magnetized needle and a steady hand

              Comment


              • #8
                One way is to use FindExecutable to locate the default application for the file type and then SHELL to it. Here's one way to do that:

                Code:
                FUNCTION GetStandardProgram(fileext AS WSTRING) AS WSTRING
                   LOCAL wszExecName AS WSTRINGZ * %MAX_PATH
                   LOCAL wszFName AS WSTRINGZ * %MAX_PATH
                   LOCAL wszPath AS WSTRINGZ * %MAX_PATH
                
                   LOCAL lngRet AS LONG
                   lngRet = gettemppathW(BYVAL %MAX_PATH,wszPath)
                   wszFName = wszPath & "testfile." & fileext
                   OPEN wszFName FOR OUTPUT AS #1
                   CLOSE #1
                   lngRet =  FindExecutableW(wszFName, BYVAL 0, wszExecName)
                   KILL wszFName
                   SELECT CASE lngRet
                     CASE 2
                         FUNCTION = "Error in GetStandardProgram: " & wszFName & " not found"
                    CASE 3
                         FUNCTION = "Error in GetStandardProgram: Invalid path in " & wszFName
                    CASE 5
                         FUNCTION = "Error in GetStandardProgram: " & wszFName & " cannot be accessed"
                    CASE 8
                         FUNCTION = "Error in GetStandardProgram: Out of memory or Resources"
                    CASE 31
                         FUNCTION = "Error in GetStandardProgram: There is no default executable for " & wszFName
                    CASE ELSE
                         FUNCTION = wszExecName
                    END SELECT
                END FUNCTION

                But see https://blogs.msdn.microsoft.com/old...08-00/?p=93274..

                With that cautionary note, GetStandardProgram("html") does return the correct browser executable for me.

                Comment


                • #9
                  To get the system default browser you could use AssocQueryString and then start the browser with the desirered link.
                  Code:
                  #COMPILE EXE
                  #DIM ALL
                  #INCLUDE ONCE "Shlwapi.inc"
                  
                  FUNCTION PBMAIN
                    ? GetAssociatedExe(".html")
                    WAITKEY$
                  END FUNCTION
                  
                  FUNCTION GetAssociatedExe(fileExt AS STRINGZ) AS STRING
                    LOCAL resultBuffer AS ASCIIZ * %MAX_PATH
                  
                    IF AssocQueryString(%ASSOCF_INIT_IGNOREUNKNOWN, _
                                        %ASSOCSTR_EXECUTABLE, _
                                        fileExt, _
                                        "", _
                                        resultBuffer, _
                                        SIZEOF(resultBuffer)) = %S_OK THEN FUNCTION = resultBuffer
                  END FUNCTION
                  Mikael
                  Last edited by Mikael T; 8 May 2018, 08:25 AM.

                  Comment

                  Working...
                  X