Create .exe which opens a URL

Hi

This is my first posting here in this forum. I hope the formatting is right. Otherwise please let me know and i will do it better the next time :-)

I'm trying to make a simple program which opens a URL when executed (double clicked). I am using mingw from mingw.org on Windows10.

Following is my code which won't compile with the error message saying that the class is unknown.



class Program
{
static void Main(string[] args)
{
System.Diagnostics.Process.Start("https://www.google.com/search?q=test");
}


}



I am a student in information technology and quite a beginner in C and C++. This program in C/C++ which i am trying to code right now is just a small part of much bigger web application which i'm doing as a study work. At the moment i don't have the time to go much deeper into C/C++ but later i would like to. However i like coding and i would like to try to get this done with some help.

If someone could give me some hints on how to do this or let me know about some tutorials dealing with this topic i would be very thankful.

Regards and thanks in advance,
Max
Is that code snippet C#? It sure doesn't look like any C++ code I've ever seen.

Yup, it looks like a VERY old C# code snippet from Code Project:

https://www.codeproject.com/questions/608844/howplustopluscreateplusexeplusfile
Last edited on
The C++ equivalent of this (using Win32, not .NET stuff) would be CreateProcess I think.
https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa
To open a URL using the default browser, use ShellExecute() (both narrow and wide stream versions are available).
https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecutea

1
2
3
4
5
6
7
8
9
#include <windows.h>
#include <shellapi.h>

#pragma comment(lib, "Shell32")

int main()
{
  ShellExecuteA( NULL, NULL, "http://www.cplusplus.com", NULL, NULL, SW_SHOWNORMAL );
}

The function returns > 32 if the browser successfully starts; <= 32 otherwise.
This function gives no feedback about the success or failure of loading the named website.

Enjoy!
Thanks so much guys, special thanks to Duthomhas for the ready to use and working code!!

Max
Love your username, BTW. :O)
Registered users can post here. Sign in or register to post.