update dev300-m58
[ooovba.git] / shell / source / win32 / shlxthandler / util / registry.cxx
blobf7ade4614f55ceaf06a5242c08d8e7ab51b469f0
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: registry.cxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_shell.hxx"
34 #if defined _MSC_VER
35 #pragma warning(push, 1)
36 #endif
37 #include <windows.h>
38 #if defined _MSC_VER
39 #pragma warning(pop)
40 #endif
41 #include <malloc.h>
42 #include "internal/dbgmacros.hxx"
43 #include "internal/registry.hxx"
45 #if defined _MSC_VER
46 #pragma warning(push, 1)
47 #endif
48 #include <objbase.h>
49 #if defined _MSC_VER
50 #pragma warning(pop)
51 #endif
53 //---------------------------------------
55 //---------------------------------------
57 // Size of a CLSID as a string
58 const int CLSID_STRING_SIZE = 39;
60 //---------------------------------------
62 //---------------------------------------
64 bool SetRegistryKey(HKEY RootKey, const char* KeyName, const char* ValueName, const char* Value)
66 HKEY hSubKey;
68 // open or create the desired key
69 int rc = RegCreateKeyExA(
70 RootKey, KeyName, 0, "", REG_OPTION_NON_VOLATILE, KEY_WRITE, 0, &hSubKey, 0);
72 if (ERROR_SUCCESS == rc)
74 rc = RegSetValueExA(
75 hSubKey, ValueName, 0, REG_SZ, reinterpret_cast<const BYTE*>(Value), strlen(Value) + 1);
77 RegCloseKey(hSubKey);
80 return (ERROR_SUCCESS == rc);
83 //---------------------------------------
85 //---------------------------------------
87 bool DeleteRegistryKey(HKEY RootKey, const char* KeyName)
89 HKEY hKey;
91 int rc = RegOpenKeyExA(
92 RootKey,
93 KeyName,
95 KEY_READ | DELETE,
96 &hKey);
98 if ( rc == ERROR_FILE_NOT_FOUND )
99 return true;
101 if (ERROR_SUCCESS == rc)
103 char* SubKey;
104 DWORD nMaxSubKeyLen;
106 rc = RegQueryInfoKeyA(
107 hKey, 0, 0, 0, 0,
108 &nMaxSubKeyLen,
109 0, 0, 0, 0, 0, 0);
111 nMaxSubKeyLen++; // space for trailing '\0'
113 SubKey = reinterpret_cast<char*>(
114 _alloca(nMaxSubKeyLen*sizeof(char)));
116 while (ERROR_SUCCESS == rc)
118 DWORD nLen = nMaxSubKeyLen;
120 rc = RegEnumKeyExA(
121 hKey,
122 0, // always index zero
123 SubKey,
124 &nLen,
125 0, 0, 0, 0);
127 if (ERROR_NO_MORE_ITEMS == rc)
129 rc = RegDeleteKeyA(RootKey, KeyName);
130 break;
132 else if (rc == ERROR_SUCCESS)
134 DeleteRegistryKey(hKey, SubKey);
137 } // while
139 RegCloseKey(hKey);
141 } // if
143 return (ERROR_SUCCESS == rc);
146 /** May be used to determine if the specified registry key has subkeys
147 The function returns true on success else if an error occures false
149 bool HasSubkeysRegistryKey(HKEY RootKey, const char* KeyName, /* out */ bool& bResult)
151 HKEY hKey;
153 LONG rc = RegOpenKeyExA(RootKey, KeyName, 0, KEY_READ, &hKey);
155 if (ERROR_SUCCESS == rc)
157 DWORD nSubKeys = 0;
159 rc = RegQueryInfoKeyA(hKey, 0, 0, 0, &nSubKeys, 0, 0, 0, 0, 0, 0, 0);
161 bResult = (nSubKeys > 0);
164 return (ERROR_SUCCESS == rc);
167 // Convert a CLSID to a char string.
168 std::string ClsidToString(const CLSID& clsid)
170 // Get CLSID
171 LPOLESTR wszCLSID = NULL;
172 StringFromCLSID(clsid, &wszCLSID);
174 char buff[39];
175 // Covert from wide characters to non-wide.
176 wcstombs(buff, wszCLSID, sizeof(buff));
178 // Free memory.
179 CoTaskMemFree(wszCLSID) ;
181 return std::string(buff);
184 //---------------------------------------
186 //---------------------------------------
188 bool QueryRegistryKey(HKEY RootKey, const char* KeyName, const char* ValueName, char *pszData, DWORD dwBufLen)
190 HKEY hKey;
192 int rc = RegOpenKeyExA(
193 RootKey,
194 KeyName,
196 KEY_READ,
197 &hKey);
199 if (ERROR_SUCCESS == rc)
201 rc = RegQueryValueExA(
202 hKey, ValueName, NULL, NULL, (LPBYTE)pszData,&dwBufLen);
204 RegCloseKey(hKey);
207 return (ERROR_SUCCESS == rc);