Archived

This topic is now archived and is closed to further replies.

ShellExecute for Linux

This topic is 5112 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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...)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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..



Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
If you are just interested in sending the score to a game server (via HTTP/CGI or PHP?), and not display web content, I think it is better to do it "in-process". You could do this fairly trivially just using socket functions, but using a HTTP library (like libcurl?) might be better.

If all you need is to send a HTTP request, and don''t care about displaying the response, there is no need to invoke a browser at all.

Share this post


Link to post
Share on other sites
quote:
Original post by CWizard
If you are just interested in sending the score to a game server (via HTTP/CGI or PHP?), and not display web content, I think it is better to do it "in-process". You could do this fairly trivially just using socket functions, but using a HTTP library (like libcurl?) might be better.

If all you need is to send a HTTP request, and don''t care about displaying the response, there is no need to invoke a browser at all.


I''ve never used sockets before so I''ll do a little research, if you have any links that would be helpfull please post them..
Just one question do I need to do anything special with the webserver to handle the requests? I''m not hosting the site on my computer so I''m wonder if this would be something I''d need admin access to set up..

Share this post


Link to post
Share on other sites
quote:
Original post by Turt99
I've never used sockets before so I'll do a little research, if you have any links that would be helpfull please post them..
They wouldn't be hard to find, but if you haven't used them before, it's probably not the best reason to start. However, here's one: Beej's Guide to Network Programming
quote:
Just one question do I need to do anything special with the webserver to handle the requests? I'm not hosting the site on my computer so I'm wonder if this would be something I'd need admin access to set up..
I don't know how you have it setup now, but probably, no. Assuming you have a HTML "form" it is trivial to do the equivalent programmatically; you just do the same thing as the web browser do. It could be as simple as sending this request to the server:

GET /cgi-bin/submit-score.cgi?player-name=CWizard&score_code=c67556ae54d09e5209beddeacc45b9ef HTTP/1.1
Host: www.your-domain.tld

(you can do the same thing with a telnet client)

Or, if the script needs it to be POSTed, it is a bit more complicated:

POST /cgi-bin/submit-score.cgi? HTTP/1.1
Host: www.your-domain.tld
Content-Type: multipart/form-data; boundary=-----------------------------3866236621304
Content-Length: 200

-----------------------------3866236621304
Content-Disposition: form-data; name="player-name"

CWizard
-----------------------------3866236621304
Content-Disposition: form-data; name="score_code"

c67556ae54d09e5209beddeacc45b9ef
-----------------------------3866236621304

(something like that; I think it's correct)

However, a HTTP library should provide an easy interface to all this.

[edited by - CWizard on December 15, 2003 6:21:19 PM]

Share this post


Link to post
Share on other sites