SDL  2.0
SDL_windows.c
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any damages
7  arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any purpose,
10  including commercial applications, and to alter it and redistribute it
11  freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19  3. This notice may not be removed or altered from any source distribution.
20 */
21 #include "../../SDL_internal.h"
22 
23 #if defined(__WIN32__) || defined(__WINRT__)
24 
25 #include "SDL_windows.h"
26 #include "SDL_error.h"
27 #include "SDL_assert.h"
28 
29 #include <objbase.h> /* for CoInitialize/CoUninitialize (Win32 only) */
30 
31 #ifndef _WIN32_WINNT_VISTA
32 #define _WIN32_WINNT_VISTA 0x0600
33 #endif
34 
35 
36 /* Sets an error message based on GetLastError() */
37 int
38 WIN_SetErrorFromHRESULT(const char *prefix, HRESULT hr)
39 {
40  TCHAR buffer[1024];
41  char *message;
42  FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, hr, 0,
43  buffer, SDL_arraysize(buffer), NULL);
44  message = WIN_StringToUTF8(buffer);
45  SDL_SetError("%s%s%s", prefix ? prefix : "", prefix ? ": " : "", message);
46  SDL_free(message);
47  return -1;
48 }
49 
50 /* Sets an error message based on GetLastError() */
51 int
52 WIN_SetError(const char *prefix)
53 {
54  return WIN_SetErrorFromHRESULT(prefix, GetLastError());
55 }
56 
57 HRESULT
58 WIN_CoInitialize(void)
59 {
60  /* SDL handles any threading model, so initialize with the default, which
61  is compatible with OLE and if that doesn't work, try multi-threaded mode.
62 
63  If you need multi-threaded mode, call CoInitializeEx() before SDL_Init()
64  */
65 #ifdef __WINRT__
66  /* DLudwig: On WinRT, it is assumed that COM was initialized in main().
67  CoInitializeEx is available (not CoInitialize though), however
68  on WinRT, main() is typically declared with the [MTAThread]
69  attribute, which, AFAIK, should initialize COM.
70  */
71  return S_OK;
72 #else
73  HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
74  if (hr == RPC_E_CHANGED_MODE) {
75  hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
76  }
77 
78  /* S_FALSE means success, but someone else already initialized. */
79  /* You still need to call CoUninitialize in this case! */
80  if (hr == S_FALSE) {
81  return S_OK;
82  }
83 
84  return hr;
85 #endif
86 }
87 
88 void
90 {
91 #ifndef __WINRT__
92  CoUninitialize();
93 #endif
94 }
95 
96 #ifndef __WINRT__
97 static BOOL
98 IsWindowsVersionOrGreater(WORD wMajorVersion, WORD wMinorVersion, WORD wServicePackMajor)
99 {
100  OSVERSIONINFOEXW osvi;
101  DWORDLONG const dwlConditionMask = VerSetConditionMask(
102  VerSetConditionMask(
103  VerSetConditionMask(
104  0, VER_MAJORVERSION, VER_GREATER_EQUAL ),
105  VER_MINORVERSION, VER_GREATER_EQUAL ),
106  VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL );
107 
108  SDL_zero(osvi);
109  osvi.dwOSVersionInfoSize = sizeof(osvi);
110  osvi.dwMajorVersion = wMajorVersion;
111  osvi.dwMinorVersion = wMinorVersion;
112  osvi.wServicePackMajor = wServicePackMajor;
113 
114  return VerifyVersionInfoW(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR, dwlConditionMask) != FALSE;
115 }
116 #endif
117 
119 {
120 #ifdef __WINRT__
121  return TRUE;
122 #else
123  return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_VISTA), LOBYTE(_WIN32_WINNT_VISTA), 0);
124 #endif
125 }
126 
127 #endif /* __WIN32__ || __WINRT__ */
128 
129 /* vi: set ts=4 sw=4 expandtab: */
GLuint GLsizei const GLchar * message
int WIN_SetErrorFromHRESULT(const char *prefix, HRESULT hr)
BOOL WIN_IsWindowsVistaOrGreater()
HRESULT WIN_CoInitialize(void)
void SDL_free(void *mem)
#define TRUE
Definition: edid-parse.c:31
#define WIN_StringToUTF8(S)
Definition: SDL_windows.h:45
#define SDL_zero(x)
Definition: SDL_stdinc.h:355
#define S_OK
Definition: SDL_directx.h:47
#define NULL
Definition: begin_code.h:143
void WIN_CoUninitialize(void)
int WIN_SetError(const char *prefix)
#define SDL_SetError
GLuint buffer
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:93
#define FALSE
Definition: edid-parse.c:32