I want my c++ code to open a url.However all the threads online promote using ShellExecute with an already specified url. My code requires the user to input the url and the program must then open it in a browser so the url is in the form of a string. Much Appreciated.
Asked
Active
Viewed 9,507 times
1
-
2possible duplicate of [Open URL from C++ code](http://stackoverflow.com/questions/17347950/open-url-from-c-code) – MatthiasB Aug 22 '14 at 09:25
-
1Also relevant: [C++ open link with ShellExecute](http://stackoverflow.com/questions/11168646/c-open-link-with-shellexecute). Can you explain the problem with ShellExecute? you can pass the url as parameter to the function, so you can pass a url from an user input. – MatthiasB Aug 22 '14 at 09:29
2 Answers
3
I believe this should to the trick for you, of course, if you are using Windows:
std::cout<<"Enter the link: ";
std::string link;
std::cin>>link;
ShellExecute(NULL, "open", link.c_str(), NULL, NULL, SW_SHOWNORMAL);

bl618515
- 107
- 7
-
That gives an error saying "'HINSTANCE ShellExecuteW(HWND,LPCWSTR,LPCWSTR,LPCWSTR,LPCWSTR,INT)': cannot convert argument 2 from 'const char [5]' to 'LPCWSTR'". It should be `ShellExecute(NULL, L"open", link.c_str(), NULL, NULL, SW_SHOWNORMAL);`. – Donald Duck Sep 12 '16 at 12:44
-
Actually, it should be `_T("open")` if you want it to work with all character formats. Except you'd also need to pass the link as a wide string. Better to use explicit variants `ShellExecuteA` or `ShellExecuteW` as required. – paddy May 27 '19 at 07:23
2
Something like that :
std::string myUrl;
std::cin >> myUrl;
system(std::string("start " + myUrl).c_str());
?

vincentp
- 1,433
- 9
- 12
-
Yes exactly! I was using pointers so I had to make a few changes but this worked! Thanks. – Shannon D'souza Aug 22 '14 at 09:41
-
`system()` opens the console. Is there a way of keeping it from doing that? – Donald Duck Sep 12 '16 at 12:41