shell/inc/registry.hxx | 8 - shell/source/win32/shlxthandler/shlxthdl.cxx | 44 +------ shell/source/win32/shlxthandler/util/registry.cxx | 133 ++-------------------- vcl/win/window/salframe.cxx | 41 ++---- 4 files changed, 41 insertions(+), 185 deletions(-)
New commits: commit 7de85014812ae6b3fdcd5c846ea097728fd9bcab Author: Mike Kaganski <mike.kagan...@collabora.com> AuthorDate: Sat Jun 15 19:40:06 2024 +0500 Commit: Xisco Fauli <xiscofa...@libreoffice.org> CommitDate: Mon Jun 17 14:15:05 2024 +0200 Simplify a bit using newer registry API Change-Id: I0bd2a238662e1da0c62cdaec96b9892df82298f6 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/168909 Tested-by: Jenkins Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com> Signed-off-by: Xisco Fauli <xiscofa...@libreoffice.org> Reviewed-on: https://gerrit.libreoffice.org/c/core/+/168991 diff --git a/vcl/win/window/salframe.cxx b/vcl/win/window/salframe.cxx index 288b561970f0..ebcb85f55cff 100644 --- a/vcl/win/window/salframe.cxx +++ b/vcl/win/window/salframe.cxx @@ -2652,22 +2652,15 @@ void WinSalFrame::UpdateSettings( AllSettings& rSettings ) aMouseSettings.SetStartDragWidth( nDragWidth ); if ( nDragHeight ) aMouseSettings.SetStartDragHeight( nDragHeight ); - HKEY hRegKey; - if ( RegOpenKeyW( HKEY_CURRENT_USER, - L"Control Panel\Desktop", - &hRegKey ) == ERROR_SUCCESS ) { wchar_t aValueBuf[10]; DWORD nValueSize = sizeof( aValueBuf ); - DWORD nType; - if ( RegQueryValueExW( hRegKey, L"MenuShowDelay", nullptr, - &nType, reinterpret_cast<LPBYTE>(aValueBuf), &nValueSize ) == ERROR_SUCCESS ) + if (RegGetValueW(HKEY_CURRENT_USER, L"Control Panel\Desktop", L"MenuShowDelay", + RRF_RT_REG_SZ, nullptr, reinterpret_cast<LPVOID>(aValueBuf), &nValueSize) + == ERROR_SUCCESS) { - if ( nType == REG_SZ ) - aMouseSettings.SetMenuDelay( static_cast<sal_uLong>(ImplW2I( aValueBuf )) ); + aMouseSettings.SetMenuDelay( static_cast<sal_uLong>(ImplW2I( aValueBuf )) ); } - - RegCloseKey( hRegKey ); } StyleSettings aStyleSettings = rSettings.GetStyleSettings(); @@ -2949,30 +2942,22 @@ void WinSalFrame::UpdateSettings( AllSettings& rSettings ) aStyleSettings.SetDragFullOptions( nDragFullOptions ); } - if ( RegOpenKeyW( HKEY_CURRENT_USER, - L"Control Panel\International\Calendars\TwoDigitYearMax", - &hRegKey ) == ERROR_SUCCESS ) { wchar_t aValueBuf[10]; - DWORD nValue; DWORD nValueSize = sizeof( aValueBuf ); - DWORD nType; - if ( RegQueryValueExW( hRegKey, L"1", nullptr, - &nType, reinterpret_cast<LPBYTE>(aValueBuf), &nValueSize ) == ERROR_SUCCESS ) + if (RegGetValueW(HKEY_CURRENT_USER, + L"Control Panel\International\Calendars\TwoDigitYearMax", L"1", + RRF_RT_REG_SZ, nullptr, reinterpret_cast<LPVOID>(aValueBuf), &nValueSize) + == ERROR_SUCCESS) { - if ( nType == REG_SZ ) + DWORD nValue = static_cast<sal_uLong>(ImplW2I(aValueBuf)); + if ((nValue > 1000) && (nValue < 10000)) { - nValue = static_cast<sal_uLong>(ImplW2I( aValueBuf )); - if ( (nValue > 1000) && (nValue < 10000) ) - { - std::shared_ptr<comphelper::ConfigurationChanges> batch(comphelper::ConfigurationChanges::create()); - officecfg::Office::Common::DateFormat::TwoDigitYear::set(static_cast<sal_Int32>(nValue-99), batch); - batch->commit(); - } + std::shared_ptr<comphelper::ConfigurationChanges> batch(comphelper::ConfigurationChanges::create()); + officecfg::Office::Common::DateFormat::TwoDigitYear::set(static_cast<sal_Int32>(nValue-99), batch); + batch->commit(); } } - - RegCloseKey( hRegKey ); } rSettings.SetMouseSettings( aMouseSettings ); commit fc12c5f452f47cba64b585f5487ed4e120e849dc Author: Mike Kaganski <mike.kagan...@collabora.com> AuthorDate: Sat Jun 15 18:33:16 2024 +0500 Commit: Xisco Fauli <xiscofa...@libreoffice.org> CommitDate: Mon Jun 17 14:14:58 2024 +0200 Simplify a bit using newer registry API Change-Id: I8e0106464260126983a1d55c66c218e78da7f94e Reviewed-on: https://gerrit.libreoffice.org/c/core/+/168908 Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com> Tested-by: Jenkins Signed-off-by: Xisco Fauli <xiscofa...@libreoffice.org> Reviewed-on: https://gerrit.libreoffice.org/c/core/+/168990 diff --git a/shell/inc/registry.hxx b/shell/inc/registry.hxx index 3ea0d85f74b8..d5228fd5fff3 100644 --- a/shell/inc/registry.hxx +++ b/shell/inc/registry.hxx @@ -37,12 +37,12 @@ bool SetRegistryKey(HKEY RootKey, const Filepath_char_t* KeyName, const Filepath /** Deletes the specified registry key and all of its subkeys Returns true on success */ -bool DeleteRegistryKey(HKEY RootKey, const Filepath_char_t* KeyName); +bool DeleteRegistryTree(HKEY RootKey, const Filepath_char_t* KeyName); -/** May be used to determine if the specified registry key has subkeys - The function returns true on success else if an error occurs false +/** Deletes the specified registry key (only if it has no subkeys) + Returns true on success */ -bool HasSubkeysRegistryKey(HKEY RootKey, const Filepath_char_t* KeyName, bool& bResult); +bool DeleteRegistryKey(HKEY RootKey, const Filepath_char_t* KeyName); /** Converts a GUID to its string representation */ diff --git a/shell/source/win32/shlxthandler/shlxthdl.cxx b/shell/source/win32/shlxthandler/shlxthdl.cxx index d794e9f800b7..b487e31a57ac 100644 --- a/shell/source/win32/shlxthandler/shlxthdl.cxx +++ b/shell/source/win32/shlxthandler/shlxthdl.cxx @@ -91,7 +91,7 @@ namespace /* private */ { std::wstring tmp = L"CLSID\"; tmp += ClsidToString(Guid); - return DeleteRegistryKey(HKEY_CLASSES_ROOT, tmp.c_str()) ? S_OK : E_FAIL; + return DeleteRegistryTree(HKEY_CLASSES_ROOT, tmp.c_str()) ? S_OK : E_FAIL; } HRESULT RegisterColumnHandler(const wchar_t* ModuleFileName) @@ -114,7 +114,7 @@ namespace /* private */ std::wstring tmp = L"Folder\shellex\ColumnHandlers\"; tmp += ClsidToString(CLSID_COLUMN_HANDLER); - if (!DeleteRegistryKey(HKEY_CLASSES_ROOT, tmp.c_str())) + if (!DeleteRegistryTree(HKEY_CLASSES_ROOT, tmp.c_str())) return E_FAIL; return UnregisterComComponent(CLSID_COLUMN_HANDLER); @@ -152,16 +152,14 @@ namespace /* private */ SubstitutePlaceholder(tmp, EXTENSION_PLACEHOLDER, OOFileExtensionTable[i].ExtensionU); SubstitutePlaceholder(tmp, GUID_PLACEHOLDER, iid); - DeleteRegistryKey(HKEY_CLASSES_ROOT, tmp.c_str()); + DeleteRegistryTree(HKEY_CLASSES_ROOT, tmp.c_str()); // if there are no further subkey below .ext\shellex // delete the whole subkey tmp = SHELLEX_ENTRY; SubstitutePlaceholder(tmp, EXTENSION_PLACEHOLDER, OOFileExtensionTable[i].ExtensionU); - bool HasSubKeys = true; - if (HasSubkeysRegistryKey(HKEY_CLASSES_ROOT, tmp.c_str(), HasSubKeys) && !HasSubKeys) - DeleteRegistryKey(HKEY_CLASSES_ROOT, tmp.c_str()); + DeleteRegistryKey(HKEY_CLASSES_ROOT, tmp.c_str()); } return UnregisterComComponent(CLSID_INFOTIP_HANDLER); } @@ -193,21 +191,17 @@ namespace /* private */ FwdKeyEntry = FORWARD_PROPSHEET_MYPROPSHEET_ENTRY; SubstitutePlaceholder(FwdKeyEntry, FORWARDKEY_PLACEHOLDER, OOFileExtensionTable[i].RegistryForwardKey); - DeleteRegistryKey(HKEY_CLASSES_ROOT, FwdKeyEntry.c_str()); + DeleteRegistryTree(HKEY_CLASSES_ROOT, FwdKeyEntry.c_str()); FwdKeyEntry = FORWARD_PROPSHEET_ENTRY; SubstitutePlaceholder(FwdKeyEntry, FORWARDKEY_PLACEHOLDER, OOFileExtensionTable[i].RegistryForwardKey); - bool HasSubKeys = true; - if (HasSubkeysRegistryKey(HKEY_CLASSES_ROOT, FwdKeyEntry.c_str(), HasSubKeys) && !HasSubKeys) - DeleteRegistryKey(HKEY_CLASSES_ROOT, FwdKeyEntry.c_str()); + DeleteRegistryKey(HKEY_CLASSES_ROOT, FwdKeyEntry.c_str()); FwdKeyEntry = FORWARD_SHELLEX_ENTRY; SubstitutePlaceholder(FwdKeyEntry, FORWARDKEY_PLACEHOLDER, OOFileExtensionTable[i].RegistryForwardKey); - HasSubKeys = true; - if (HasSubkeysRegistryKey(HKEY_CLASSES_ROOT, FwdKeyEntry.c_str(), HasSubKeys) && !HasSubKeys) - DeleteRegistryKey(HKEY_CLASSES_ROOT, FwdKeyEntry.c_str()); + DeleteRegistryKey(HKEY_CLASSES_ROOT, FwdKeyEntry.c_str()); } return UnregisterComComponent(CLSID_PROPERTYSHEET_HANDLER); @@ -246,16 +240,14 @@ namespace /* private */ SubstitutePlaceholder(tmp, EXTENSION_PLACEHOLDER, OOFileExtensionTable[i].ExtensionU); SubstitutePlaceholder(tmp, GUID_PLACEHOLDER, iid); - DeleteRegistryKey(HKEY_CLASSES_ROOT, tmp.c_str()); + DeleteRegistryTree(HKEY_CLASSES_ROOT, tmp.c_str()); // if there are no further subkey below .ext\shellex // delete the whole subkey tmp = SHELLEX_ENTRY; SubstitutePlaceholder(tmp, EXTENSION_PLACEHOLDER, OOFileExtensionTable[i].ExtensionU); - bool HasSubKeys = true; - if (HasSubkeysRegistryKey(HKEY_CLASSES_ROOT, tmp.c_str(), HasSubKeys) && !HasSubKeys) - DeleteRegistryKey(HKEY_CLASSES_ROOT, tmp.c_str()); + DeleteRegistryKey(HKEY_CLASSES_ROOT, tmp.c_str()); } return UnregisterComComponent(CLSID_THUMBVIEWER_HANDLER); } @@ -275,22 +267,8 @@ namespace /* private */ HRESULT UnapproveShellExtension(const CLSID& Clsid) { - HKEY hkey; - - LONG rc = RegOpenKeyW( - HKEY_LOCAL_MACHINE, - SHELL_EXTENSION_APPROVED_KEY_NAME, - &hkey); - - if (ERROR_SUCCESS == rc) - { - rc = RegDeleteValueW( - hkey, - ClsidToString(Clsid).c_str()); - - rc |= RegCloseKey(hkey); - } - + LSTATUS rc = RegDeleteKeyValueW(HKEY_LOCAL_MACHINE, SHELL_EXTENSION_APPROVED_KEY_NAME, + ClsidToString(Clsid).c_str()); return rc == ERROR_SUCCESS ? S_OK : E_FAIL; } diff --git a/shell/source/win32/shlxthandler/util/registry.cxx b/shell/source/win32/shlxthandler/util/registry.cxx index cc42c092e9c8..5e4f73797606 100644 --- a/shell/source/win32/shlxthandler/util/registry.cxx +++ b/shell/source/win32/shlxthandler/util/registry.cxx @@ -30,143 +30,36 @@ bool SetRegistryKey(HKEY RootKey, const Filepath_char_t* KeyName, const Filepath_char_t* ValueName, const Filepath_char_t* Value) { - HKEY hSubKey; - - // open or create the desired key - wchar_t dummy[] = L""; - int rc = RegCreateKeyExW( - RootKey, KeyName, 0, dummy, REG_OPTION_NON_VOLATILE, KEY_WRITE, nullptr, &hSubKey, nullptr); - - if (ERROR_SUCCESS == rc) - { - rc = RegSetValueExW( - hSubKey, ValueName, 0, REG_SZ, reinterpret_cast<const BYTE*>(Value), - static_cast<DWORD>((wcslen(Value) + 1) * sizeof(*Value))); - - RegCloseKey(hSubKey); - } - + int rc = RegSetKeyValueW(RootKey, KeyName, ValueName, REG_SZ, reinterpret_cast<LPCVOID>(Value), + (wcslen(Value) + 1) * sizeof(*Value)); return (ERROR_SUCCESS == rc); } - -bool DeleteRegistryKey(HKEY RootKey, const Filepath_char_t* KeyName) +bool DeleteRegistryTree(HKEY RootKey, const Filepath_char_t* KeyName) { - HKEY hKey; - - int rc = RegOpenKeyExW( - RootKey, - KeyName, - 0, - KEY_READ | DELETE, - &hKey); - - if ( rc == ERROR_FILE_NOT_FOUND ) - return true; - - if (ERROR_SUCCESS == rc) - { - wchar_t* SubKey; - DWORD nMaxSubKeyLen; - - rc = RegQueryInfoKeyW( - hKey, nullptr, nullptr, nullptr, nullptr, - &nMaxSubKeyLen, - nullptr, nullptr, nullptr, nullptr, nullptr, nullptr); - - nMaxSubKeyLen++; // space for trailing ' - - SubKey = static_cast<wchar_t*>( - _alloca(nMaxSubKeyLen*sizeof(wchar_t))); - - while (ERROR_SUCCESS == rc) - { - DWORD nLen = nMaxSubKeyLen; - - rc = RegEnumKeyExW( - hKey, - 0, // always index zero - SubKey, - &nLen, - nullptr, nullptr, nullptr, nullptr); - - if (ERROR_NO_MORE_ITEMS == rc) - { - rc = RegDeleteKeyW(RootKey, KeyName); - break; - } - else if (rc == ERROR_SUCCESS) - { - DeleteRegistryKey(hKey, SubKey); - } - - } // while - - RegCloseKey(hKey); - - } // if - - return (ERROR_SUCCESS == rc); + return (ERROR_SUCCESS == RegDeleteTreeW(RootKey, KeyName)); } -/** May be used to determine if the specified registry key has subkeys - The function returns true on success else if an error occurs false -*/ -bool HasSubkeysRegistryKey(HKEY RootKey, const Filepath_char_t* KeyName, /* out */ bool& bResult) +bool DeleteRegistryKey(HKEY RootKey, const Filepath_char_t* KeyName) { - HKEY hKey; - - LONG rc = RegOpenKeyExW(RootKey, KeyName, 0, KEY_READ, &hKey); - - if (ERROR_SUCCESS == rc) - { - DWORD nSubKeys = 0; - - rc = RegQueryInfoKeyW(hKey, nullptr, nullptr, nullptr, &nSubKeys, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr); - - RegCloseKey(hKey); - bResult = (nSubKeys > 0); - } - - return (ERROR_SUCCESS == rc); + return (ERROR_SUCCESS == RegDeleteKeyW(RootKey, KeyName)); } // Convert a CLSID to a char string. Filepath_t ClsidToString(const CLSID& clsid) { - // Get CLSID - LPOLESTR wszCLSID = nullptr; - StringFromCLSID(clsid, &wszCLSID); - - std::wstring sResult = wszCLSID; - - // Free memory. - CoTaskMemFree(wszCLSID) ; - - return sResult; + wchar_t wszCLSID[std::size(L"{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}")]; + if (StringFromGUID2(clsid, wszCLSID, std::size(wszCLSID))) + return std::wstring(wszCLSID); + return {}; } bool QueryRegistryKey(HKEY RootKey, const Filepath_char_t* KeyName, const Filepath_char_t* ValueName, Filepath_char_t *pszData, DWORD dwBufLen) { - HKEY hKey; - - int rc = RegOpenKeyExW( - RootKey, - KeyName, - 0, - KEY_READ, - &hKey); - - if (ERROR_SUCCESS == rc) - { - DWORD dwBytes = dwBufLen * sizeof(*pszData); - rc = RegQueryValueExW( - hKey, ValueName, nullptr, nullptr, reinterpret_cast<LPBYTE>(pszData),&dwBytes); - - RegCloseKey(hKey); - } - + DWORD dwBytes = dwBufLen * sizeof(*pszData); + LSTATUS rc = RegGetValueW(RootKey, KeyName, ValueName, RRF_RT_REG_SZ, nullptr, + reinterpret_cast<LPBYTE>(pszData), &dwBytes); return (ERROR_SUCCESS == rc); }