27 #include "precompiled.h"
56 const DWORD flags = HEAP_ZERO_MEMORY;
57 return HeapAlloc(GetProcessHeap(), flags, size);
62 const DWORD flags = 0;
63 HeapFree(GetProcessHeap(), flags, p);
81 EnterCriticalSection(&
cs[
id]);
88 LeaveCriticalSection(&
cs[
id]);
95 const BOOL successfullyEntered = TryEnterCriticalSection(&
cs[
id]);
96 if(!successfullyEntered)
98 LeaveCriticalSection(&
cs[
id]);
105 for(
int i = 0; i <
NUM_CS; i++)
106 InitializeCriticalSection(&
cs[i]);
115 for(
int i = 0; i <
NUM_CS; i++)
116 DeleteCriticalSection(&
cs[i]);
117 memset(
cs, 0,
sizeof(
cs));
127 switch(GetLastError())
132 case ERROR_OPERATION_ABORTED:
135 case ERROR_INVALID_HANDLE:
137 case ERROR_INSUFFICIENT_BUFFER:
139 case ERROR_INVALID_PARAMETER:
140 case ERROR_BAD_ARGUMENTS:
143 case ERROR_OUTOFMEMORY:
144 case ERROR_NOT_ENOUGH_MEMORY:
146 case ERROR_NOT_SUPPORTED:
147 case ERROR_CALL_NOT_IMPLEMENTED:
148 case ERROR_PROC_NOT_FOUND:
151 case ERROR_FILE_NOT_FOUND:
152 case ERROR_PATH_NOT_FOUND:
154 case ERROR_ACCESS_DENIED:
175 const wchar_t* commandLine = GetCommandLineW();
177 size_t numChars = wcslen(commandLine);
178 argvContents = (
wchar_t*)HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS, (numChars+1)*
sizeof(
wchar_t));
182 bool ignoreSpace =
false;
183 for(
size_t i = 0; i < numChars; i++)
188 ignoreSpace = !ignoreSpace;
207 s_argv = (
wchar_t**)HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS,
s_argc*
sizeof(
wchar_t*));
209 for(
int i = 0; i <
s_argc; i++)
212 nextArg += wcslen(nextArg)+1;
231 HeapFree(GetProcessHeap(), 0,
s_argv);
238 for(
int i = 0; i <
s_argc; i++)
240 if(!wcscmp(
s_argv[i], arg))
288 wchar_t path[MAX_PATH];
289 const HRESULT ret = SHGetFolderPathW(hwnd, csidl, token, 0, path);
291 if(GetLastError() == ERROR_NO_TOKEN)
302 const UINT length = GetSystemDirectoryW(0, 0);
304 std::wstring path(length,
'\0');
305 const UINT charsWritten = GetSystemDirectoryW(&path[0], length);
306 ENSURE(charsWritten == length-1);
326 systemPath->~OsPath();
328 executablePath->~OsPath();
330 localAppdataPath->~OsPath();
332 roamingAppdataPath->~OsPath();
334 personalPath->~OsPath();
374 if(IsDebuggerPresent())
377 char* var = getenv(
"_NO_DEBUG_HEAP");
378 if(!var || var[0] !=
'1')
385 if(pHeapSetInformation)
388 pHeapSetInformation(GetProcessHeap(), HeapCompatibilityInformation, &flags,
sizeof(flags));
390 #endif // #if WINVER >= 0x0501
403 static WUTIL_FUNC(pWow64DisableWow64FsRedirection,
BOOL, (PVOID*));
424 BOOL isWow64Process = FALSE;
425 const BOOL ok = pIsWow64Process(GetCurrentProcess(), &isWow64Process);
427 isWow64 = (isWow64Process == TRUE);
464 if(!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, &hToken))
468 if (!LookupPrivilegeValueW(NULL, privilege, &tp.Privileges[0].Luid))
470 tp.PrivilegeCount = 1;
471 tp.Privileges[0].Attributes = enable? SE_PRIVILEGE_ENABLED : 0;
474 const BOOL ok = AdjustTokenPrivileges(hToken, FALSE, &tp, 0, 0, 0);
475 if(!ok || GetLastError() != 0)
486 #ifndef LIB_STATIC_LINK
493 const DWORD flags = GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS|GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT;
504 return GetModuleHandle(0);
523 DWORD tid = GetWindowThreadProcessId(hWnd, &pid);
526 if(pid == GetCurrentProcessId())
static OsPath * GetFolderPath(int csidl)
#define WINIT_REGISTER_LATE_SHUTDOWN(func)
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning. ...
static void FreeDirectories()
static OsPath * roamingAppdataPath
void wutil_Unlock(WinLockId id)
some WinAPI functions SetLastError(0) on success, which is bad because it can hide previous errors...
HMODULE wutil_LibModuleHandle()
void * wutil_Allocate(size_t size)
#define WUTIL_FUNC(varName, ret, params)
static void FreeUser32Dll()
static void ForciblyLoadUser32Dll()
static Status wutil_Shutdown()
const Status INVALID_HANDLE
int wcscpy_s(wchar_t *dst, size_t max_dst_chars, const wchar_t *src)
const Status NOT_SUPPORTED
bool wutil_IsLocked(WinLockId id)
const OsPath & wutil_RoamingAppdataPath()
const OsPath & wutil_LocalAppdataPath()
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
#define UNUSED2(param)
mark a function local variable or parameter as unused and avoid the corresponding compiler warning...
#define WINIT_REGISTER_EARLY_INIT(func)
static OsPath * systemPath
WinScopedDisableWow64Redirection()
static OsPath * executablePath
const Status INVALID_PARAM
static wchar_t * argvContents
i64 Status
Error handling system.
static BOOL CALLBACK FindAppWindowByPid(HWND hWnd, LPARAM lParam)
const Status INVALID_SIZE
static CRITICAL_SECTION cs[NUM_CS]
const Status FILE_NOT_FOUND
Status wutil_SetPrivilege(const wchar_t *privilege, bool enable)
void * m_wasRedirectionEnabled
static void ShutdownLocks()
static Status wutil_Init()
static void ImportWow64Functions()
OsPath sys_ExecutablePathname()
const OsPath & wutil_ExecutablePath()
#define WARN_IF_FALSE(expression)
static void DetectWow64()
static HMODULE hUser32Dll
const OsPath & wutil_SystemPath()
static void EnableLowFragmentationHeap()
static OsPath * personalPath
static void GetDirectories()
static OsPath * localAppdataPath
bool wutil_HasCommandLineArgument(const wchar_t *arg)
#define wdbg_assert(expr)
similar to ENSURE but safe to use during critical init or while under the heap or dbghelp locks...
~WinScopedDisableWow64Redirection()
void wutil_Lock(WinLockId id)
#define WUTIL_IMPORT_KERNEL32(procName, varName)
const OsPath & wutil_PersonalPath()
static void ReadCommandLine()
static void FreeCommandLine()