1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
27 #include <registry.hxx>
31 bool SetRegistryKey(HKEY RootKey
, const Filepath_char_t
* KeyName
, const Filepath_char_t
* ValueName
, const Filepath_char_t
* Value
)
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
)
43 hSubKey
, ValueName
, 0, REG_SZ
, reinterpret_cast<const BYTE
*>(Value
),
44 static_cast<DWORD
>((wcslen(Value
) + 1) * sizeof(*Value
)));
49 return (ERROR_SUCCESS
== rc
);
53 bool DeleteRegistryKey(HKEY RootKey
, const Filepath_char_t
* KeyName
)
57 int rc
= RegOpenKeyExW(
64 if ( rc
== ERROR_FILE_NOT_FOUND
)
67 if (ERROR_SUCCESS
== rc
)
72 rc
= RegQueryInfoKeyW(
73 hKey
, nullptr, nullptr, nullptr, nullptr,
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
;
88 0, // always index zero
91 nullptr, nullptr, nullptr, nullptr);
93 if (ERROR_NO_MORE_ITEMS
== rc
)
95 rc
= RegDeleteKeyW(RootKey
, KeyName
);
98 else if (rc
== ERROR_SUCCESS
)
100 DeleteRegistryKey(hKey
, SubKey
);
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
)
119 LONG rc
= RegOpenKeyExW(RootKey
, KeyName
, 0, KEY_READ
, &hKey
);
121 if (ERROR_SUCCESS
== rc
)
125 rc
= RegQueryInfoKeyW(hKey
, nullptr, nullptr, nullptr, &nSubKeys
, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
128 bResult
= (nSubKeys
> 0);
131 return (ERROR_SUCCESS
== rc
);
134 // Convert a CLSID to a char string.
135 Filepath_t
ClsidToString(const CLSID
& clsid
)
138 LPOLESTR wszCLSID
= nullptr;
139 StringFromCLSID(clsid
, &wszCLSID
);
141 std::wstring sResult
= wszCLSID
;
144 CoTaskMemFree(wszCLSID
) ;
150 bool QueryRegistryKey(HKEY RootKey
, const Filepath_char_t
* KeyName
, const Filepath_char_t
* ValueName
, Filepath_char_t
*pszData
, DWORD dwBufLen
)
154 int rc
= RegOpenKeyExW(
161 if (ERROR_SUCCESS
== rc
)
163 DWORD dwBytes
= dwBufLen
* sizeof(*pszData
);
164 rc
= RegQueryValueExW(
165 hKey
, ValueName
, nullptr, nullptr, reinterpret_cast<LPBYTE
>(pszData
),&dwBytes
);
170 return (ERROR_SUCCESS
== rc
);
173 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */