Announcement

Collapse
No announcement yet.

Open a URL in a web browser

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

  • Open a URL in a web browser

    Hi,

    I want to load a URL in a web browser from my program
    Is this possible in PowerBASIC?

    Other programming languages I have used had a ShowURL command or function
    Brian Heibert
    [email protected]
    http://www.heibertsoftware.com
    http://www.winvocalassist.com
    http://www.heibert.net

  • #2
    You can use the ShellExecute function:

    Code:
    #COMPILE EXE
    #DIM ALL
    
    #INCLUDE "WIN32API.INC"
    
    FUNCTION ShowURL(BYVAL strURL AS STRING) AS INTEGER
        LOCAL pszURL AS ASCIIZ PTR
        LOCAL nResult AS LONG
    
        pszURL = STRPTR(strURL)
        nResult = ShellExecute(%NULL, "open", @pszURL, "", "", %SW_SHOWDEFAULT)
        IF nResult > 32 THEN
            FUNCTION = %TRUE
        ELSE
            FUNCTION = %FALSE
        END IF
    END FUNCTION
        
    FUNCTION PBMAIN () AS LONG
    
        IF ShowURL("http://www.catalyst.com") = %FALSE THEN
            MSGBOX "Unable to open the default web browser", %MB_ICONERROR, "Error"
        END IF
        
    END FUNCTION
    Mike Stefanik
    sockettools.com

    Comment


    • #3
      shell "start http://www.mywebpage.com"

      Comment


      • #4
        Shawn, did you actually try that or just typing off the top of your head? It doesn't work, because "start" is a built-in shell command, not an executable command (this is mentioned in the help). So the code would have to be something like:

        Code:
        SHELL ENVIRON$("COMSPEC") + " /C start http://www.catalyst.com"
        And while it's a single line of code, there's two reasons you don't want to do that. First, it incurs unnecessary overhead by executing the shell to simply execute another program. In other words, you're basically loading the command shell just to call ShellExecute for you. Second, you (briefly) see the command shell window as it loads, starts the browser, and then closes. Ugly, and again, unnecessary to just save yourself from adding 11 lines of code to your program. That really doesn't make a lot of sense, at least from my perspective.

        EDIT: Just as a side note, over the years I've learned a basic lesson: never post code snippets unless you've actually run them through the compiler (or interpreter, as the case may be, depending on the language). I've been bitten enough by typos and simple mistakes in the past that I always do that, just to make sure that what I've posted is correct. Posting bad code, even when the idea is generally okay, just confuses new programmers because they often think that they're doing something wrong. Just my two cents there.
        Last edited by Mike Stefanik; 9 Mar 2008, 12:53 PM.
        Mike Stefanik
        sockettools.com

        Comment


        • #5
          And while it's a single line of code, there's two reasons you don't want to do that....
          Ah, excuse me, but there might be a valid reason TO do this. But does it really matter? What is wrong with posting multiple solutions to a problem? If you have one way, great, post it. But if someone takes a different approach, why tell them its no good?

          Ok, if the code snippet "doesn't work as advertised", I can see posting a comment/correction to make it work. But let's remember that outside of the café, all posts here are freely offered in an attempt to be helpful. Lets show some respect and appreciation for those that take the time to help, ok?
          Software makes Hardware Happen

          Comment


          • #6
            I don't think I was being disrespectful, or at least that's certainly not what I intended. And there's nothing wrong with posting multiple solutions, just as there's nothing wrong pointing out that a particular solution has inherent problems or limitations. If the developer doesn't care about those particular issues with their implementation, that's fine, they have that choice.

            I suppose that one could take umbrage at me commenting that showing a command prompt window is "ugly", but that's just my personal opinion, it wasn't meant to slight Shawn. I apologize if it was taken that way. Perhaps I also should have put a smiley face after my first sentence; in re-reading it, I could see how it could be taken as an accusation, when really I meant it as an "oops, you really didn't try that did ya?" kind of comment. I've done that plenty of times myself in the past.

            Just as a personal observation, if anyone were to point out inefficiencies in code that I posted, I'd be happy for the criticism. I believe that having someone say "No, you really don't want to do it that way, this way is much better" is how we learn and perfect our craft as programmers. "Just getting the job done" shouldn't always be the final criteria with these sorts of things, from my perspective. It's important, of course, but so is elegance, efficiency and even cleverness in finding a solution to a problem (I'm saying that generally; I don't think that using ShellExecute is particularly clever). In any case, Brian has two solutions he can use, depending on whatever he prefers.
            Last edited by Mike Stefanik; 9 Mar 2008, 03:13 PM. Reason: Fixed some typos
            Mike Stefanik
            sockettools.com

            Comment


            • #7
              ShellExecute() is the Windows way. Shell(cmd) is the DOS way, where you let the command interpreter do the work, incurring more overhead and less control. It's also possible to use Shell(cmd) to delete and move files, or get a directory listing, but hardly anybody does because it is archaic, and there are better alternatives. What if M$ decide in their wisdom to remove or restrict the command interpreter [COMSPEC] in the future (a likely outcome)? Then any program calling it will be stuffed.

              The API functions are available and at your disposal. Use them
              kgpsoftware.com | Slam DBMS | PrpT Control | Other Downloads | Contact Me

              Comment


              • #8
                It was just alternate solution but -

                1) it was off the top of my head
                2) as far as the rest of Mike said: he's probably right

                I wasn't offended at all.

                Comment

                Working...
                X