2010-02-08 08:23:39 -08:00
|
|
|
/* Copyright (c) 2010 Wildfire Games
|
2009-04-18 10:00:33 -07:00
|
|
|
*
|
2010-02-08 08:23:39 -08:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
|
* the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included
|
|
|
|
|
* in all copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
2009-04-18 10:00:33 -07:00
|
|
|
*/
|
|
|
|
|
|
2009-04-18 10:51:05 -07:00
|
|
|
/*
|
|
|
|
|
* various Windows-specific utilities
|
2006-04-11 16:59:08 -07:00
|
|
|
*/
|
|
|
|
|
|
2004-05-07 18:11:51 -07:00
|
|
|
#include "precompiled.h"
|
2010-03-01 06:52:58 -08:00
|
|
|
#include "lib/sysdep/os/win/wutil.h"
|
2004-05-07 18:11:51 -07:00
|
|
|
|
2004-06-04 05:41:53 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h> // __argc
|
2005-05-31 12:00:49 -07:00
|
|
|
|
2006-04-22 09:26:16 -07:00
|
|
|
#include "lib/path_util.h"
|
2007-04-25 11:19:35 -07:00
|
|
|
#include "lib/posix/posix.h"
|
2010-03-01 06:52:58 -08:00
|
|
|
#include "lib/sysdep/os/win/win.h"
|
|
|
|
|
#include "lib/sysdep/os/win/winit.h"
|
2005-01-29 08:43:46 -08:00
|
|
|
|
2009-08-02 04:07:42 -07:00
|
|
|
#include <shlobj.h> // SHGetFolderPath
|
|
|
|
|
|
|
|
|
|
|
2007-06-04 15:59:14 -07:00
|
|
|
WINIT_REGISTER_EARLY_INIT(wutil_Init);
|
|
|
|
|
WINIT_REGISTER_LATE_SHUTDOWN(wutil_Shutdown);
|
2004-07-12 07:25:39 -07:00
|
|
|
|
2007-06-24 06:24:40 -07:00
|
|
|
|
2005-06-21 20:23:22 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
2007-05-23 19:32:53 -07:00
|
|
|
// safe allocator
|
2005-06-21 09:44:12 -07:00
|
|
|
|
2005-01-30 09:40:24 -08: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-21 20:23:22 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
2007-05-23 19:32:53 -07:00
|
|
|
// locks
|
2005-05-11 15:40:19 -07:00
|
|
|
|
2005-06-21 09:44:12 -07: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-02 15:56:51 -08:00
|
|
|
|
|
|
|
|
static CRITICAL_SECTION cs[NUM_CS];
|
2005-01-23 10:04:34 -08:00
|
|
|
static bool cs_valid;
|
2004-03-02 15:56:51 -08:00
|
|
|
|
2008-01-19 03:33:11 -08:00
|
|
|
void win_lock(WinLockId id)
|
2004-03-02 15:56:51 -08:00
|
|
|
{
|
2008-01-19 03:33:11 -08:00
|
|
|
if(!cs_valid)
|
|
|
|
|
return;
|
|
|
|
|
EnterCriticalSection(&cs[id]);
|
2004-03-02 15:56:51 -08:00
|
|
|
}
|
|
|
|
|
|
2008-01-19 03:33:11 -08:00
|
|
|
void win_unlock(WinLockId id)
|
2004-03-02 15:56:51 -08:00
|
|
|
{
|
2008-01-19 03:33:11 -08:00
|
|
|
if(!cs_valid)
|
|
|
|
|
return;
|
|
|
|
|
LeaveCriticalSection(&cs[id]);
|
2005-01-23 10:04:34 -08:00
|
|
|
}
|
|
|
|
|
|
2008-01-19 03:33:11 -08:00
|
|
|
bool win_is_locked(WinLockId id)
|
2005-06-21 09:44:12 -07:00
|
|
|
{
|
|
|
|
|
if(!cs_valid)
|
2008-01-19 03:33:11 -08:00
|
|
|
return false;
|
|
|
|
|
const BOOL successfullyEntered = TryEnterCriticalSection(&cs[id]);
|
|
|
|
|
if(!successfullyEntered)
|
|
|
|
|
return true; // still locked
|
|
|
|
|
LeaveCriticalSection(&cs[id]);
|
|
|
|
|
return false; // probably not locked
|
2005-06-21 09:44:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-05-04 10:30:32 -07:00
|
|
|
static void InitLocks()
|
2005-01-23 10:04:34 -08:00
|
|
|
{
|
|
|
|
|
for(int i = 0; i < NUM_CS; i++)
|
|
|
|
|
InitializeCriticalSection(&cs[i]);
|
|
|
|
|
|
|
|
|
|
cs_valid = true;
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-04 10:30:32 -07:00
|
|
|
static void ShutdownLocks()
|
2005-01-23 10:04:34 -08:00
|
|
|
{
|
|
|
|
|
cs_valid = false;
|
|
|
|
|
|
|
|
|
|
for(int i = 0; i < NUM_CS; i++)
|
|
|
|
|
DeleteCriticalSection(&cs[i]);
|
|
|
|
|
memset(cs, 0, sizeof(cs));
|
2004-03-02 15:56:51 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2005-06-21 20:23:22 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
2007-05-23 19:32:53 -07:00
|
|
|
// error codes
|
|
|
|
|
|
|
|
|
|
// only call after a Win32 function indicates failure.
|
|
|
|
|
LibError LibError_from_GLE(bool warn_if_failed)
|
|
|
|
|
{
|
2007-12-20 12:09:19 -08:00
|
|
|
LibError err = ERR::FAIL;
|
2007-05-23 19:32:53 -07:00
|
|
|
switch(GetLastError())
|
|
|
|
|
{
|
|
|
|
|
case ERROR_OUTOFMEMORY:
|
|
|
|
|
err = ERR::NO_MEM; break;
|
2004-03-02 15:56:51 -08:00
|
|
|
|
2007-05-23 19:32:53 -07: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;
|
|
|
|
|
*/
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-09-08 01:09:32 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// command line
|
|
|
|
|
|
|
|
|
|
// copy of GetCommandLine string. will be tokenized and then referenced by
|
|
|
|
|
// the argv pointers.
|
2009-11-03 13:46:35 -08:00
|
|
|
static wchar_t* argvContents;
|
2007-09-08 01:09:32 -07:00
|
|
|
|
|
|
|
|
int wutil_argc = 0;
|
2009-11-03 13:46:35 -08:00
|
|
|
wchar_t** wutil_argv = 0;
|
2007-09-08 01:09:32 -07:00
|
|
|
|
|
|
|
|
static void ReadCommandLine()
|
|
|
|
|
{
|
2009-11-03 13:46:35 -08:00
|
|
|
const wchar_t* commandLine = GetCommandLineW();
|
2007-09-08 01:09:32 -07:00
|
|
|
// (this changes as quotation marks are removed)
|
2009-11-03 13:46:35 -08:00
|
|
|
size_t numChars = wcslen(commandLine);
|
|
|
|
|
argvContents = (wchar_t*)HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS, (numChars+1)*sizeof(wchar_t));
|
|
|
|
|
wcscpy_s(argvContents, numChars+1, commandLine);
|
2007-09-08 01:09:32 -07:00
|
|
|
|
|
|
|
|
// first pass: tokenize string and count number of arguments
|
|
|
|
|
bool ignoreSpace = false;
|
|
|
|
|
for(size_t i = 0; i < numChars; i++)
|
|
|
|
|
{
|
|
|
|
|
switch(argvContents[i])
|
|
|
|
|
{
|
|
|
|
|
case '"':
|
|
|
|
|
ignoreSpace = !ignoreSpace;
|
|
|
|
|
// strip the " character
|
2009-11-03 13:46:35 -08:00
|
|
|
memmove(argvContents+i, argvContents+i+1, (numChars-i)*sizeof(wchar_t));
|
2007-09-08 01:09:32 -07:00
|
|
|
numChars--;
|
2007-09-08 01:37:30 -07:00
|
|
|
i--;
|
2007-09-08 01:09:32 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ' ':
|
|
|
|
|
if(!ignoreSpace)
|
|
|
|
|
{
|
|
|
|
|
argvContents[i] = '\0';
|
|
|
|
|
wutil_argc++;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-09-08 01:37:30 -07:00
|
|
|
wutil_argc++;
|
2007-09-08 01:09:32 -07:00
|
|
|
|
|
|
|
|
// have argv entries point into the tokenized string
|
2009-11-03 13:46:35 -08:00
|
|
|
wutil_argv = (wchar_t**)HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS, wutil_argc*sizeof(wchar_t*));
|
|
|
|
|
wchar_t* nextArg = argvContents;
|
2007-09-08 01:09:32 -07:00
|
|
|
for(int i = 0; i < wutil_argc; i++)
|
|
|
|
|
{
|
|
|
|
|
wutil_argv[i] = nextArg;
|
2009-11-03 13:46:35 -08:00
|
|
|
nextArg += wcslen(nextArg)+1;
|
2007-09-08 01:09:32 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void FreeCommandLine()
|
|
|
|
|
{
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, wutil_argv);
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, argvContents);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-11-03 13:46:35 -08:00
|
|
|
bool wutil_HasCommandLineArgument(const wchar_t* arg)
|
2007-09-08 01:09:32 -07:00
|
|
|
{
|
|
|
|
|
for(int i = 0; i < wutil_argc; i++)
|
|
|
|
|
{
|
2009-11-03 13:46:35 -08:00
|
|
|
if(!wcscmp(wutil_argv[i], arg))
|
2007-09-08 01:09:32 -07:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-05-23 19:32:53 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// directories
|
|
|
|
|
|
2009-11-03 13:46:35 -08:00
|
|
|
// (NB: wutil_Init is called before static ctors => use placement new)
|
|
|
|
|
static fs::wpath* systemPath;
|
|
|
|
|
static fs::wpath* executablePath;
|
|
|
|
|
static fs::wpath* appdataPath;
|
|
|
|
|
|
|
|
|
|
const fs::wpath& wutil_SystemPath()
|
|
|
|
|
{
|
|
|
|
|
return *systemPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const fs::wpath& wutil_ExecutablePath()
|
|
|
|
|
{
|
|
|
|
|
return *executablePath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const fs::wpath& wutil_AppdataPath()
|
|
|
|
|
{
|
|
|
|
|
return *appdataPath;
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-23 19:32:53 -07:00
|
|
|
|
|
|
|
|
static void GetDirectories()
|
|
|
|
|
{
|
2009-08-02 04:07:42 -07:00
|
|
|
WinScopedPreserveLastError s;
|
2009-11-03 13:46:35 -08:00
|
|
|
wchar_t path[MAX_PATH+1];
|
2009-08-02 04:07:42 -07:00
|
|
|
|
|
|
|
|
// system directory
|
|
|
|
|
{
|
2009-11-03 13:46:35 -08:00
|
|
|
const UINT charsWritten = GetSystemDirectoryW(path, ARRAY_SIZE(path));
|
2009-08-02 04:07:42 -07:00
|
|
|
debug_assert(charsWritten != 0);
|
2009-11-03 13:46:35 -08:00
|
|
|
systemPath = new(win_alloc(sizeof(fs::wpath))) fs::wpath(path);
|
2009-08-02 04:07:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// executable's directory
|
|
|
|
|
{
|
2009-11-03 13:46:35 -08:00
|
|
|
const DWORD len = GetModuleFileNameW(GetModuleHandle(0), path, ARRAY_SIZE(path));
|
2009-08-02 04:07:42 -07:00
|
|
|
debug_assert(len != 0);
|
2009-11-03 13:46:35 -08:00
|
|
|
executablePath = new(win_alloc(sizeof(fs::wpath))) fs::wpath(path);
|
|
|
|
|
*executablePath = executablePath->branch_path();
|
2009-08-02 04:07:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// application data
|
|
|
|
|
{
|
|
|
|
|
HWND hwnd = 0; // ignored unless a dial-up connection is needed to access the folder
|
|
|
|
|
HANDLE token = 0;
|
2009-11-03 13:46:35 -08:00
|
|
|
const HRESULT ret = SHGetFolderPathW(hwnd, CSIDL_APPDATA, token, 0, path);
|
2009-08-02 04:07:42 -07:00
|
|
|
debug_assert(SUCCEEDED(ret));
|
2009-11-03 13:46:35 -08:00
|
|
|
appdataPath = new(win_alloc(sizeof(fs::wpath))) fs::wpath(path);
|
2009-08-02 04:07:42 -07:00
|
|
|
}
|
2007-05-23 19:32:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-11-03 13:46:35 -08:00
|
|
|
static void FreeDirectories()
|
|
|
|
|
{
|
|
|
|
|
systemPath->~basic_path();
|
|
|
|
|
win_free(systemPath);
|
|
|
|
|
executablePath->~basic_path();
|
|
|
|
|
win_free(executablePath);
|
|
|
|
|
appdataPath->~basic_path();
|
|
|
|
|
win_free(appdataPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-05-23 19:32:53 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// 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 10:52:05 -07:00
|
|
|
static HMODULE hUser32Dll;
|
|
|
|
|
|
2007-05-23 19:32:53 -07:00
|
|
|
static void ForciblyLoadUser32Dll()
|
|
|
|
|
{
|
2010-04-03 03:46:28 -07:00
|
|
|
hUser32Dll = LoadLibraryW(L"user32.dll");
|
2007-05-23 19:32:53 -07:00
|
|
|
}
|
2005-06-25 00:54:15 -07:00
|
|
|
|
2007-05-23 19:32:53 -07:00
|
|
|
// avoids Boundschecker warning
|
|
|
|
|
static void FreeUser32Dll()
|
|
|
|
|
{
|
|
|
|
|
FreeLibrary(hUser32Dll);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// memory
|
|
|
|
|
|
|
|
|
|
static void EnableLowFragmentationHeap()
|
|
|
|
|
{
|
2005-06-21 09:44:12 -07:00
|
|
|
#if WINVER >= 0x0501
|
2010-04-03 03:46:28 -07:00
|
|
|
const HMODULE hKernel32Dll = GetModuleHandleW(L"kernel32.dll");
|
2008-06-28 10:51:18 -07:00
|
|
|
typedef BOOL (WINAPI* PHeapSetInformation)(HANDLE, HEAP_INFORMATION_CLASS, void*, size_t);
|
|
|
|
|
PHeapSetInformation pHeapSetInformation = (PHeapSetInformation)GetProcAddress(hKernel32Dll, "HeapSetInformation");
|
2007-05-23 19:32:53 -07:00
|
|
|
if(!pHeapSetInformation)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
ULONG flags = 2; // enable LFH
|
|
|
|
|
pHeapSetInformation(GetProcessHeap(), HeapCompatibilityInformation, &flags, sizeof(flags));
|
|
|
|
|
#endif // #if WINVER >= 0x0501
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-05-26 08:34:10 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// version
|
|
|
|
|
|
2010-04-03 03:46:28 -07:00
|
|
|
static wchar_t windowsVersionString[20];
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 11:48:32 -07:00
|
|
|
static size_t windowsVersion; // see WUTIL_VERSION_*
|
2007-05-26 08:34:10 -07:00
|
|
|
|
|
|
|
|
static void DetectWindowsVersion()
|
|
|
|
|
{
|
|
|
|
|
// note: don't use GetVersion[Ex] because it gives the version of the
|
|
|
|
|
// emulated OS when running an app with compatibility shims enabled.
|
|
|
|
|
HKEY hKey;
|
2010-04-03 03:46:28 -07:00
|
|
|
if(RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
|
2007-05-26 08:34:10 -07:00
|
|
|
{
|
2010-04-03 03:46:28 -07:00
|
|
|
DWORD size = sizeof(windowsVersionString);
|
|
|
|
|
(void)RegQueryValueExW(hKey, L"CurrentVersion", 0, 0, (LPBYTE)windowsVersionString, &size);
|
2007-06-12 16:29:27 -07:00
|
|
|
|
2008-09-16 10:46:45 -07:00
|
|
|
int major = 0, minor = 0;
|
2010-04-03 03:46:28 -07:00
|
|
|
int ret = swscanf_s(windowsVersionString, L"%d.%d", &major, &minor);
|
2007-06-12 16:29:27 -07:00
|
|
|
debug_assert(ret == 2);
|
|
|
|
|
debug_assert(major <= 0xFF && minor <= 0xFF);
|
|
|
|
|
windowsVersion = (major << 8) | minor;
|
2007-05-26 08:34:10 -07:00
|
|
|
|
|
|
|
|
RegCloseKey(hKey);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
debug_assert(0);
|
|
|
|
|
}
|
|
|
|
|
|
2007-06-24 06:24:40 -07:00
|
|
|
|
2010-04-03 03:46:28 -07:00
|
|
|
const wchar_t* wutil_WindowsFamily()
|
2007-06-24 06:24:40 -07:00
|
|
|
{
|
|
|
|
|
debug_assert(windowsVersion != 0);
|
|
|
|
|
switch(windowsVersion)
|
|
|
|
|
{
|
|
|
|
|
case WUTIL_VERSION_2K:
|
2010-04-03 03:46:28 -07:00
|
|
|
return L"Win2k";
|
2007-06-24 06:24:40 -07:00
|
|
|
case WUTIL_VERSION_XP:
|
2010-04-03 03:46:28 -07:00
|
|
|
return L"WinXP";
|
2007-06-24 06:24:40 -07:00
|
|
|
case WUTIL_VERSION_XP64:
|
2010-04-03 03:46:28 -07:00
|
|
|
return L"WinXP64";
|
2007-06-24 06:24:40 -07:00
|
|
|
case WUTIL_VERSION_VISTA:
|
2010-04-03 03:46:28 -07:00
|
|
|
return L"Vista";
|
2007-06-24 06:24:40 -07:00
|
|
|
default:
|
2010-04-03 03:46:28 -07:00
|
|
|
return L"Windows";
|
2007-06-24 06:24:40 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-04-03 03:46:28 -07:00
|
|
|
const wchar_t* wutil_WindowsVersionString()
|
2007-05-26 08:34:10 -07:00
|
|
|
{
|
2007-06-12 16:29:27 -07:00
|
|
|
debug_assert(windowsVersionString[0] != '\0');
|
|
|
|
|
return windowsVersionString;
|
2007-05-26 08:34:10 -07:00
|
|
|
}
|
|
|
|
|
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 11:48:32 -07:00
|
|
|
size_t wutil_WindowsVersion()
|
2007-05-26 08:34:10 -07:00
|
|
|
{
|
2007-06-12 16:29:27 -07:00
|
|
|
debug_assert(windowsVersion != 0);
|
|
|
|
|
return windowsVersion;
|
2007-05-26 08:34:10 -07:00
|
|
|
}
|
|
|
|
|
|
2007-06-12 16:29:27 -07:00
|
|
|
|
2007-05-23 19:32:53 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
2007-05-24 05:31:16 -07: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.
|
|
|
|
|
|
2008-06-28 10:51:18 -07:00
|
|
|
typedef BOOL (WINAPI *PIsWow64Process)(HANDLE, PBOOL);
|
|
|
|
|
typedef BOOL (WINAPI *PWow64DisableWow64FsRedirection)(PVOID*);
|
|
|
|
|
typedef BOOL (WINAPI *PWow64RevertWow64FsRedirection)(PVOID);
|
|
|
|
|
static PIsWow64Process pIsWow64Process;
|
|
|
|
|
static PWow64DisableWow64FsRedirection pWow64DisableWow64FsRedirection;
|
|
|
|
|
static PWow64RevertWow64FsRedirection pWow64RevertWow64FsRedirection;
|
2007-05-23 19:32:53 -07:00
|
|
|
|
|
|
|
|
static bool isWow64;
|
|
|
|
|
|
2007-05-24 05:31:16 -07:00
|
|
|
static void ImportWow64Functions()
|
2007-05-23 19:32:53 -07:00
|
|
|
{
|
2010-04-03 03:46:28 -07:00
|
|
|
const HMODULE hKernel32Dll = GetModuleHandleW(L"kernel32.dll");
|
2008-06-28 10:51:18 -07:00
|
|
|
pIsWow64Process = (PIsWow64Process)GetProcAddress(hKernel32Dll, "IsWow64Process");
|
|
|
|
|
pWow64DisableWow64FsRedirection = (PWow64DisableWow64FsRedirection)GetProcAddress(hKernel32Dll, "Wow64DisableWow64FsRedirection");
|
|
|
|
|
pWow64RevertWow64FsRedirection = (PWow64RevertWow64FsRedirection)GetProcAddress(hKernel32Dll, "Wow64RevertWow64FsRedirection");
|
2007-05-24 05:31:16 -07:00
|
|
|
}
|
2007-05-23 19:32:53 -07:00
|
|
|
|
2007-05-24 05:31:16 -07:00
|
|
|
static void DetectWow64()
|
|
|
|
|
{
|
2007-05-23 19:32:53 -07:00
|
|
|
// function not found => running on 32-bit Windows
|
|
|
|
|
if(!pIsWow64Process)
|
2005-02-01 19:26:38 -08:00
|
|
|
{
|
2007-05-23 19:32:53 -07:00
|
|
|
isWow64 = false;
|
|
|
|
|
return;
|
2005-02-01 19:26:38 -08:00
|
|
|
}
|
|
|
|
|
|
2007-05-23 19:32:53 -07:00
|
|
|
BOOL isWow64Process = FALSE;
|
2007-06-08 19:40:48 -07:00
|
|
|
const BOOL ok = pIsWow64Process(GetCurrentProcess(), &isWow64Process);
|
2007-05-23 19:32:53 -07:00
|
|
|
WARN_IF_FALSE(ok);
|
|
|
|
|
isWow64 = (isWow64Process == TRUE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool wutil_IsWow64()
|
|
|
|
|
{
|
|
|
|
|
return isWow64;
|
|
|
|
|
}
|
|
|
|
|
|
2007-12-22 10:15:52 -08:00
|
|
|
|
|
|
|
|
WinScopedDisableWow64Redirection::WinScopedDisableWow64Redirection()
|
2007-05-24 05:31:16 -07:00
|
|
|
{
|
|
|
|
|
// 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;
|
2007-12-22 10:15:52 -08:00
|
|
|
BOOL ok = pWow64DisableWow64FsRedirection(&m_wasRedirectionEnabled);
|
2007-05-24 05:31:16 -07:00
|
|
|
WARN_IF_FALSE(ok);
|
|
|
|
|
}
|
|
|
|
|
|
2007-12-22 10:15:52 -08:00
|
|
|
WinScopedDisableWow64Redirection::~WinScopedDisableWow64Redirection()
|
2007-05-24 05:31:16 -07:00
|
|
|
{
|
|
|
|
|
if(!wutil_IsWow64())
|
|
|
|
|
return;
|
2007-12-22 10:15:52 -08:00
|
|
|
BOOL ok = pWow64RevertWow64FsRedirection(m_wasRedirectionEnabled);
|
2007-05-24 05:31:16 -07:00
|
|
|
WARN_IF_FALSE(ok);
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-23 19:32:53 -07:00
|
|
|
|
2007-12-20 12:09:19 -08:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// module handle
|
|
|
|
|
|
2008-05-13 12:43:02 -07:00
|
|
|
#ifndef LIB_STATIC_LINK
|
2007-12-20 12:09:19 -08:00
|
|
|
|
|
|
|
|
HMODULE wutil_LibModuleHandle;
|
|
|
|
|
|
2008-12-17 08:32:46 -08:00
|
|
|
BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD UNUSED(reason), LPVOID UNUSED(reserved))
|
2007-12-20 12:09:19 -08:00
|
|
|
{
|
|
|
|
|
DisableThreadLibraryCalls(hInstance);
|
|
|
|
|
wutil_LibModuleHandle = hInstance;
|
|
|
|
|
return TRUE; // success (ignored unless reason == DLL_PROCESS_ATTACH)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
|
|
HMODULE wutil_LibModuleHandle = GetModuleHandle(0);
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
2007-09-23 03:15:28 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// find main window
|
|
|
|
|
|
|
|
|
|
// this is required by the error dialog and clipboard code.
|
|
|
|
|
// note that calling from wutil_Init won't work, because the app will not
|
|
|
|
|
// have created its window by then.
|
|
|
|
|
|
|
|
|
|
static HWND hAppWindow;
|
|
|
|
|
|
|
|
|
|
static BOOL CALLBACK FindAppWindowByPid(HWND hWnd, LPARAM UNUSED(lParam))
|
|
|
|
|
{
|
|
|
|
|
DWORD pid;
|
2009-07-16 16:53:46 -07:00
|
|
|
(void)GetWindowThreadProcessId(hWnd, &pid); // (function always succeeds)
|
2007-09-23 03:15:28 -07:00
|
|
|
|
|
|
|
|
if(pid == GetCurrentProcessId())
|
|
|
|
|
{
|
|
|
|
|
hAppWindow = hWnd;
|
|
|
|
|
return FALSE; // done
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return TRUE; // keep calling
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-16 16:53:46 -07:00
|
|
|
|
2007-09-23 03:15:28 -07:00
|
|
|
HWND wutil_AppWindow()
|
|
|
|
|
{
|
|
|
|
|
if(!hAppWindow)
|
|
|
|
|
{
|
2009-07-16 16:53:46 -07:00
|
|
|
// to avoid wasting time, FindAppWindowByPid returns FALSE after
|
|
|
|
|
// finding the desired window, which causes EnumWindows to 'fail'.
|
|
|
|
|
// we detect actual errors by checking GetLastError.
|
|
|
|
|
WinScopedPreserveLastError s;
|
|
|
|
|
SetLastError(0);
|
|
|
|
|
(void)EnumWindows(FindAppWindowByPid, 0); // (see above)
|
|
|
|
|
debug_assert(GetLastError() == 0);
|
2007-09-23 03:15:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return hAppWindow;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-05-23 19:32:53 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
2007-06-03 17:00:57 -07:00
|
|
|
static LibError wutil_Init()
|
2007-05-23 19:32:53 -07:00
|
|
|
{
|
2007-05-04 10:30:32 -07:00
|
|
|
InitLocks();
|
2004-11-19 14:41:37 -08:00
|
|
|
|
2007-05-23 19:32:53 -07:00
|
|
|
ForciblyLoadUser32Dll();
|
2004-11-21 23:40:32 -08:00
|
|
|
|
2007-05-23 19:32:53 -07:00
|
|
|
EnableLowFragmentationHeap();
|
|
|
|
|
|
2007-09-08 01:09:32 -07:00
|
|
|
ReadCommandLine();
|
|
|
|
|
|
2007-05-23 19:32:53 -07:00
|
|
|
GetDirectories();
|
|
|
|
|
|
2007-05-26 08:34:10 -07:00
|
|
|
DetectWindowsVersion();
|
|
|
|
|
|
2007-05-24 05:31:16 -07:00
|
|
|
ImportWow64Functions();
|
2007-05-23 19:32:53 -07:00
|
|
|
DetectWow64();
|
2005-07-16 10:52:05 -07:00
|
|
|
|
2007-05-04 10:30:32 -07:00
|
|
|
return INFO::OK;
|
2004-03-02 15:56:51 -08:00
|
|
|
}
|
|
|
|
|
|
2005-05-11 15:40:19 -07:00
|
|
|
|
2007-05-04 10:30:32 -07:00
|
|
|
static LibError wutil_Shutdown()
|
2004-03-02 15:56:51 -08:00
|
|
|
{
|
2007-09-08 01:09:32 -07:00
|
|
|
FreeCommandLine();
|
|
|
|
|
|
2007-05-23 19:32:53 -07:00
|
|
|
FreeUser32Dll();
|
2006-12-09 06:39:52 -08:00
|
|
|
|
2007-05-23 19:32:53 -07:00
|
|
|
ShutdownLocks();
|
2006-12-09 06:39:52 -08:00
|
|
|
|
2009-11-03 13:46:35 -08:00
|
|
|
FreeDirectories();
|
|
|
|
|
|
2007-05-04 10:30:32 -07:00
|
|
|
return INFO::OK;
|
2006-12-09 06:39:52 -08:00
|
|
|
}
|