Create .exe which opens a URL

Sep 24, 2019 at 9:21pm
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
Sep 24, 2019 at 10:32pm
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 Sep 24, 2019 at 10:34pm
Sep 25, 2019 at 1:14am
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
Sep 25, 2019 at 7:16am
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!
Sep 25, 2019 at 7:26pm
Thanks so much guys, special thanks to Duthomhas for the ready to use and working code!!

Max
Sep 25, 2019 at 8:48pm
Love your username, BTW. :O)
Topic archived. No new replies allowed.