Author Topic: Opening a Website from C++  (Read 6414 times)

0 Members and 1 Guest are viewing this topic.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Opening a Website from C++
« 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 :)

Offline ben_g

  • Hey cool I can set a custom title now :)
  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +125/-4
  • Asm noob
    • View Profile
    • Our programmer's team: GameCommandoSquad
Re: Opening a Website from C++
« Reply #1 on: May 14, 2011, 04:48:43 pm »
Maybe it's the easyest to detect the platform, and make a function for every platform
My projects
 - The Lost Survivors (Unreal Engine) ACTIVE [GameCommandoSquad main project]
 - Oxo, with single-calc multiplayer and AI (axe) RELEASED (screenshot) (topic)
 - An android version of oxo (java)  ACTIVE
 - A 3D collision detection library (axe) RELEASED! (topic)(screenshot)(more recent screenshot)(screenshot of it being used in a tilemapper)
Spoiler For inactive:
- A first person shooter with a polygon-based 3d engine. (z80, will probably be recoded in axe using GLib) ON HOLD (screenshot)
 - A java MORPG. (pc) DEEP COMA(read more)(screenshot)
 - a minecraft game in axe DEAD (source code available)
 - a 3D racing game (axe) ON HOLD (outdated screenshot of asm version)

This signature was last updated on 20/04/2015 and may be outdated

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Opening a Website from C++
« Reply #2 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++.

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Re: Opening a Website from C++
« Reply #3 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.




Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Opening a Website from C++
« Reply #4 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?

Ashbad

  • Guest
Re: Opening a Website from C++
« Reply #5 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);
}

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Opening a Website from C++
« Reply #6 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?

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Opening a Website from C++
« Reply #7 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)

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Opening a Website from C++
« Reply #8 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))

Offline alberthrocks

  • Moderator
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 876
  • Rating: +103/-10
    • View Profile
Re: Opening a Website from C++
« Reply #9 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...
« Last Edit: May 14, 2011, 07:24:52 pm by alberthrocks »
Withgusto Networks Founder and Administrator
Main Server Status: http://withg.org/status/
Backup Server Status: Not available
Backup 2/MC Server Status: http://mc.withg.org/status/


Proud member of ClrHome!

Miss my old signature? Here it is!
Spoiler For Signature:
Alternate "New" IRC post notification bot (Newy) down? Go here to reset it! http://withg.org/albert/cpuhero/

Withgusto Networks Founder and Administrator
Main Server Status: http://withg.org/status/
Backup Server Status: Not available
Backup 2/MC Server Status: http://mc.withg.org/status/

Activity remains limited due to busyness from school et al. Sorry! :( Feel free to PM, email, or if you know me well enough, FB me if you have a question/concern. :)

Don't expect me to be online 24/7 until summer. Contact me via FB if you feel it's urgent.


Proud member of ClrHome!

Spoiler For "My Projects! :D":
Projects:

Computer/Web/IRC Projects:
C______c: 0% done (Doing planning and trying to not forget it :P)
A_____m: 40% done (Need to develop a sophisticated process queue, and a pretty web GUI)
AtomBot v3.0: 0% done (Planning stage, may do a litmus test of developer wants in the future)
IdeaFrenzy: 0% done (Planning and trying to not forget it :P)
wxWabbitemu: 40% done (NEED MOAR FEATURES :P)

Calculator Projects:
M__ C_____ (an A____ _____ clone): 0% done (Need to figure out physics and Axe)
C2I: 0% done (planning, checking the demand for it, and dreaming :P)

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Opening a Website from C++
« Reply #10 on: May 15, 2011, 03:47:36 am »
Woah, the webbrowser module is in Python? Had no idea.

Offline broooom

  • LV2 Member (Next: 40)
  • **
  • Posts: 28
  • Rating: +2/-0
    • View Profile
Re: Opening a Website from C++
« Reply #11 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.
« Last Edit: May 15, 2011, 07:38:44 am by broooom »

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Opening a Website from C++
« Reply #12 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.

Offline broooom

  • LV2 Member (Next: 40)
  • **
  • Posts: 28
  • Rating: +2/-0
    • View Profile
Re: Opening a Website from C++
« Reply #13 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.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Opening a Website from C++
« Reply #14 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.