spiegel-keyman/windows/src/engine/kmtip/registryw.cpp
Marc Durdin 738e1946a6 [Windows] Debug logging and proof of concept tests for keyboarding support within metro-style apps
[Windows] More tidyup and robustness for metro app support - debug cleanup and serialization of input (not quite finished)

[windows] Refactor serialized input code when used with key event thread model

[windows] Add consistent precompiled headers for other projects

[windows] Merge console window test into metro support

[Windows] Tidy up work and identify additional TODOs for metro-style app support

[Windows] Ensure error case falls through to default hook processing for console windows

[Windows] Refactor shared memory into memory mapped file so we can cross 32-64 bit boundary

[Windows] Tweaks to C++ security calls and parameters

[Windows] Start refactor of SerialKeyEvent* classes

[Windows] Rename to SerialKeyEventServer (refactoring)

[Windows] Complete refactoring of SerialKeyEventClient class

[Windows] Further encapsulation and cleanup with 'interfaces' to reduce header pollution

[Windows] Complete serialization fix with move of modifier state management from client thread to server thread to guarantee consistency

[Windows] Replace atom-based keyboard switching with memory mapped file indexed to avoid security constraints

[Windows] Fixup Left Alt+Shift interaction with serializer

[Windows] Use Windows 8.1 SDK for test
2018-10-18 20:00:49 +11:00

227 lines
5.5 KiB
C++

