From e6f83a91b85a146ecdf675f9c21e83b15f28f015 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bi=C8=99oc=20George?= Date: Fri, 3 Apr 2020 17:46:30 +0200 Subject: [PATCH] [UTILMAN] Implement the Registry management code skeleton (#2456) The following SaveAppSettings(), QueryAppSettings() and InitAppRegKey() are helper functions. This is merely a base Registry skeleton for Utility Manager as more work on it has to come later. --- base/applications/utilman/CMakeLists.txt | 1 + base/applications/utilman/precomp.h | 46 +++++- base/applications/utilman/registry.c | 202 +++++++++++++++++++++++ 3 files changed, 248 insertions(+), 1 deletion(-) create mode 100644 base/applications/utilman/registry.c diff --git a/base/applications/utilman/CMakeLists.txt b/base/applications/utilman/CMakeLists.txt index 90a909e9dd0..6f0addb1bd0 100644 --- a/base/applications/utilman/CMakeLists.txt +++ b/base/applications/utilman/CMakeLists.txt @@ -3,6 +3,7 @@ list(APPEND SOURCE dialog.c process.c about.c + registry.c precomp.h) add_rc_deps(utilman.rc ${CMAKE_CURRENT_SOURCE_DIR}/res/utilman.ico) diff --git a/base/applications/utilman/precomp.h b/base/applications/utilman/precomp.h index fba37387efc..fcfe045ea47 100644 --- a/base/applications/utilman/precomp.h +++ b/base/applications/utilman/precomp.h @@ -2,7 +2,7 @@ * PROJECT: ReactOS Utility Manager (Accessibility) * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+) * PURPOSE: Pre-compiled header file - * COPYRIGHT: Copyright 2019 Bișoc George (fraizeraust99 at gmail dot com) + * COPYRIGHT: Copyright 2019-2020 Bișoc George (fraizeraust99 at gmail dot com) */ #ifndef _UTILMAN_H @@ -50,6 +50,43 @@ typedef struct _UTILMAN_STATE BOOL bState; } UTILMAN_STATE, *PUTILMAN_STATE; +typedef struct _REGISTRY_SETTINGS +{ + /* Accessibility Registry settings */ + LPCWSTR wszAppPath; + DWORD dwAppType; + DWORD dwClientControlCode; + LPCWSTR wszAppName; + LPCWSTR wszErrorOnLaunch; + BOOL bHideClient; + BOOL bStartWithUtilman; + BOOL bStartWithROS; + LPCWSTR wszHungRespondAction; + DWORD dwHungTimeOut; + + /* Utility Manager Registry settings */ + BOOL bShowWarning; +} REGISTRY_SETTINGS, *PREGISTRY_SETTINGS; + +typedef struct _REGISTRY_DATA +{ + /* On-Screen Keyboard Registry data */ + LPCWSTR lpwsOskPath; + LPCWSTR lpwszOskDisplayName; + + /* Magnify Registry data */ + LPCWSTR lpwszMagnifyPath; + LPCWSTR lpwszMagnifyDisplayName; +} REGISTRY_DATA, *PREGISTRY_DATA; + +/* ENUMERATIONS ***************************************************************/ + +typedef enum _WRITE_REGISTRY +{ + REGISTRY_ACCESSIBILITY, + REGISTRY_UTILMAN +} WRITE_REGISTRY, *PWRITE_REGISTRY; + /* DECLARATIONS ***************************************************************/ /* dialog.c */ @@ -68,8 +105,15 @@ BOOL CloseProcess(IN LPCWSTR lpProcessName); VOID ShowAboutDlg(HWND hDlgParent); INT_PTR CALLBACK AboutDlgProc(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam); +/* registry.c */ +BOOL InitAppRegKey(IN HKEY hPredefinedKey, IN LPCWSTR lpwszSubKey, OUT PHKEY phKey, OUT LPDWORD lpdwDisposition); +BOOL QueryAppSettings(IN HKEY hKey, IN LPCWSTR lpwszSubKey, IN LPCWSTR lpwszRegValue, OUT PVOID ReturnedData, IN OUT LPDWORD lpdwSizeData); +BOOL SaveAppSettings(IN HKEY hKey, IN LPCWSTR lpwszRegValue, IN DWORD dwRegType, IN PVOID Data, IN DWORD cbSize); + /* Struct variable declaration */ extern UTILMAN_GLOBALS Globals; +extern REGISTRY_SETTINGS Settings; +extern REGISTRY_DATA RegData; #endif /* _UTILMAN_H */ diff --git a/base/applications/utilman/registry.c b/base/applications/utilman/registry.c new file mode 100644 index 00000000000..d7a31781260 --- /dev/null +++ b/base/applications/utilman/registry.c @@ -0,0 +1,202 @@ +/* + * PROJECT: ReactOS Utility Manager (Accessibility) + * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+) + * PURPOSE: Registry functions for Utility Manager settings management + * COPYRIGHT: Copyright 2020 Bișoc George (fraizeraust99 at gmail dot com) + */ + +/* INCLUDES *******************************************************************/ + +#include "precomp.h" + +/* GLOBALS ********************************************************************/ + +UTILMAN_GLOBALS Globals; +REGISTRY_DATA RegData; +REGISTRY_SETTINGS Settings; + +/* DEFINES ********************************************************************/ + +#define ACCESS_UTILMAN_KEY L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Accessibility\\Utility Manager" +#define UTILMAN_KEY L"SOFTWARE\\Microsoft\\Utility Manager" +#define OSK_KEY L"On-Screen Keyboard" +#define MAGNIFIER_KEY L"Magnifier" + +/* FUNCTIONS ******************************************************************/ + +/** + * @InitAppRegKey + * + * Initialize a key. The function may not necessarily create it but open the key + * if it already exists. The disposition pointed lpdwDisposition determines that. + * This is a function helper. + * + * @param[in] hPredefinedKey + * The predefined key (e.g. HKEY_CLASSES_ROOT). + * + * @param[in] lpwszSubKey + * The path to the sub key to be created. + * + * @param[out] phKey + * A pointer that receives a handle to the key given by the function. + * + * @param[out] lpdwDisposition + * A pointer that receives the disposition given by the function. + * + * @return + * Returns TRUE if the function successfully created a key (or opened it), + * FALSE otherwise for failure. + * + */ +BOOL InitAppRegKey(IN HKEY hPredefinedKey, + IN LPCWSTR lpwszSubKey, + OUT PHKEY phKey, + OUT LPDWORD lpdwDisposition) +{ + LONG lResult; + + lResult = RegCreateKeyExW(hPredefinedKey, + lpwszSubKey, + 0, + NULL, + 0, + KEY_WRITE, + NULL, + phKey, + lpdwDisposition); + if (lResult != ERROR_SUCCESS) + { + DPRINT("InitAppRegKey(): Failed to create the following key (or open the key) of path \"%S\". The error code is \"%li\".\n", lpwszSubKey, lResult); + return FALSE; + } + + return TRUE; +} + +/** + * @QueryAppSettings + * + * Query the setting from the application's key. This is a function helper. + * + * @param[in] hKey + * A handle to a key. + * + * @param[in] lpwszSubKey + * The path to a sub-key. + * + * @param[in] lpwszRegValue + * The registry value where we need to get the data from. + * + * @param[out] ReturnedData + * An arbitrary pointer that receives the returned data. Being arbitrary, + * the data can be of any type. + * + * @param[inout] lpdwSizeData + * A pointer to the returned data pointed by ReturnedData parameter that + * retrieves the size of the aforementioned data, in bytes. + * + * @return + * Returns TRUE if the function successfully loaded the value we wanted, + * FALSE otherwise for failure. + * + */ +BOOL QueryAppSettings(IN HKEY hKey, + IN LPCWSTR lpwszSubKey, + IN LPCWSTR lpwszRegValue, + OUT PVOID ReturnedData, + IN OUT LPDWORD lpdwSizeData) +{ + LONG lResult; + HKEY hKeyQueryValue; + + lResult = RegOpenKeyExW(hKey, + lpwszSubKey, + 0, + KEY_READ, + &hKeyQueryValue); + if (lResult != ERROR_SUCCESS) + { + DPRINT("QueryAppSettings(): Failed to open the key of path \"%S\". The error code is \"%li\".\n", lpwszSubKey, lResult); + return FALSE; + } + + lResult = RegQueryValueExW(hKeyQueryValue, + lpwszRegValue, + NULL, + NULL, + (LPBYTE)&ReturnedData, + lpdwSizeData); + if (lResult != ERROR_SUCCESS) + { + DPRINT("QueryAppSettings(): Failed to query the data from value \"%S\". The error code is \"%li\".\n", lpwszRegValue, lResult); + RegCloseKey(hKeyQueryValue); + return FALSE; + } + + RegCloseKey(hKeyQueryValue); + return TRUE; +} + +/** + * @SaveAppSettings + * + * Save an application's setting data to the Registry. This is a function helper. + * + * @param[in] hKey + * A handle to a key. + * + * @param[in] lpwszRegValue + * The path to the sub key where the value needs to be created. + * + * @param[out] dwRegType + * The type of registry value to be created (e.g. a REG_DWORD). + * + * @param[in] Data + * A pointer to an arbitrary data for the value to be set. Being arbitrary, + * the data can be of any type (in conformity with the registry type pointed by + * dwRegType) otherwise the function might lead to a undefined behaviour. + * + * @param[in] cbSize + * The size of the buffer data pointed by Data parameter, in bytes. + * + * @return + * Returns TRUE if the function successfully saved the application's setting, + * FALSE otherwise for failure. + * + */ +BOOL SaveAppSettings(IN HKEY hKey, + IN LPCWSTR lpwszRegValue, + IN DWORD dwRegType, + IN PVOID Data, + IN DWORD cbSize) +{ + LONG lResult; + HKEY hKeySetValue; + + lResult = RegOpenKeyExW(hKey, + NULL, + 0, + KEY_SET_VALUE, + &hKeySetValue); + if (lResult != ERROR_SUCCESS) + { + DPRINT("SaveAppSettings(): Failed to open the key, the error code is \"%li\"!\n", lResult); + return FALSE; + } + + lResult = RegSetValueExW(hKeySetValue, + lpwszRegValue, + 0, + dwRegType, + (LPBYTE)&Data, + cbSize); + if (lResult != ERROR_SUCCESS) + { + DPRINT("SaveAppSettings(): Failed to set the \"%S\" value with data, the error code is \"%li\"!\n", lpwszRegValue, lResult); + RegCloseKey(hKeySetValue); + return FALSE; + } + + RegCloseKey(hKeySetValue); + return TRUE; +}