Omnimaga

General Discussion => Technology and Development => Computer Programming => Topic started by: Munchor on May 14, 2011, 02:57:47 pm

Title: Opening a Website from C++
Post by: Munchor on May 14, 2011, 02:57:47 pm
I was wondering how you open a website in C++. I need it to be cross-platform, no "windows.h" please.

Thanks :)
Title: Re: Opening a Website from C++
Post by: ben_g on May 14, 2011, 04:48:43 pm
Maybe it's the easyest to detect the platform, and make a function for every platform
Title: Re: Opening a Website from C++
Post by: Munchor on May 14, 2011, 04:56:45 pm
Maybe it's the easyest to detect the platform, and make a function for every platform

Well, there are lots of platforms (Windows, Linux, Mac, Solaris) and I hope there's a way to do this.

In Python, webbrowser.open("url") will open in the default browser on any platform so I guess there is a way to do that in C/C++.
Title: Re: Opening a Website from C++
Post by: Deep Toaster on May 14, 2011, 05:01:31 pm
That's because Python gets interpreted the same way, remember. C++ is compiled to assembly, which is different on each OS. I don't think there's a real cross-platform way to do it. Since you're going to have to release separate versions for each platform anyway (you can't release an .exe for Mac users, for instance) what about keeping different versions for each? I think that's what most devvers do.

I don't know C++ though, so I might be wrong.
Title: Re: Opening a Website from C++
Post by: Munchor on May 14, 2011, 05:15:51 pm
I guess I'll check that later, it's not a priority at the moment, but I hope there's a library or something.

Anybody has had to do this before?
Title: Re: Opening a Website from C++
Post by: Ashbad on May 14, 2011, 06:07:51 pm
just make for windows or Linux for now, and later do it hardcore by using '#ifdef', '#elseif', '#endif' expressions to keep it as one source file ;)

And, for start, windows:

Code: [Select]
#include <windows.h>

using namespace std;

void main()
{
   ShellExecute(NULL, "open", "http://www.thebest404pageever.com/swf/index.php", NULL, NULL, SW_SHOWNORMAL);
}
Title: Re: Opening a Website from C++
Post by: Munchor on May 14, 2011, 06:27:47 pm
Well I really need a Linux one then because the Windows one is easily found with Google. Who knows how to or had better success searching?
Title: Re: Opening a Website from C++
Post by: Jim Bauwens on May 14, 2011, 06:38:46 pm
You need to execute xdg-open http://example.com . I think you can do this with os.system (or whatever is used to run shell commands)
Title: Re: Opening a Website from C++
Post by: Munchor on May 14, 2011, 06:44:19 pm
Thanks a lot jimbauwen :D I'll find the shell executing code for C++ (os.system() is Python :P))
Title: Re: Opening a Website from C++
Post by: alberthrocks on May 14, 2011, 07:24:44 pm
Maybe it's the easyest to detect the platform, and make a function for every platform

Well, there are lots of platforms (Windows, Linux, Mac, Solaris) and I hope there's a way to do this.

In Python, webbrowser.open("url") will open in the default browser on any platform so I guess there is a way to do that in C/C++.
That's because Python gets interpreted the same way, remember. C++ is compiled to assembly, which is different on each OS. I don't think there's a real cross-platform way to do it. Since you're going to have to release separate versions for each platform anyway (you can't release an .exe for Mac users, for instance) what about keeping different versions for each? I think that's what most devvers do.

I don't know C++ though, so I might be wrong.

Python's webbrowser module is in fact, pure source.
http://hg.python.org/cpython/file/d71476b9a55d/Lib/webbrowser.py

Technically, if you translated all of this source code (by HAND, not by program, since there is no such thing as automated Python => C++), you could probably achieve a Python webbrowser-like library for inclusion into your program. :) Of course, translating (and possibly writing a LOT of code) is a whole different thing...
Title: Re: Opening a Website from C++
Post by: Munchor on May 15, 2011, 03:47:36 am
Woah, the webbrowser module is in Python? Had no idea.
Title: Re: Opening a Website from C++
Post by: broooom on May 15, 2011, 07:27:31 am
You can use cURL. Or just use sockets to write your own request over HTTP.

just make for windows or Linux for now, and later do it hardcore by using '#ifdef', '#elseif', '#endif' expressions to keep it as one source file ;)

And, for start, windows:

Code: [Select]
#include <windows.h>

using namespace std;

void main()
{
   ShellExecute(NULL, "open", "http://www.thebest404pageever.com/swf/index.php", NULL, NULL, SW_SHOWNORMAL);
}
Sorry to say, but that is just a horrible way. Also, void main()? I'm not even sure most compilers will accept that.
Title: Re: Opening a Website from C++
Post by: Munchor on May 15, 2011, 07:28:41 am
I managed to open it, but only works in Linux. I found a Windows-only way too and can probably find a Mac too. Maybe I'll go with try blocks I don't know because it's impossible to find out the user OS during runtime.
Title: Re: Opening a Website from C++
Post by: broooom on May 15, 2011, 07:39:14 am
I managed to open it, but only works in Linux. I found a Windows-only way too and can probably find a Mac too. Maybe I'll go with try blocks I don't know because it's impossible to find out the user OS during runtime.
You'll have to compile seperately anyway, so it'll be known at compile time.
Title: Re: Opening a Website from C++
Post by: Munchor on May 15, 2011, 07:40:04 am
I managed to open it, but only works in Linux. I found a Windows-only way too and can probably find a Mac too. Maybe I'll go with try blocks I don't know because it's impossible to find out the user OS during runtime.
You'll have to compile seperately anyway, so it'll be known at compile time.

Yeah I figured out I'll have to make separate releases.