LibreOffice Module shell (master) 1
registry.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20
21#if !defined WIN32_LEAN_AND_MEAN
22# define WIN32_LEAN_AND_MEAN
23#endif
24#include <windows.h>
25
26#include <malloc.h>
27#include <registry.hxx>
28
29#include <objbase.h>
30
31bool SetRegistryKey(HKEY RootKey, const Filepath_char_t* KeyName, const Filepath_char_t* ValueName, const Filepath_char_t* Value)
32{
33 HKEY hSubKey;
34
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);
39
40 if (ERROR_SUCCESS == rc)
41 {
42 rc = RegSetValueExW(
43 hSubKey, ValueName, 0, REG_SZ, reinterpret_cast<const BYTE*>(Value),
44 static_cast<DWORD>((wcslen(Value) + 1) * sizeof(*Value)));
45
46 RegCloseKey(hSubKey);
47 }
48
49 return (ERROR_SUCCESS == rc);
50}
51
52
53bool DeleteRegistryKey(HKEY RootKey, const Filepath_char_t* KeyName)
54{
55 HKEY hKey;
56
57 int rc = RegOpenKeyExW(
58 RootKey,
59 KeyName,
60 0,
61 KEY_READ | DELETE,
62 &hKey);
63
64 if ( rc == ERROR_FILE_NOT_FOUND )
65 return true;
66
67 if (ERROR_SUCCESS == rc)
68 {
69 wchar_t* SubKey;
70 DWORD nMaxSubKeyLen;
71
72 rc = RegQueryInfoKeyW(
73 hKey, nullptr, nullptr, nullptr, nullptr,
74 &nMaxSubKeyLen,
75 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
76
77 nMaxSubKeyLen++; // space for trailing '\0'
78
79 SubKey = static_cast<wchar_t*>(
80 _alloca(nMaxSubKeyLen*sizeof(wchar_t)));
81
82 while (ERROR_SUCCESS == rc)
83 {
84 DWORD nLen = nMaxSubKeyLen;
85
86 rc = RegEnumKeyExW(
87 hKey,
88 0, // always index zero
89 SubKey,
90 &nLen,
91 nullptr, nullptr, nullptr, nullptr);
92
93 if (ERROR_NO_MORE_ITEMS == rc)
94 {
95 rc = RegDeleteKeyW(RootKey, KeyName);
96 break;
97 }
98 else if (rc == ERROR_SUCCESS)
99 {
100 DeleteRegistryKey(hKey, SubKey);
101 }
102
103 } // while
104
105 RegCloseKey(hKey);
106
107 } // if
108
109 return (ERROR_SUCCESS == rc);
110}
111
115bool HasSubkeysRegistryKey(HKEY RootKey, const Filepath_char_t* KeyName, /* out */ bool& bResult)
116{
117 HKEY hKey;
118
119 LONG rc = RegOpenKeyExW(RootKey, KeyName, 0, KEY_READ, &hKey);
120
121 if (ERROR_SUCCESS == rc)
122 {
123 DWORD nSubKeys = 0;
124
125 rc = RegQueryInfoKeyW(hKey, nullptr, nullptr, nullptr, &nSubKeys, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
126
127 RegCloseKey(hKey);
128 bResult = (nSubKeys > 0);
129 }
130
131 return (ERROR_SUCCESS == rc);
132}
133
134// Convert a CLSID to a char string.
135Filepath_t ClsidToString(const CLSID& clsid)
136{
137 // Get CLSID
138 LPOLESTR wszCLSID = nullptr;
139 StringFromCLSID(clsid, &wszCLSID);
140
141 std::wstring sResult = wszCLSID;
142
143 // Free memory.
144 CoTaskMemFree(wszCLSID) ;
145
146 return sResult;
147}
148
149
150bool QueryRegistryKey(HKEY RootKey, const Filepath_char_t* KeyName, const Filepath_char_t* ValueName, Filepath_char_t *pszData, DWORD dwBufLen)
151{
152 HKEY hKey;
153
154 int rc = RegOpenKeyExW(
155 RootKey,
156 KeyName,
157 0,
158 KEY_READ,
159 &hKey);
160
161 if (ERROR_SUCCESS == rc)
162 {
163 DWORD dwBytes = dwBufLen * sizeof(*pszData);
164 rc = RegQueryValueExW(
165 hKey, ValueName, nullptr, nullptr, reinterpret_cast<LPBYTE>(pszData),&dwBytes);
166
167 RegCloseKey(hKey);
168 }
169
170 return (ERROR_SUCCESS == rc);
171}
172
173/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
char Filepath_char_t
Definition: filepath.hxx:21
std::string Filepath_t
Definition: filepath.hxx:22
Value
Filepath_t ClsidToString(const CLSID &clsid)
Converts a GUID to its string representation.
Definition: registry.cxx:135
bool HasSubkeysRegistryKey(HKEY RootKey, const Filepath_char_t *KeyName, bool &bResult)
May be used to determine if the specified registry key has subkeys The function returns true on succe...
Definition: registry.cxx:115
bool QueryRegistryKey(HKEY RootKey, const Filepath_char_t *KeyName, const Filepath_char_t *ValueName, Filepath_char_t *pszData, DWORD dwBufLen)
Get the content of a specific key.
Definition: registry.cxx:150
bool DeleteRegistryKey(HKEY RootKey, const Filepath_char_t *KeyName)
Deletes the specified registry key and all of its subkeys Returns true on success.
Definition: registry.cxx:53
bool SetRegistryKey(HKEY RootKey, const Filepath_char_t *KeyName, const Filepath_char_t *ValueName, const Filepath_char_t *Value)
Sets a value of the specified registry key, an empty ValueName sets the default value Returns true on...
Definition: registry.cxx:31
LONG
unsigned char BYTE