Pyrogenesis  trunk
wutil.h
Go to the documentation of this file.
1 /* Copyright (c) 2012 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 #include "lib/os_path.h"
31 #include "lib/sysdep/os/win/win.h"
32 
33 template<typename H>
35 {
36  return h != 0 && h != INVALID_HANDLE_VALUE;
37 }
38 
39 
40 //-----------------------------------------------------------------------------
41 // dynamic linking
42 
43 // define a function pointer (optionally prepend 'static')
44 #define WUTIL_FUNC(varName, ret, params)\
45  ret (WINAPI* varName) params
46 
47 // rationale:
48 // - splitting up WUTIL_FUNC and WUTIL_IMPORT is a bit verbose in
49 // the common case of a local function pointer definition,
50 // but allows one-time initialization of static variables.
51 // - differentiating between procName and varName allows searching
52 // for the actual definition of the function pointer in the code.
53 // - a cast would require passing in ret/params.
54 // - writing a type-punned pointer breaks strict-aliasing rules.
55 #define WUTIL_IMPORT(hModule, procName, varName)\
56  STMT(\
57  const FARPROC f = GetProcAddress(hModule, #procName);\
58  memcpy(&varName, &f, sizeof(FARPROC));\
59  )
60 
61 // note: Kernel32 is guaranteed to be loaded, so we don't
62 // need to LoadLibrary and FreeLibrary.
63 #define WUTIL_IMPORT_KERNEL32(procName, varName)\
64  WUTIL_IMPORT(GetModuleHandleW(L"kernel32.dll"), procName, varName)
65 
66 
67 //-----------------------------------------------------------------------------
68 // safe allocator
69 
70 extern void* wutil_Allocate(size_t size);
71 extern void wutil_Free(void* p);
72 
73 
74 //-----------------------------------------------------------------------------
75 // locks
76 
77 // critical sections used by win-specific code
79 {
80  WDBG_SYM_CS, // protects (non-reentrant) dbghelp.dll
82 
84 };
85 
86 extern void wutil_Lock(WinLockId id);
87 extern void wutil_Unlock(WinLockId id);
88 
89 // used in a desperate attempt to avoid deadlock in wseh.
90 extern bool wutil_IsLocked(WinLockId id);
91 
93 {
94 public:
96  : m_id(id)
97  {
99  }
100 
102  {
104  }
105 
106 private:
108 };
109 
110 
111 //-----------------------------------------------------------------------------
112 // errors
113 
114 /**
115  * some WinAPI functions SetLastError(0) on success, which is bad because
116  * it can hide previous errors. this class takes care of restoring the
117  * previous value.
118  **/
120 {
121 public:
123  : m_lastError(GetLastError())
124  {
125  SetLastError(0);
126  }
127 
129  {
130  if(m_lastError != 0 && GetLastError() == 0)
131  SetLastError(m_lastError);
132  }
133 
134 private:
136 };
137 
138 
139 /**
140  * @return the Status equivalent of GetLastError(), or ERR::FAIL if
141  * there's no equivalent.
142  * SetLastError(0) should be called before the Windows function to
143  * make sure no stale errors are returned.
144  **/
145 extern Status StatusFromWin();
146 
147 
148 //-----------------------------------------------------------------------------
149 // command line
150 
151 extern int wutil_argc();
152 extern wchar_t** wutil_argv();
153 
154 extern bool wutil_HasCommandLineArgument(const wchar_t* arg);
155 
156 
157 //-----------------------------------------------------------------------------
158 // directories
159 
160 extern const OsPath& wutil_SystemPath();
161 extern const OsPath& wutil_ExecutablePath();
162 extern const OsPath& wutil_LocalAppdataPath();
163 extern const OsPath& wutil_RoamingAppdataPath();
164 extern const OsPath& wutil_PersonalPath();
165 
166 
167 //-----------------------------------------------------------------------------
168 // Wow64
169 
170 extern bool wutil_IsWow64();
171 
173 {
174 public:
177 
178 private:
180 };
181 
182 
183 //-----------------------------------------------------------------------------
184 
185 LIB_API Status wutil_SetPrivilege(const wchar_t* privilege, bool enable);
186 
187 /**
188  * @return module handle of lib code (that of the main EXE if
189  * linked statically, otherwise the DLL).
190  * this is necessary for the error dialog.
191  **/
192 extern HMODULE wutil_LibModuleHandle();
193 
194 
195 /**
196  * @return handle to the first window owned by the current process, or
197  * 0 if none exist (e.g. it hasn't yet created one).
198  *
199  * enumerates all top-level windows and stops if PID matches.
200  * once this function returns a non-NULL handle, it will always
201  * return that cached value.
202  **/
203 extern HWND wutil_AppWindow();
204 
205 #endif // #ifndef INCLUDED_WUTIL
Definition: wutil.h:80
const OsPath & wutil_RoamingAppdataPath()
Definition: wutil.cpp:273
bool wutil_IsWow64()
Definition: wutil.cpp:434
some WinAPI functions SetLastError(0) on success, which is bad because it can hide previous errors...
Definition: wutil.h:119
bool wutil_IsValidHandle(H h)
Definition: wutil.h:34
bool wutil_IsLocked(WinLockId id)
Definition: wutil.cpp:91
Definition: wutil.h:81
Definition: wutil.h:92
WinLockId m_id
Definition: wutil.h:107
~WinScopedPreserveLastError()
Definition: wutil.h:128
bool wutil_HasCommandLineArgument(const wchar_t *arg)
Definition: wutil.cpp:236
void wutil_Lock(WinLockId id)
Definition: wutil.cpp:77
const OsPath & wutil_PersonalPath()
Definition: wutil.cpp:278
Definition: wutil.h:83
const OsPath & wutil_LocalAppdataPath()
Definition: wutil.cpp:268
Definition: wutil.h:172
void * wutil_Allocate(size_t size)
Definition: wutil.cpp:54
int wutil_argc()
Definition: wutil.cpp:217
Definition: path.h:77
wchar_t ** wutil_argv()
Definition: wutil.cpp:222
unsigned long DWORD
Definition: wgl.h:56
~WinScopedLock()
Definition: wutil.h:101
Status StatusFromWin()
Definition: wutil.cpp:125
i64 Status
Error handling system.
Definition: status.h:171
void * m_wasRedirectionEnabled
Definition: wutil.h:179
DWORD m_lastError
Definition: wutil.h:135
WinScopedPreserveLastError()
Definition: wutil.h:122
WinLockId
Definition: wutil.h:78
WinScopedLock(WinLockId id)
Definition: wutil.h:95
HMODULE wutil_LibModuleHandle()
Definition: wutil.cpp:494
LIB_API Status wutil_SetPrivilege(const wchar_t *privilege, bool enable)
Definition: wutil.cpp:463
HWND wutil_AppWindow()
Definition: wutil.cpp:536
void wutil_Free(void *p)
Definition: wutil.cpp:60
void wutil_Unlock(WinLockId id)
Definition: wutil.cpp:84
const OsPath & wutil_ExecutablePath()
Definition: wutil.cpp:263
const OsPath & wutil_SystemPath()
Definition: wutil.cpp:258