Reputation: 3886
I am not sure if this is possible, but I am trying to open a URL in the users default browser in C++. I would just use 'ShellExecute', but I am trying to make it cross platform. Anyone know any ways that this is possible, if it is?
By cross platform I mean the 3:
I've literally Google search for sooo long, and found nothing. Thanks for any help :)
Upvotes: 1
Views: 783
Reputation: 7302
Your best bet is to use a cross platform library like Qt
or something like that. Qt has a nice class called QDesktopServices
which you can use to do it:
QDesktopServices::openUrl(QUrl("http://google.com", QUrl::TolerantMode));
According to the documentation:
Opens the given url in the appropriate Web browser for the user's desktop environment, and returns true if successful; otherwise returns false.
Do note that this will add an dependency on Qt for all your platforms for what might be a very trivial task. It is best that you use custom code for each platform and set compiler directives to see which operating system you are on and run the browser launching code according to that.
Like, if it is being compiled on windows, you could just compile ShellExecute
function, if it is being run on Linux, then depending on the desktop environment you could run the appropriate command.
But, if you are really making a cross platform application, a dependency like Qt would not be bad as it will help with a lot of cross-platform stuff (like keeping an icon in the system tray, multimedia playback etc.).
Without a library, there is no standard-C++ way to do it that will run on all platforms.
Upvotes: 5