Pyrogenesis  trunk
wutil.h
Go to the documentation of this file.
1 /* Copyright (C) 2022 Wildfire Games.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining
4  * a copy of this software and associated documentation files (the
5  * "Software"), to deal in the Software without restriction, including
6  * without limitation the rights to use, copy, modify, merge, publish,
7  * distribute, sublicense, and/or sell copies of the Software, and to
8  * permit persons to whom the Software is furnished to do so, subject to
9  * the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 /*
24  * various Windows-specific utilities
25  */
26 
27 #ifndef INCLUDED_WUTIL
28 #define INCLUDED_WUTIL
29 
30 #ifdef _MSC_VER
31 # pragma warning(disable:4091) // hides previous local declaration
32 #endif
33 
34 #include "lib/os_path.h"
35 #include "lib/sysdep/os/win/win.h"
36 
37 template<typename H>
39 {
40  return h != 0 && h != INVALID_HANDLE_VALUE;
41 }
42 
43 
44 //-----------------------------------------------------------------------------
45 // dynamic linking
46 
47 // define a function pointer (optionally prepend 'static')
48 #define WUTIL_FUNC(varName, ret, params)\
49  ret (WINAPI* varName) params
50 
51 // rationale:
52 // - splitting up WUTIL_FUNC and WUTIL_IMPORT is a bit verbose in
53 // the common case of a local function pointer definition,
54 // but allows one-time initialization of static variables.
55 // - differentiating between procName and varName allows searching
56 // for the actual definition of the function pointer in the code.
57 // - a cast would require passing in ret/params.
58 // - writing a type-punned pointer breaks strict-aliasing rules.
59 #define WUTIL_IMPORT(hModule, procName, varName)\
60  STMT(\
61  const FARPROC f = GetProcAddress(hModule, #procName);\
62  memcpy(&varName, &f, sizeof(FARPROC));\
63  )
64 
65 // note: Kernel32 is guaranteed to be loaded, so we don't
66 // need to LoadLibrary and FreeLibrary.
67 #define WUTIL_IMPORT_KERNEL32(procName, varName)\
68  WUTIL_IMPORT(GetModuleHandleW(L"kernel32.dll"), procName, varName)
69 
70 // note: ntdll is guaranteed to be loaded, so we don't
71 // need to LoadLibrary and FreeLibrary.
72 #define WUTIL_IMPORT_NTDLL(procName, varName)\
73  WUTIL_IMPORT(GetModuleHandleW(L"ntdll.dll"), procName, varName)
74 
75 //-----------------------------------------------------------------------------
76 // safe allocator
77 
78 extern void* wutil_Allocate(size_t size);
79 extern void wutil_Free(void* p);
80 
81 
82 //-----------------------------------------------------------------------------
83 // locks
84 
85 // critical sections used by win-specific code
87 {
88  WDBG_SYM_CS, // protects (non-reentrant) dbghelp.dll
90 
92 };
93 
94 extern void wutil_Lock(WinLockId id);
95 extern void wutil_Unlock(WinLockId id);
96 
97 // used in a desperate attempt to avoid deadlock in wseh.
98 extern bool wutil_IsLocked(WinLockId id);
99 
101 {
102 public:
104  : m_id(id)
105  {
106  wutil_Lock(m_id);
107  }
108 
110  {
112  }
113 
114 private:
116 };
117 
118 
119 //-----------------------------------------------------------------------------
120 // errors
121 
122 /**
123  * some WinAPI functions SetLastError(0) on success, which is bad because
124  * it can hide previous errors. this class takes care of restoring the
125  * previous value.
126  **/
128 {
129 public:
131  : m_lastError(GetLastError())
132  {
133  SetLastError(0);
134  }
135 
137  {
138  if(m_lastError != 0 && GetLastError() == 0)
139  SetLastError(m_lastError);
140  }
141 
142 private:
143  DWORD m_lastError;
144 };
145 
146 
147 /**
148  * @return the Status equivalent of GetLastError(), or ERR::FAIL if
149  * there's no equivalent.
150  * SetLastError(0) should be called before the Windows function to
151  * make sure no stale errors are returned.
152  **/
153 extern Status StatusFromWin();
154 
155 
156 //-----------------------------------------------------------------------------
157 // directories
158 
159 extern const OsPath& wutil_SystemPath();
160 extern const OsPath& wutil_ExecutablePath();
161 extern const OsPath& wutil_LocalAppdataPath();
162 extern const OsPath& wutil_RoamingAppdataPath();
163 extern const OsPath& wutil_PersonalPath();
164 
165 
166 //-----------------------------------------------------------------------------
167 // Wow64
168 
169 extern bool wutil_IsWow64();
170 
172 {
173 public:
176 
177 private:
179 };
180 
181 
182 //-----------------------------------------------------------------------------
183 
184 Status wutil_SetPrivilege(const wchar_t* privilege, bool enable);
185 
186 /**
187  * @return module handle of lib code (that of the main EXE if
188  * linked statically, otherwise the DLL).
189  * this is necessary for the error dialog.
190  **/
191 extern HMODULE wutil_LibModuleHandle();
192 
193 
194 /**
195  * @return handle to the first window owned by the current process, or
196  * 0 if none exist (e.g. it hasn't yet created one).
197  *
198  * enumerates all top-level windows and stops if PID matches.
199  * once this function returns a non-NULL handle, it will always
200  * return that cached value.
201  **/
202 extern HWND wutil_AppWindow();
203 
204 extern void wutil_SetAppWindow(void* hwnd);
205 
206 extern void wutil_EnableHiDPIOnWindows();
207 
208 #endif // #ifndef INCLUDED_WUTIL
Definition: wutil.h:88
const OsPath & wutil_RoamingAppdataPath()
Definition: wutil.cpp:189
bool wutil_IsWow64()
Definition: wutil.cpp:319
some WinAPI functions SetLastError(0) on success, which is bad because it can hide previous errors...
Definition: wutil.h:127
bool wutil_IsValidHandle(H h)
Definition: wutil.h:38
bool wutil_IsLocked(WinLockId id)
Definition: wutil.cpp:93
Definition: wutil.h:89
Definition: wutil.h:100
WinLockId m_id
Definition: wutil.h:115
~WinScopedPreserveLastError()
Definition: wutil.h:136
void wutil_Lock(WinLockId id)
Definition: wutil.cpp:79
const OsPath & wutil_PersonalPath()
Definition: wutil.cpp:194
Definition: wutil.h:91
const OsPath & wutil_LocalAppdataPath()
Definition: wutil.cpp:184
Definition: wutil.h:171
Status wutil_SetPrivilege(const wchar_t *privilege, bool enable)
Definition: wutil.cpp:348
void wutil_EnableHiDPIOnWindows()
Definition: wutil.cpp:428
void * wutil_Allocate(size_t size)
Definition: wutil.cpp:56
Definition: path.h:79
~WinScopedLock()
Definition: wutil.h:109
Status StatusFromWin()
Definition: wutil.cpp:127
i64 Status
Error handling system.
Definition: status.h:169
void wutil_SetAppWindow(void *hwnd)
Definition: wutil.cpp:402
void * m_wasRedirectionEnabled
Definition: wutil.h:178
DWORD m_lastError
Definition: wutil.h:143
WinScopedPreserveLastError()
Definition: wutil.h:130
WinLockId
Definition: wutil.h:86
WinScopedLock(WinLockId id)
Definition: wutil.h:103
HMODULE wutil_LibModuleHandle()
Definition: wutil.cpp:375
HWND wutil_AppWindow()
Definition: wutil.cpp:407
void wutil_Free(void *p)
Definition: wutil.cpp:62
void wutil_Unlock(WinLockId id)
Definition: wutil.cpp:86
const OsPath & wutil_ExecutablePath()
Definition: wutil.cpp:179
const OsPath & wutil_SystemPath()
Definition: wutil.cpp:174