2006-04-12 01:59:08 +02:00
|
|
|
/**
|
|
|
|
* =========================================================================
|
2007-05-04 19:30:32 +02:00
|
|
|
* File : wutil.cpp
|
2006-04-12 01:59:08 +02:00
|
|
|
* Project : 0 A.D.
|
2007-05-04 19:30:32 +02:00
|
|
|
* Description : various Windows-specific utilities
|
2006-04-12 01:59:08 +02:00
|
|
|
* =========================================================================
|
|
|
|
*/
|
|
|
|
|
2007-05-07 18:33:24 +02:00
|
|
|
// license: GPL; see lib/license.txt
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2004-05-08 03:11:51 +02:00
|
|
|
#include "precompiled.h"
|
2007-05-04 19:30:32 +02:00
|
|
|
#include "wutil.h"
|
2004-05-08 03:11:51 +02:00
|
|
|
|
2004-06-04 14:41:53 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h> // __argc
|
2005-05-31 21:00:49 +02:00
|
|
|
|
2006-04-22 18:26:16 +02:00
|
|
|
#include "lib/path_util.h"
|
2007-04-25 20:19:35 +02:00
|
|
|
#include "lib/posix/posix.h"
|
2007-05-08 17:11:53 +02:00
|
|
|
#include "win.h"
|
2007-05-04 19:30:32 +02:00
|
|
|
#include "winit.h"
|
2005-01-29 17:43:46 +01:00
|
|
|
|
2007-05-04 19:30:32 +02:00
|
|
|
|
|
|
|
#pragma SECTION_PRE_LIBC(B)
|
|
|
|
WIN_REGISTER_FUNC(wutil_PreLibcInit);
|
|
|
|
#pragma FORCE_INCLUDE(wutil_PreLibcInit)
|
|
|
|
#pragma SECTION_POST_ATEXIT(Y)
|
|
|
|
WIN_REGISTER_FUNC(wutil_Shutdown);
|
|
|
|
#pragma FORCE_INCLUDE(wutil_Shutdown)
|
|
|
|
#pragma SECTION_RESTORE
|
2006-02-16 21:45:38 +01:00
|
|
|
|
2004-07-12 16:25:39 +02:00
|
|
|
|
2005-06-22 05:23:22 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
2007-05-24 04:32:53 +02:00
|
|
|
// safe allocator
|
2005-06-21 18:44:12 +02:00
|
|
|
|
2005-01-30 18:40:24 +01:00
|
|
|
//
|
|
|
|
// safe allocator that may be used independently of libc malloc
|
|
|
|
// (in particular, before _cinit and while calling static dtors).
|
|
|
|
// used by wpthread critical section code.
|
|
|
|
//
|
|
|
|
|
|
|
|
void* win_alloc(size_t size)
|
|
|
|
{
|
|
|
|
const DWORD flags = HEAP_ZERO_MEMORY;
|
|
|
|
return HeapAlloc(GetProcessHeap(), flags, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void win_free(void* p)
|
|
|
|
{
|
|
|
|
const DWORD flags = 0;
|
|
|
|
HeapFree(GetProcessHeap(), flags, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-22 05:23:22 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
2007-05-24 04:32:53 +02:00
|
|
|
// locks
|
2005-05-12 00:40:19 +02:00
|
|
|
|
2005-06-21 18:44:12 +02:00
|
|
|
// several init functions are before called before _cinit.
|
|
|
|
// POSIX static mutex init may not have been done by then,
|
|
|
|
// so we need our own lightweight functions.
|
2004-03-03 00:56:51 +01:00
|
|
|
|
|
|
|
static CRITICAL_SECTION cs[NUM_CS];
|
2005-01-23 19:04:34 +01:00
|
|
|
static bool cs_valid;
|
2004-03-03 00:56:51 +01:00
|
|
|
|
|
|
|
void win_lock(uint idx)
|
|
|
|
{
|
2005-06-28 06:12:50 +02:00
|
|
|
debug_assert(idx < NUM_CS && "win_lock: invalid critical section index");
|
2005-01-23 19:04:34 +01:00
|
|
|
if(cs_valid)
|
|
|
|
EnterCriticalSection(&cs[idx]);
|
2004-03-03 00:56:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void win_unlock(uint idx)
|
|
|
|
{
|
2005-06-28 06:12:50 +02:00
|
|
|
debug_assert(idx < NUM_CS && "win_unlock: invalid critical section index");
|
2005-01-23 19:04:34 +01:00
|
|
|
if(cs_valid)
|
|
|
|
LeaveCriticalSection(&cs[idx]);
|
|
|
|
}
|
|
|
|
|
2005-06-21 18:44:12 +02:00
|
|
|
int win_is_locked(uint idx)
|
|
|
|
{
|
2005-06-28 06:12:50 +02:00
|
|
|
debug_assert(idx < NUM_CS && "win_is_locked: invalid critical section index");
|
2005-06-21 18:44:12 +02:00
|
|
|
if(!cs_valid)
|
|
|
|
return -1;
|
|
|
|
BOOL got_it = TryEnterCriticalSection(&cs[idx]);
|
|
|
|
if(got_it)
|
|
|
|
LeaveCriticalSection(&cs[idx]);
|
|
|
|
return !got_it;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-05-04 19:30:32 +02:00
|
|
|
static void InitLocks()
|
2005-01-23 19:04:34 +01:00
|
|
|
{
|
|
|
|
for(int i = 0; i < NUM_CS; i++)
|
|
|
|
InitializeCriticalSection(&cs[i]);
|
|
|
|
|
|
|
|
cs_valid = true;
|
|
|
|
}
|
|
|
|
|
2007-05-04 19:30:32 +02:00
|
|
|
static void ShutdownLocks()
|
2005-01-23 19:04:34 +01:00
|
|
|
{
|
|
|
|
cs_valid = false;
|
|
|
|
|
|
|
|
for(int i = 0; i < NUM_CS; i++)
|
|
|
|
DeleteCriticalSection(&cs[i]);
|
|
|
|
memset(cs, 0, sizeof(cs));
|
2004-03-03 00:56:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-22 05:23:22 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
2007-05-24 04:32:53 +02:00
|
|
|
// error codes
|
|
|
|
|
|
|
|
// only call after a Win32 function indicates failure.
|
|
|
|
LibError LibError_from_GLE(bool warn_if_failed)
|
|
|
|
{
|
|
|
|
LibError err;
|
|
|
|
switch(GetLastError())
|
|
|
|
{
|
|
|
|
case ERROR_OUTOFMEMORY:
|
|
|
|
err = ERR::NO_MEM; break;
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2007-05-24 04:32:53 +02:00
|
|
|
case ERROR_INVALID_PARAMETER:
|
|
|
|
err = ERR::INVALID_PARAM; break;
|
|
|
|
case ERROR_INSUFFICIENT_BUFFER:
|
|
|
|
err = ERR::BUF_SIZE; break;
|
|
|
|
|
|
|
|
/*
|
|
|
|
case ERROR_ACCESS_DENIED:
|
|
|
|
err = ERR::FILE_ACCESS; break;
|
|
|
|
case ERROR_FILE_NOT_FOUND:
|
|
|
|
case ERROR_PATH_NOT_FOUND:
|
|
|
|
err = ERR::TNODE_NOT_FOUND; break;
|
|
|
|
*/
|
|
|
|
default:
|
|
|
|
err = ERR::FAIL; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(warn_if_failed)
|
|
|
|
DEBUG_WARN_ERR(err);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// return the LibError equivalent of GetLastError(), or ERR::FAIL if
|
|
|
|
// there's no equal.
|
|
|
|
// you should SetLastError(0) before calling whatever will set ret
|
|
|
|
// to make sure we do not return any stale errors.
|
|
|
|
LibError LibError_from_win32(DWORD ret, bool warn_if_failed)
|
|
|
|
{
|
|
|
|
if(ret != FALSE)
|
|
|
|
return INFO::OK;
|
|
|
|
return LibError_from_GLE(warn_if_failed);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// directories
|
|
|
|
|
|
|
|
char win_sys_dir[MAX_PATH+1];
|
|
|
|
char win_exe_dir[MAX_PATH+1];
|
|
|
|
|
|
|
|
static void GetDirectories()
|
|
|
|
{
|
|
|
|
GetSystemDirectory(win_sys_dir, sizeof(win_sys_dir));
|
|
|
|
|
|
|
|
if(GetModuleFileName(GetModuleHandle(0), win_exe_dir, MAX_PATH) != 0)
|
|
|
|
path_strip_fn(win_exe_dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// user32 fix
|
|
|
|
|
|
|
|
// HACK: make sure a reference to user32 is held, even if someone
|
|
|
|
// decides to delay-load it. this fixes bug #66, which was the
|
|
|
|
// Win32 mouse cursor (set via user32!SetCursor) appearing as a
|
|
|
|
// black 32x32(?) rectangle. underlying cause was as follows:
|
|
|
|
// powrprof.dll was the first client of user32, causing it to be
|
|
|
|
// loaded. after we were finished with powrprof, we freed it, in turn
|
|
|
|
// causing user32 to unload. later code would then reload user32,
|
|
|
|
// which apparently terminally confused the cursor implementation.
|
|
|
|
//
|
|
|
|
// since we hold a reference here, user32 will never unload.
|
|
|
|
// of course, the benefits of delay-loading are lost for this DLL,
|
|
|
|
// but that is unavoidable. it is safer to force loading it, rather
|
|
|
|
// than documenting the problem and asking it not be delay-loaded.
|
2005-07-16 19:52:05 +02:00
|
|
|
static HMODULE hUser32Dll;
|
|
|
|
|
2007-05-24 04:32:53 +02:00
|
|
|
static void ForciblyLoadUser32Dll()
|
|
|
|
{
|
|
|
|
hUser32Dll = LoadLibrary("user32.dll");
|
|
|
|
}
|
2005-06-25 09:54:15 +02:00
|
|
|
|
2007-05-24 04:32:53 +02:00
|
|
|
// avoids Boundschecker warning
|
|
|
|
static void FreeUser32Dll()
|
|
|
|
{
|
|
|
|
FreeLibrary(hUser32Dll);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// memory
|
|
|
|
|
|
|
|
// note: has no effect if config.h's HAVE_VC_DEBUG_ALLOC is 0.
|
|
|
|
static void EnableMemoryTracking()
|
2005-06-21 18:44:12 +02:00
|
|
|
{
|
2005-08-09 18:23:19 +02:00
|
|
|
#if CONFIG_PARANOIA
|
2005-06-22 05:23:22 +02:00
|
|
|
debug_heap_enable(DEBUG_HEAP_ALL);
|
2005-07-16 19:52:05 +02:00
|
|
|
#elif !defined(NDEBUG)
|
2005-06-22 05:23:22 +02:00
|
|
|
debug_heap_enable(DEBUG_HEAP_NORMAL);
|
|
|
|
#endif
|
2007-05-24 04:32:53 +02:00
|
|
|
}
|
2005-06-21 18:44:12 +02:00
|
|
|
|
2007-05-24 04:32:53 +02:00
|
|
|
static void EnableLowFragmentationHeap()
|
|
|
|
{
|
2005-06-21 18:44:12 +02:00
|
|
|
#if WINVER >= 0x0501
|
2005-02-02 04:26:38 +01:00
|
|
|
HMODULE hKernel32Dll = LoadLibrary("kernel32.dll");
|
2007-05-24 04:32:53 +02:00
|
|
|
BOOL (WINAPI* pHeapSetInformation)(HANDLE, HEAP_INFORMATION_CLASS, void*, size_t);
|
|
|
|
*(void**)&pHeapSetInformation = GetProcAddress(hKernel32Dll, "HeapSetInformation");
|
|
|
|
if(!pHeapSetInformation)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ULONG flags = 2; // enable LFH
|
|
|
|
pHeapSetInformation(GetProcessHeap(), HeapCompatibilityInformation, &flags, sizeof(flags));
|
|
|
|
|
|
|
|
FreeLibrary(hKernel32Dll);
|
|
|
|
#endif // #if WINVER >= 0x0501
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2007-05-24 14:31:16 +02:00
|
|
|
// Wow64
|
|
|
|
|
|
|
|
// Wow64 'helpfully' redirects all 32-bit apps' accesses of
|
|
|
|
// %windir%\\system32\\drivers to %windir%\\system32\\drivers\\SysWOW64.
|
|
|
|
// that's bad, because the actual drivers are not in the subdirectory. to
|
|
|
|
// work around this, provide for temporarily disabling redirection.
|
|
|
|
|
|
|
|
static BOOL (WINAPI *pIsWow64Process)(HANDLE, PBOOL);
|
|
|
|
static BOOL (WINAPI *pWow64DisableWow64FsRedirection)(PVOID*) = 0;
|
|
|
|
static BOOL (WINAPI *pWow64RevertWow64FsRedirection)(PVOID) = 0;
|
2007-05-24 04:32:53 +02:00
|
|
|
|
|
|
|
static bool isWow64;
|
|
|
|
|
2007-05-24 14:31:16 +02:00
|
|
|
static void ImportWow64Functions()
|
2007-05-24 04:32:53 +02:00
|
|
|
{
|
2007-05-24 14:31:16 +02:00
|
|
|
HMODULE hKernel32Dll = LoadLibrary("kernel32.dll");
|
2007-05-24 04:32:53 +02:00
|
|
|
*(void**)&pIsWow64Process = GetProcAddress(hKernel32Dll, "IsWow64Process");
|
2007-05-24 14:31:16 +02:00
|
|
|
*(void**)&pWow64DisableWow64FsRedirection = GetProcAddress(hKernel32Dll, "Wow64DisableWow64FsRedirection");
|
|
|
|
*(void**)&pWow64RevertWow64FsRedirection = GetProcAddress(hKernel32Dll, "Wow64RevertWow64FsRedirection");
|
2007-05-24 04:32:53 +02:00
|
|
|
FreeLibrary(hKernel32Dll);
|
2007-05-24 14:31:16 +02:00
|
|
|
}
|
2007-05-24 04:32:53 +02:00
|
|
|
|
2007-05-24 14:31:16 +02:00
|
|
|
static void DetectWow64()
|
|
|
|
{
|
2007-05-24 04:32:53 +02:00
|
|
|
// function not found => running on 32-bit Windows
|
|
|
|
if(!pIsWow64Process)
|
2005-02-02 04:26:38 +01:00
|
|
|
{
|
2007-05-24 04:32:53 +02:00
|
|
|
isWow64 = false;
|
|
|
|
return;
|
2005-02-02 04:26:38 +01:00
|
|
|
}
|
|
|
|
|
2007-05-24 04:32:53 +02:00
|
|
|
BOOL isWow64Process = FALSE;
|
|
|
|
const BOOL ok = IsWow64Process(GetCurrentProcess(), &isWow64Process);
|
|
|
|
WARN_IF_FALSE(ok);
|
|
|
|
isWow64 = (isWow64Process == TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool wutil_IsWow64()
|
|
|
|
{
|
|
|
|
return isWow64;
|
|
|
|
}
|
|
|
|
|
2007-05-24 14:31:16 +02:00
|
|
|
void wutil_DisableWow64Redirection(void*& wasRedirectionEnabled)
|
|
|
|
{
|
|
|
|
// note: don't just check if the function pointers are valid. 32-bit
|
|
|
|
// Vista includes them but isn't running Wow64, so calling the functions
|
|
|
|
// would fail. since we have to check if actually on Wow64, there's no
|
|
|
|
// more need to verify the pointers (their existence is implied).
|
|
|
|
if(!wutil_IsWow64())
|
|
|
|
return;
|
|
|
|
BOOL ok = pWow64DisableWow64FsRedirection(&wasRedirectionEnabled);
|
|
|
|
WARN_IF_FALSE(ok);
|
|
|
|
}
|
|
|
|
|
|
|
|
void wutil_RevertWow64Redirection(void* wasRedirectionEnabled)
|
|
|
|
{
|
|
|
|
if(!wutil_IsWow64())
|
|
|
|
return;
|
|
|
|
BOOL ok = pWow64RevertWow64FsRedirection(wasRedirectionEnabled);
|
|
|
|
WARN_IF_FALSE(ok);
|
|
|
|
}
|
|
|
|
|
2007-05-24 04:32:53 +02:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
static LibError wutil_PreLibcInit()
|
|
|
|
{
|
2007-05-04 19:30:32 +02:00
|
|
|
InitLocks();
|
2004-11-19 23:41:37 +01:00
|
|
|
|
2007-05-24 04:32:53 +02:00
|
|
|
ForciblyLoadUser32Dll();
|
2004-11-22 08:40:32 +01:00
|
|
|
|
2007-05-24 04:32:53 +02:00
|
|
|
EnableMemoryTracking();
|
2004-11-22 08:40:32 +01:00
|
|
|
|
2007-05-24 04:32:53 +02:00
|
|
|
EnableLowFragmentationHeap();
|
|
|
|
|
|
|
|
GetDirectories();
|
|
|
|
|
2007-05-24 14:31:16 +02:00
|
|
|
ImportWow64Functions();
|
2007-05-24 04:32:53 +02:00
|
|
|
DetectWow64();
|
2005-07-16 19:52:05 +02:00
|
|
|
|
2007-05-04 19:30:32 +02:00
|
|
|
return INFO::OK;
|
2004-03-03 00:56:51 +01:00
|
|
|
}
|
|
|
|
|
2005-05-12 00:40:19 +02:00
|
|
|
|
2007-05-04 19:30:32 +02:00
|
|
|
static LibError wutil_Shutdown()
|
2004-03-03 00:56:51 +01:00
|
|
|
{
|
2007-05-24 04:32:53 +02:00
|
|
|
FreeUser32Dll();
|
2006-12-09 15:39:52 +01:00
|
|
|
|
2007-05-24 04:32:53 +02:00
|
|
|
ShutdownLocks();
|
2006-12-09 15:39:52 +01:00
|
|
|
|
2007-05-04 19:30:32 +02:00
|
|
|
return INFO::OK;
|
2006-12-09 15:39:52 +01:00
|
|
|
}
|