Last active
August 1, 2023 18:53
-
-
Save CobaltXII/371ea1a4a0783986ca562de3bd05604e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if defined(_WIN32) | |
#include <combaseapi.h> // CoInitializeEx | |
#include <objbase.h> // COINIT_APARTMENTTHREADED, COINIT_DISABLE_OLE1DDE | |
#include <shellapi.h> // ShellExecute | |
#include <winuser.h> // SW_SHOWNORMAL | |
#elif defined(__APPLE__) | |
#include <CoreFoundation/CFBase.h> // CFRelease | |
#include <CoreFoundation/CFString.h> // kCFStringEncodingASCII | |
#include <CoreFoundation/CFURL.h> // CFURLCreateWithBytes | |
#include <CoreServices/CoreServices.h> // LSOpenCFURLRef | |
#elif defined(EMSCRIPTEN) | |
#include <emscripten/emscripten.h> // EM_ASM_INT | |
#else | |
#include <stdlib.h> // system | |
#include <string.h> // memcpy, strlen | |
#endif | |
bool openLink(std::string url) { | |
#if defined(_WIN32) | |
// https://docs.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-coinitializeex | |
// https://docs.microsoft.com/en-us/windows/win32/api/objbase/ne-objbase-coinit | |
// https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecutea | |
// https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow | |
static bool comInitialized = false; | |
if (!comInitialized) { | |
HRESULT status = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); | |
if (status != S_OK && status != S_FALSE) return false; | |
comInitialized = true; | |
} | |
return ShellExecute(NULL, NULL, (LPCSTR)url.c_str(), NULL, NULL, SW_SHOWNORMAL) > 32; | |
#elif defined(__APPLE__) | |
// https://developer.apple.com/documentation/corefoundation/1542075-cfurlcreatewithbytes?language=objc | |
// https://developer.apple.com/documentation/corefoundation/cfstringbuiltinencodings?language=objc | |
// https://developer.apple.com/documentation/coreservices/1442850-lsopencfurlref?language=objc | |
// https://developer.apple.com/documentation/corefoundation/1521153-cfrelease?language=objc | |
CFURLRef inURL = CFURLCreateWithBytes(NULL, (const UInt8*)url.c_str(), url.length(), kCFStringEncodingASCII, NULL); | |
if (!inURL) return false; | |
OSStatus status = LSOpenCFURLRef(inURL, NULL); | |
CFRelease(inURL); | |
return !status; | |
#elif defined(EMSCRIPTEN) | |
// https://developer.mozilla.org/en-US/docs/Web/API/Window/open | |
return EM_ASM_INT({ | |
return window.open(UTF8ToString($0)) !== null; | |
}, url.c_str()); | |
#else | |
// Try your best! | |
// https://man7.org/linux/man-pages/man3/system.3.html | |
const char* prefix = "open "; | |
char* command = new char[url.length() + strlen(prefix) + 1](); | |
memcpy(command, prefix, strlen(prefix)); | |
memcpy(command + strlen(prefix), url.c_str(), url.length()); | |
int status = system(command); | |
return status != -1 && status != 127; | |
#endif | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment