1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: registry.cxx,v $
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"
35 #pragma warning(push, 1)
42 #include "internal/dbgmacros.hxx"
43 #include "internal/registry.hxx"
46 #pragma warning(push, 1)
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
)
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
)
75 hSubKey
, ValueName
, 0, REG_SZ
, reinterpret_cast<const BYTE
*>(Value
), strlen(Value
) + 1);
80 return (ERROR_SUCCESS
== rc
);
83 //---------------------------------------
85 //---------------------------------------
87 bool DeleteRegistryKey(HKEY RootKey
, const char* KeyName
)
91 int rc
= RegOpenKeyExA(
98 if ( rc
== ERROR_FILE_NOT_FOUND
)
101 if (ERROR_SUCCESS
== rc
)
106 rc
= RegQueryInfoKeyA(
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
;
122 0, // always index zero
127 if (ERROR_NO_MORE_ITEMS
== rc
)
129 rc
= RegDeleteKeyA(RootKey
, KeyName
);
132 else if (rc
== ERROR_SUCCESS
)
134 DeleteRegistryKey(hKey
, SubKey
);
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
)
153 LONG rc
= RegOpenKeyExA(RootKey
, KeyName
, 0, KEY_READ
, &hKey
);
155 if (ERROR_SUCCESS
== rc
)
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
)
171 LPOLESTR wszCLSID
= NULL
;
172 StringFromCLSID(clsid
, &wszCLSID
);
175 // Covert from wide characters to non-wide.
176 wcstombs(buff
, wszCLSID
, sizeof(buff
));
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
)
192 int rc
= RegOpenKeyExA(
199 if (ERROR_SUCCESS
== rc
)
201 rc
= RegQueryValueExA(
202 hKey
, ValueName
, NULL
, NULL
, (LPBYTE
)pszData
,&dwBufLen
);
207 return (ERROR_SUCCESS
== rc
);