ShellExecute for Linux

Started by
12 comments, last by Turt99 20 years, 4 months ago
I''m looking for a C++ fuction that will work like ShellExecute does on windows, basically what I need to do is open a web page from with in my application, so it would have to open the default browser and go to a specific site with query string.
FOLLOW ME ON TWITTER OR MY BLOG
Advertisement
It''s not going to be that simple. If you application is associated with one of the big desktop environments (Gnome or KDE) it''s pretty easy, as they both have settings to tell you what the default browser is. Debian has an alternatives system that ensures that certain "generic" executables always exist that call the correct program (a couple being: sensible-browser, www-browser, and x-www-browser) but, as far as I know, no other distributions do it quite the same way (but some have similar mechanisms, of course).

So, what most applications end up doing that are not associated with any desktop environment is provide a means for the user to select what browser to use (provide a list of the common ones: mozilla, galeon, epiphany, konqueror, opera, netscape, "sensible-browser" and possibly links and lynx -- cull what doesn''t exist in PATH -- as well as a "custom" enter-whatever-you-want).

If you want to get more fancy, you can link at runtime to the two big desktop environment libraries and use their features after detecting which the user is currently running using some heuristic-type approach and/or detect the various distributions'' sensible-browser type of executables. (This probably wouldn''t be all that hard, but I''ve never tried it; maybe I will later...)

Ouch, at this point that sounds like a whole lot more work then its worth, what I''m trying to do is make it so that my game will automatically send in your highscores, so that they can be posted on the site, for windows that ended up to be pretty easy, just load that page and I can edit the query string..

But if I need them to set up a browser to use, then I need to do something different for each browser (I''m guessing not much different but you didn''t get into that) it seems like just going to the site and entering in the code supplied isn''t too hard..

So from your information it sounds to me that first step would be to figure out the browser to use, then open that browser, I would assume that it would just go to the home page then and I''d have to figure a way to send it a messages to go to a different page..

I''m thinking there is probably a better way of doing this, ie. not opening the site but sending the message another way

FOLLOW ME ON TWITTER OR MY BLOG
quote:Original post by Turt99
So from your information it sounds to me that first step would be to figure out the browser to use, then open that browser, I would assume that it would just go to the home page then and I''d have to figure a way to send it a messages to go to a different page..

Just pass the page as the second parameter (after the executable name) on the command line you start it with and every browser should know to show that page. Once you know the executable you want to use it isn''t that difficult.

If I do get around to writing some robust detector code I''ll post it somewhere for you.

Try using libcurl to do it from within your own code, no browser needed.

http://curl.sourceforge.net/libcurl/

And if you don't like libcurl itself, that page has a link to several other http client libraries.

- dorix

edit: clickety click, Barba trick


[edited by - dorix on December 15, 2003 1:49:34 PM]
I am not sure how common it is, but there seem to be a "standard" environment variable, BROWSER, that might contain the user''s prefered browser.
Okay, got around to writing the beginning of a moderately robust "browser detection" example (and put it here). It checks for the BROWSER environment variable, then checks for gconfd (which is linked and checked for Gnome settings if it''s running), then it checks for distro-specific scripts, and finally just runs checks a list of common browsers. It shouldn''t return any invalid options (NULL if there''s no options), so the first value in the array returned is probably what is the best choice. One could probably add support for KDE and such too, I just didn''t do it.

Rethinking a bit, Dorix is probably correct about doing it in-process if possible. If the webpage is meant to be anything interactive it probably isn''t an easy option though.

CWizard: yeah, I forgot about that (support for it is in the example source I uploaded, like I said). It seems that it''s not set too commonly from my experience; I couldn''t say how few or how many applications pay attention to it though (I know Abiword does, which is why I knew about it, but that''s the only one I can think of off hand; I''m sure there''s more).

Yes I like Dorix option, I think it would cover what I need it to do.. so I might give that a try. I will also take I looks at the browser finding script it sounds interesting..

One major thing that makes me lean towards Dorix option is that its cross platform so I wouldn''t need to do seperate things for the different versions..



FOLLOW ME ON TWITTER OR MY BLOG
quote:Original post by Turt99
Yes I like Dorix option, I think it would cover what I need it to do.. so I might give that a try.
Depending on the nature of your program and this feature, it might be wisest to let a real (and by the user, prefered) browser handle it. See the section Handing off Tasks to Specialist Programs in the really nice book The Art of Unix Programming by ESR.
quote:Original post by CWizard
quote:Original post by Turt99
Yes I like Dorix option, I think it would cover what I need it to do.. so I might give that a try.
Depending on the nature of your program and this feature, it might be wisest to let a real (and by the user, prefered) browser handle it. See the section Handing off Tasks to Specialist Programs in the really nice book The Art of Unix Programming by ESR.


Currently what happens is you get a score code from the game when your game is over, its a jumble of your score and the level and all that, then the user takes this score code browses over to the site and inputs it in, this is working fine, but I just wanted to make it a little easier on the user and a little more secure for me.

So in the windows version I''m making it so that the user will type there name and the game will fill out and submit the form its self (really just sends a query string to emulate filling out the form). After the score is submitted the browser will remain and just say "thanks"

This is good becuase the user doesn''t need to leave the game and there is less chance that they would find a way to submit a fake score..

Thats what I want to do in linux, so I guess in a way your right it might be better not to do it totally hidden.

One question I have, lets just say I know that all the users will be using mozilla, would I be using the system command to run it? or am I doing something like fork and then exec.. I''ve never done either of them so I''m not really sure..

Thanks for all your help

FOLLOW ME ON TWITTER OR MY BLOG

This topic is closed to new replies.

Advertisement