Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / shell / source / win32 / shlxthandler / util / registry.cxx
blobcc42c092e9c8d405e5365781c95823cb37015b6d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #if !defined WIN32_LEAN_AND_MEAN
22 # define WIN32_LEAN_AND_MEAN
23 #endif
24 #include <windows.h>
26 #include <malloc.h>
27 #include <registry.hxx>
29 #include <objbase.h>
31 bool SetRegistryKey(HKEY RootKey, const Filepath_char_t* KeyName, const Filepath_char_t* ValueName, const Filepath_char_t* Value)
33 HKEY hSubKey;
35 // open or create the desired key
36 wchar_t dummy[] = L"";
37 int rc = RegCreateKeyExW(
38 RootKey, KeyName, 0, dummy, REG_OPTION_NON_VOLATILE, KEY_WRITE, nullptr, &hSubKey, nullptr);
40 if (ERROR_SUCCESS == rc)
42 rc = RegSetValueExW(
43 hSubKey, ValueName, 0, REG_SZ, reinterpret_cast<const BYTE*>(Value),
44 static_cast<DWORD>((wcslen(Value) + 1) * sizeof(*Value)));
46 RegCloseKey(hSubKey);
49 return (ERROR_SUCCESS == rc);
53 bool DeleteRegistryKey(HKEY RootKey, const Filepath_char_t* KeyName)
55 HKEY hKey;
57 int rc = RegOpenKeyExW(
58 RootKey,
59 KeyName,
61 KEY_READ | DELETE,
62 &hKey);
64 if ( rc == ERROR_FILE_NOT_FOUND )
65 return true;
67 if (ERROR_SUCCESS == rc)
69 wchar_t* SubKey;
70 DWORD nMaxSubKeyLen;
72 rc = RegQueryInfoKeyW(
73 hKey, nullptr, nullptr, nullptr, nullptr,
74 &nMaxSubKeyLen,
75 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
77 nMaxSubKeyLen++; // space for trailing '\0'
79 SubKey = static_cast<wchar_t*>(
80 _alloca(nMaxSubKeyLen*sizeof(wchar_t)));
82 while (ERROR_SUCCESS == rc)
84 DWORD nLen = nMaxSubKeyLen;
86 rc = RegEnumKeyExW(
87 hKey,
88 0, // always index zero
89 SubKey,
90 &nLen,
91 nullptr, nullptr, nullptr, nullptr);
93 if (ERROR_NO_MORE_ITEMS == rc)
95 rc = RegDeleteKeyW(RootKey, KeyName);
96 break;
98 else if (rc == ERROR_SUCCESS)
100 DeleteRegistryKey(hKey, SubKey);
103 } // while
105 RegCloseKey(hKey);
107 } // if
109 return (ERROR_SUCCESS == rc);
112 /** May be used to determine if the specified registry key has subkeys
113 The function returns true on success else if an error occurs false
115 bool HasSubkeysRegistryKey(HKEY RootKey, const Filepath_char_t* KeyName, /* out */ bool& bResult)
117 HKEY hKey;
119 LONG rc = RegOpenKeyExW(RootKey, KeyName, 0, KEY_READ, &hKey);
121 if (ERROR_SUCCESS == rc)
123 DWORD nSubKeys = 0;
125 rc = RegQueryInfoKeyW(hKey, nullptr, nullptr, nullptr, &nSubKeys, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
127 RegCloseKey(hKey);
128 bResult = (nSubKeys > 0);
131 return (ERROR_SUCCESS == rc);
134 // Convert a CLSID to a char string.
135 Filepath_t ClsidToString(const CLSID& clsid)
137 // Get CLSID
138 LPOLESTR wszCLSID = nullptr;
139 StringFromCLSID(clsid, &wszCLSID);
141 std::wstring sResult = wszCLSID;
143 // Free memory.
144 CoTaskMemFree(wszCLSID) ;
146 return sResult;
150 bool QueryRegistryKey(HKEY RootKey, const Filepath_char_t* KeyName, const Filepath_char_t* ValueName, Filepath_char_t *pszData, DWORD dwBufLen)
152 HKEY hKey;
154 int rc = RegOpenKeyExW(
155 RootKey,
156 KeyName,
158 KEY_READ,
159 &hKey);
161 if (ERROR_SUCCESS == rc)
163 DWORD dwBytes = dwBufLen * sizeof(*pszData);
164 rc = RegQueryValueExW(
165 hKey, ValueName, nullptr, nullptr, reinterpret_cast<LPBYTE>(pszData),&dwBytes);
167 RegCloseKey(hKey);
170 return (ERROR_SUCCESS == rc);
173 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */