Bump version to 5.0-14
[LibreOffice.git] / shell / source / win32 / shlxthandler / util / registry.cxx
blob256ebd16c17c61b2a3e8cea5b463e1b7964ea0e9
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 _MSC_VER
22 #pragma warning(push, 1)
23 #endif
24 #include <windows.h>
25 #if defined _MSC_VER
26 #pragma warning(pop)
27 #endif
28 #include <malloc.h>
29 #include "internal/registry.hxx"
31 #if defined _MSC_VER
32 #pragma warning(push, 1)
33 #endif
34 #include <objbase.h>
35 #if defined _MSC_VER
36 #pragma warning(pop)
37 #endif
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)
48 HKEY hSubKey;
50 // open or create the desired key
51 char dummy[] = "";
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)
57 rc = RegSetValueExA(
58 hSubKey, ValueName, 0, REG_SZ, reinterpret_cast<const BYTE*>(Value),
59 static_cast<DWORD>(strlen(Value) + 1));
61 RegCloseKey(hSubKey);
64 return (ERROR_SUCCESS == rc);
69 bool DeleteRegistryKey(HKEY RootKey, const char* KeyName)
71 HKEY hKey;
73 int rc = RegOpenKeyExA(
74 RootKey,
75 KeyName,
77 KEY_READ | DELETE,
78 &hKey);
80 if ( rc == ERROR_FILE_NOT_FOUND )
81 return true;
83 if (ERROR_SUCCESS == rc)
85 char* SubKey;
86 DWORD nMaxSubKeyLen;
88 rc = RegQueryInfoKeyA(
89 hKey, 0, 0, 0, 0,
90 &nMaxSubKeyLen,
91 0, 0, 0, 0, 0, 0);
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;
102 rc = RegEnumKeyExA(
103 hKey,
104 0, // always index zero
105 SubKey,
106 &nLen,
107 0, 0, 0, 0);
109 if (ERROR_NO_MORE_ITEMS == rc)
111 rc = RegDeleteKeyA(RootKey, KeyName);
112 break;
114 else if (rc == ERROR_SUCCESS)
116 DeleteRegistryKey(hKey, SubKey);
119 } // while
121 RegCloseKey(hKey);
123 } // if
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)
133 HKEY hKey;
135 LONG rc = RegOpenKeyExA(RootKey, KeyName, 0, KEY_READ, &hKey);
137 if (ERROR_SUCCESS == rc)
139 DWORD nSubKeys = 0;
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)
152 // Get CLSID
153 LPOLESTR wszCLSID = NULL;
154 StringFromCLSID(clsid, &wszCLSID);
156 char buff[39];
157 // Covert from wide characters to non-wide.
158 wcstombs(buff, wszCLSID, sizeof(buff));
160 // Free memory.
161 CoTaskMemFree(wszCLSID) ;
163 return std::string(buff);
168 bool QueryRegistryKey(HKEY RootKey, const char* KeyName, const char* ValueName, char *pszData, DWORD dwBufLen)
170 HKEY hKey;
172 int rc = RegOpenKeyExA(
173 RootKey,
174 KeyName,
176 KEY_READ,
177 &hKey);
179 if (ERROR_SUCCESS == rc)
181 rc = RegQueryValueExA(
182 hKey, ValueName, NULL, NULL, (LPBYTE)pszData,&dwBufLen);
184 RegCloseKey(hKey);
187 return (ERROR_SUCCESS == rc);
190 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */