Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
wutil.h
Go to the documentation of this file.
1/* Copyright (C) 2023 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"
36
37template<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
78extern void* wutil_Allocate(size_t size);
79extern 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
91 NUM_CS
92};
93
94extern void wutil_Lock(WinLockId id);
95extern void wutil_Unlock(WinLockId id);
96
97// used in a desperate attempt to avoid deadlock in wseh.
98extern bool wutil_IsLocked(WinLockId id);
99
101{
102public:
104 : m_id(id)
105 {
107 }
108
110 {
112 }
113
114private:
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{
129public:
131 : m_lastError(GetLastError())
132 {
133 SetLastError(0);
134 }
135
137 {
138 if(m_lastError != 0 && GetLastError() == 0)
139 SetLastError(m_lastError);
140 }
141
142private:
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 **/
153extern Status StatusFromWin();
154
155
156//-----------------------------------------------------------------------------
157// directories
158
162
163
164//-----------------------------------------------------------------------------
165
166Status wutil_SetPrivilege(const wchar_t* privilege, bool enable);
167
168/**
169 * @return module handle of lib code (that of the main EXE if
170 * linked statically, otherwise the DLL).
171 * this is necessary for the error dialog.
172 **/
173extern HMODULE wutil_LibModuleHandle();
174
175
176/**
177 * @return handle to the first window owned by the current process, or
178 * 0 if none exist (e.g. it hasn't yet created one).
179 *
180 * enumerates all top-level windows and stops if PID matches.
181 * once this function returns a non-NULL handle, it will always
182 * return that cached value.
183 **/
184extern HWND wutil_AppWindow();
185
186extern void wutil_SetAppWindow(void* hwnd);
187
188extern void wutil_EnableHiDPIOnWindows();
189
190#endif // #ifndef INCLUDED_WUTIL
Definition: path.h:80
Definition: wutil.h:101
~WinScopedLock()
Definition: wutil.h:109
WinScopedLock(WinLockId id)
Definition: wutil.h:103
WinLockId m_id
Definition: wutil.h:115
some WinAPI functions SetLastError(0) on success, which is bad because it can hide previous errors.
Definition: wutil.h:128
WinScopedPreserveLastError()
Definition: wutil.h:130
DWORD m_lastError
Definition: wutil.h:143
~WinScopedPreserveLastError()
Definition: wutil.h:136
i64 Status
Error handling system.
Definition: status.h:173
void wutil_EnableHiDPIOnWindows()
Definition: wutil.cpp:277
WinLockId
Definition: wutil.h:87
@ NUM_CS
Definition: wutil.h:91
@ WDIR_WATCH_CS
Definition: wutil.h:89
@ WDBG_SYM_CS
Definition: wutil.h:88
bool wutil_IsValidHandle(H h)
Definition: wutil.h:38
void wutil_SetAppWindow(void *hwnd)
Definition: wutil.cpp:251
Status wutil_SetPrivilege(const wchar_t *privilege, bool enable)
Definition: wutil.cpp:197
OsPath wutil_PersonalPath()
Definition: wutil.cpp:189
OsPath wutil_LocalAppdataPath()
Definition: wutil.cpp:177
void wutil_Lock(WinLockId id)
Definition: wutil.cpp:71
Status StatusFromWin()
Definition: wutil.cpp:119
bool wutil_IsLocked(WinLockId id)
Definition: wutil.cpp:85
OsPath wutil_RoamingAppdataPath()
Definition: wutil.cpp:183
HMODULE wutil_LibModuleHandle()
Definition: wutil.cpp:224
void * wutil_Allocate(size_t size)
Definition: wutil.cpp:48
void wutil_Free(void *p)
Definition: wutil.cpp:54
void wutil_Unlock(WinLockId id)
Definition: wutil.cpp:78
HWND wutil_AppWindow()
Definition: wutil.cpp:256