/*
Name: registryw
Copyright: Copyright (C) 2003-2017 SIL International.
Documentation:
Description:
Create Date: 1 Dec 2012
Modified Date: 13 Dec 2012
Authors: mcdurdin
Related Files:
Dependencies:
Bugs:
Todo:
Notes:
History: 01 Dec 2012 - mcdurdin - I3622 - V9.0 - Add Registry*W classes for Unicode
13 Dec 2012 - mcdurdin - I3675 - V9.0 - Minor updates to I3622 for consistency
*/
#include "pch.h"
#include "keyman64.h" // I3622
#include "registryw.h"
RegistryReadOnlyW::RegistryReadOnlyW(HKEY AhRootKey)
{
FhRootKey = AhRootKey;
FhKey = NULL;
}
RegistryReadOnlyW::~RegistryReadOnlyW()
{
if(FhKey != NULL) CloseKey();
}
BOOL RegistryReadOnlyW::CloseKey(void)
{
BOOL Result = FALSE;
if(FhKey != NULL) Result = WrapError(RegCloseKey(FhKey));
FhKey = NULL;
return TRUE;
}
BOOL RegistryFullAccessW::CreateKey(LPCWSTR AKey)
{
DWORD dwDisposition;
return WrapError(RegCreateKeyExW(FhKey ? FhKey : FhRootKey, AKey, 0, NULL, 0, KEY_ALL_ACCESS, NULL,
&FhKey, &dwDisposition));
}
BOOL RegistryFullAccessW::RecursiveDeleteKey(LPCWSTR AKey)
{
HKEY hkey;
if(RegOpenKeyExW(FhKey ? FhKey : FhRootKey, AKey, 0, KEY_ALL_ACCESS, &hkey) != ERROR_SUCCESS)
return FALSE;
if(!IntRecursiveDeleteKey(hkey))
{
RegCloseKey(hkey);
return FALSE;
}
if(RegCloseKey(hkey) != ERROR_SUCCESS) return FALSE;
return WrapError(RegDeleteKeyW(hkey, AKey));
}
BOOL RegistryFullAccessW::IntRecursiveDeleteKey(HKEY hkey)
{
WCHAR buf[260];
DWORD dwlen = _countof(buf);
FILETIME ftime;
HKEY hsubkey;
LONG n = RegEnumKeyExW(hkey, 0, buf, &dwlen, NULL, NULL, NULL, &ftime);
while(n == ERROR_MORE_DATA || n == ERROR_SUCCESS)
{
if(RegOpenKeyExW(hkey, buf, 0, KEY_ALL_ACCESS, &hsubkey) != ERROR_SUCCESS) return FALSE;
IntRecursiveDeleteKey(hsubkey);
if(RegCloseKey(hsubkey) != ERROR_SUCCESS) return FALSE;
if(RegDeleteKeyW(hkey, buf) != ERROR_SUCCESS) return FALSE;
dwlen = _countof(buf);
n = RegEnumKeyExW(hkey, 0, buf, &dwlen, NULL, NULL, NULL, &ftime);
}
return TRUE;
}
BOOL RegistryFullAccessW::DeleteKey(LPCWSTR AKey)
{
return WrapError(RegDeleteKeyW(FhKey, AKey));
}
BOOL RegistryFullAccessW::DeleteValue(LPCWSTR AName)
{
return WrapError(RegDeleteValueW(FhKey, AName));
}
HKEY RegistryFullAccessW::GetKey(LPCWSTR AKey)
{
HKEY Result;
if(RegOpenKeyExW(FhKey ? FhKey : FhRootKey, AKey, 0, KEY_ALL_ACCESS, &Result) != ERROR_SUCCESS)
return 0;
return Result;
}
HKEY RegistryReadOnlyW::GetKeyReadOnly(LPCWSTR AKey)
{
HKEY Result;
if(RegOpenKeyExW(FhKey ? FhKey : FhRootKey, AKey, 0, KEY_READ, &Result) != ERROR_SUCCESS)
return 0;
return Result;
}
BOOL RegistryReadOnlyW::GetKeyNames(LPWSTR AKey, int len, int n)
{
DWORD dwlen = len;
FILETIME ftime;
return WrapError(RegEnumKeyExW(FhKey, n, AKey, &dwlen, NULL, NULL, NULL, &ftime));
}
BOOL RegistryReadOnlyW::GetValueNames(LPWSTR AName, int len, int n)
{
DWORD dwlen = len;
return WrapError(RegEnumValueW(FhKey, n, AName, &dwlen, NULL, NULL, NULL, NULL));
}
BOOL RegistryReadOnlyW::KeyExists(LPCWSTR AKey)
{
HKEY hkey = GetKeyReadOnly(AKey);
if(hkey != 0) RegCloseKey(hkey);
return hkey != 0;
}
BOOL RegistryFullAccessW::OpenKey(LPCWSTR AKey, BOOL ACreate)
{
if(ACreate)
if(!KeyExists(AKey))
return CreateKey(AKey);
else
return OpenKey(AKey, FALSE);
else
{
HKEY hkey = GetKey(AKey);
if(!hkey) return FALSE;
if(FhKey) CloseKey();
FhKey = hkey;
return TRUE;
}
}
BOOL RegistryReadOnlyW::OpenKeyReadOnly(LPCWSTR AKey)
{
HKEY hkey = GetKeyReadOnly(AKey);
if(!hkey) return FALSE;
if(FhKey) CloseKey();
FhKey = hkey;
return TRUE;
}
int RegistryReadOnlyW::ReadInteger(LPCWSTR AName)
{
int buf = 0;
DWORD dwlen = sizeof(int); // I3675
if(RegQueryValueExW(FhKey, AName, NULL, NULL, (LPBYTE) &buf, &dwlen) == ERROR_SUCCESS)
return buf;
return 0;
//return WrapError();
}
BOOL RegistryReadOnlyW::ReadString(LPCWSTR AName, LPWSTR AValue, int len)
{
DWORD dwlen = len * sizeof(WCHAR);
return WrapError(RegQueryValueExW(FhKey, AName, NULL, NULL, (LPBYTE) AValue, &dwlen));
}
BOOL RegistryReadOnlyW::ValueExists(LPCWSTR AName)
{
DWORD dwType;
return WrapError(RegQueryValueExW(FhKey, AName, NULL, &dwType, NULL, NULL));
}
BOOL RegistryReadOnlyW::WrapError(DWORD res)
{
/*if(res != ERROR_SUCCESS)
{
SendDebugMessageFormat(0,KDS_PROGRAM,0,"RegistryFullAccessW::WrapError = %d", res);
}*/
return res == ERROR_SUCCESS;
}
BOOL RegistryFullAccessW::WriteInteger(LPCWSTR AName, int AValue)
{
return WrapError(RegSetValueExW(FhKey, AName, 0, REG_DWORD, (LPBYTE) &AValue, sizeof(int)));
}
BOOL RegistryFullAccessW::WriteString(LPCWSTR AName, LPWSTR AValue)
{
DWORD dwLen = (DWORD) (wcslen(AValue)+1) * sizeof(WCHAR); // I3675
return WrapError(RegSetValueExW(FhKey, AName, 0, REG_SZ, (LPBYTE) AValue, dwLen));
}
RegistryReadOnlyW *Reg_GetKeymanInstalledKeyboardW(LPWSTR kbname)
{
RegistryReadOnlyW *reg = new RegistryReadOnlyW(HKEY_LOCAL_MACHINE);
if(!reg->OpenKeyReadOnly(REGSZ_KeymanInstalledKeyboardsLMW) || !reg->OpenKeyReadOnly(kbname))
{
delete reg;
reg = new RegistryReadOnlyW(HKEY_CURRENT_USER);
if(!reg->OpenKeyReadOnly(REGSZ_KeymanInstalledKeyboardsCUW) || !reg->OpenKeyReadOnly(kbname))
{
delete reg;
return NULL;
}
}
return reg;
}
RegistryReadOnlyW *Reg_GetKeymanActiveKeyboardW(LPWSTR kbname)
{
RegistryReadOnlyW *reg = new RegistryReadOnlyW(HKEY_CURRENT_USER);
if(!reg->OpenKeyReadOnly(REGSZ_KeymanActiveKeyboardsW) || !reg->OpenKeyReadOnly(kbname))
{
delete reg;
return NULL;
}
return reg;
}