SDL_sysurl.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../SDL_sysurl.h"
  19. #include "../../core/windows/SDL_windows.h"
  20. #include <shellapi.h>
  21. #if defined(__XBOXONE__) || defined(__XBOXSERIES__)
  22. int
  23. SDL_SYS_OpenURL(const char *url)
  24. {
  25. /* Not supported */
  26. return SDL_Unsupported();
  27. }
  28. #else
  29. /* https://msdn.microsoft.com/en-us/library/windows/desktop/bb762153%28v=vs.85%29.aspx */
  30. int
  31. SDL_SYS_OpenURL(const char *url)
  32. {
  33. WCHAR* wurl;
  34. HINSTANCE rc;
  35. /* MSDN says for safety's sake, make sure COM is initialized. */
  36. const HRESULT hr = WIN_CoInitialize();
  37. if (FAILED(hr)) {
  38. return WIN_SetErrorFromHRESULT("CoInitialize failed", hr);
  39. }
  40. wurl = WIN_UTF8ToStringW(url);
  41. if (wurl == NULL) {
  42. WIN_CoUninitialize();
  43. return SDL_OutOfMemory();
  44. }
  45. /* Success returns value greater than 32. Less is an error. */
  46. rc = ShellExecuteW(NULL, L"open", wurl, NULL, NULL, SW_SHOWNORMAL);
  47. SDL_free(wurl);
  48. WIN_CoUninitialize();
  49. return (rc > ((HINSTANCE) 32)) ? 0 : WIN_SetError("Couldn't open given URL.");
  50. }
  51. #endif
  52. /* vi: set ts=4 sw=4 expandtab: */