Reputation: 11
I found it a little tricky that some compiler can't run the <windows.h>
header file. Is it possible to open an URL in C++ without using <windows.h>
?
Here's my code, but using <windows.h>
#include <windows.h>
#include <shellapi.h>
using namespace std;
int main(){
ShellExecute(NULL,NULL,"https://ssur.cc/Easy-Way-To-Open-URL-In-CPP",NULL,NULL,SW_SHOW );
return 0;
}
Upvotes: 0
Views: 380
Reputation: 8284
You could just forward declare the function in question and let the linker handle it.
But if your compiler has issues with windows.h it will probably not support all Windows platforms/architectures. But assuming the calling-convention used by the compiler just happens to match you can do something like this:
typedef void* M_HINSTANCE;
typedef void* M_HWND;
#define M_SW_SHOW 5
// should be __stdcall , but if your compiler has trouble with windows.h, then it will probably also have trouble with __stdcall
extern "C" M_HINSTANCE ShellExecuteA(M_HWND hwnd,
const char* lpOperation,
const char* lpFile,
const char* lpParameters,const char* lpDirectory,
int nShowCmd
);
int main(){
ShellExecuteA(0,0,"https://ssur.cc/Easy-Way-To-Open-URL-In-CPP",0,0,M_SW_SHOW );
return 0;
}
You will still need to link to Shell32.lib
and like I mentioned you might get linker errors on some platforms for mismatching calling conventions.
Alternatively you can try to start the shell as a generic process like
#include <cstdlib>
int main(){
std::system("cmd.exe /c start https://ssur.cc/Easy-Way-To-Open-URL-In-CPP");
// or something like:
// std::system("explorer.exe https://ssur.cc/Easy-Way-To-Open-URL-In-CPP");
// or on some other non-windows platforms:
// std::system("open https://ssur.cc/Easy-Way-To-Open-URL-In-CPP");
return 0;
}
Upvotes: 1