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 .
22 #pragma warning(push, 1)
29 #include "internal/registry.hxx"
32 #pragma warning(push, 1)
41 // Size of a CLSID as a string
42 const int CLSID_STRING_SIZE
= 39;
46 bool SetRegistryKey(HKEY RootKey
, const char* KeyName
, const char* ValueName
, const char* Value
)
50 // open or create the desired key
52 int rc
= RegCreateKeyExA(
53 RootKey
, const_cast<char*>(KeyName
), 0, dummy
, REG_OPTION_NON_VOLATILE
, KEY_WRITE
, 0, &hSubKey
, 0);
55 if (ERROR_SUCCESS
== rc
)
58 hSubKey
, ValueName
, 0, REG_SZ
, reinterpret_cast<const BYTE
*>(Value
),
59 static_cast<DWORD
>(strlen(Value
) + 1));
64 return (ERROR_SUCCESS
== rc
);
69 bool DeleteRegistryKey(HKEY RootKey
, const char* KeyName
)
73 int rc
= RegOpenKeyExA(
80 if ( rc
== ERROR_FILE_NOT_FOUND
)
83 if (ERROR_SUCCESS
== rc
)
88 rc
= RegQueryInfoKeyA(
93 nMaxSubKeyLen
++; // space for trailing '\0'
95 SubKey
= reinterpret_cast<char*>(
96 _alloca(nMaxSubKeyLen
*sizeof(char)));
98 while (ERROR_SUCCESS
== rc
)
100 DWORD nLen
= nMaxSubKeyLen
;
104 0, // always index zero
109 if (ERROR_NO_MORE_ITEMS
== rc
)
111 rc
= RegDeleteKeyA(RootKey
, KeyName
);
114 else if (rc
== ERROR_SUCCESS
)
116 DeleteRegistryKey(hKey
, SubKey
);
125 return (ERROR_SUCCESS
== rc
);
128 /** May be used to determine if the specified registry key has subkeys
129 The function returns true on success else if an error occurs false
131 bool HasSubkeysRegistryKey(HKEY RootKey
, const char* KeyName
, /* out */ bool& bResult
)
135 LONG rc
= RegOpenKeyExA(RootKey
, KeyName
, 0, KEY_READ
, &hKey
);
137 if (ERROR_SUCCESS
== rc
)
141 rc
= RegQueryInfoKeyA(hKey
, 0, 0, 0, &nSubKeys
, 0, 0, 0, 0, 0, 0, 0);
143 bResult
= (nSubKeys
> 0);
146 return (ERROR_SUCCESS
== rc
);
149 // Convert a CLSID to a char string.
150 std::string
ClsidToString(const CLSID
& clsid
)
153 LPOLESTR wszCLSID
= NULL
;
154 StringFromCLSID(clsid
, &wszCLSID
);
157 // Covert from wide characters to non-wide.
158 wcstombs(buff
, wszCLSID
, sizeof(buff
));
161 CoTaskMemFree(wszCLSID
) ;
163 return std::string(buff
);
168 bool QueryRegistryKey(HKEY RootKey
, const char* KeyName
, const char* ValueName
, char *pszData
, DWORD dwBufLen
)
172 int rc
= RegOpenKeyExA(
179 if (ERROR_SUCCESS
== rc
)
181 rc
= RegQueryValueExA(
182 hKey
, ValueName
, NULL
, NULL
, (LPBYTE
)pszData
,&dwBufLen
);
187 return (ERROR_SUCCESS
== rc
);
190 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */