1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 // MARKER(update_precomp.py): autogen include statement, do not remove 29 #include "precompiled_shell.hxx" 30 31 #if defined _MSC_VER 32 #pragma warning(push, 1) 33 #endif 34 #include <windows.h> 35 #if defined _MSC_VER 36 #pragma warning(pop) 37 #endif 38 #include <malloc.h> 39 #include "internal/dbgmacros.hxx" 40 #include "internal/registry.hxx" 41 42 #if defined _MSC_VER 43 #pragma warning(push, 1) 44 #endif 45 #include <objbase.h> 46 #if defined _MSC_VER 47 #pragma warning(pop) 48 #endif 49 50 //--------------------------------------- 51 // 52 //--------------------------------------- 53 54 // Size of a CLSID as a string 55 const int CLSID_STRING_SIZE = 39; 56 57 //--------------------------------------- 58 // 59 //--------------------------------------- 60 61 bool SetRegistryKey(HKEY RootKey, const char* KeyName, const char* ValueName, const char* Value) 62 { 63 HKEY hSubKey; 64 65 // open or create the desired key 66 int rc = RegCreateKeyExA( 67 RootKey, KeyName, 0, "", REG_OPTION_NON_VOLATILE, KEY_WRITE, 0, &hSubKey, 0); 68 69 if (ERROR_SUCCESS == rc) 70 { 71 rc = RegSetValueExA( 72 hSubKey, ValueName, 0, REG_SZ, reinterpret_cast<const BYTE*>(Value), strlen(Value) + 1); 73 74 RegCloseKey(hSubKey); 75 } 76 77 return (ERROR_SUCCESS == rc); 78 } 79 80 //--------------------------------------- 81 // 82 //--------------------------------------- 83 84 bool DeleteRegistryKey(HKEY RootKey, const char* KeyName) 85 { 86 HKEY hKey; 87 88 int rc = RegOpenKeyExA( 89 RootKey, 90 KeyName, 91 0, 92 KEY_READ | DELETE, 93 &hKey); 94 95 if ( rc == ERROR_FILE_NOT_FOUND ) 96 return true; 97 98 if (ERROR_SUCCESS == rc) 99 { 100 char* SubKey; 101 DWORD nMaxSubKeyLen; 102 103 rc = RegQueryInfoKeyA( 104 hKey, 0, 0, 0, 0, 105 &nMaxSubKeyLen, 106 0, 0, 0, 0, 0, 0); 107 108 nMaxSubKeyLen++; // space for trailing '\0' 109 110 SubKey = reinterpret_cast<char*>( 111 _alloca(nMaxSubKeyLen*sizeof(char))); 112 113 while (ERROR_SUCCESS == rc) 114 { 115 DWORD nLen = nMaxSubKeyLen; 116 117 rc = RegEnumKeyExA( 118 hKey, 119 0, // always index zero 120 SubKey, 121 &nLen, 122 0, 0, 0, 0); 123 124 if (ERROR_NO_MORE_ITEMS == rc) 125 { 126 rc = RegDeleteKeyA(RootKey, KeyName); 127 break; 128 } 129 else if (rc == ERROR_SUCCESS) 130 { 131 DeleteRegistryKey(hKey, SubKey); 132 } 133 134 } // while 135 136 RegCloseKey(hKey); 137 138 } // if 139 140 return (ERROR_SUCCESS == rc); 141 } 142 143 /** May be used to determine if the specified registry key has subkeys 144 The function returns true on success else if an error occures false 145 */ 146 bool HasSubkeysRegistryKey(HKEY RootKey, const char* KeyName, /* out */ bool& bResult) 147 { 148 HKEY hKey; 149 150 LONG rc = RegOpenKeyExA(RootKey, KeyName, 0, KEY_READ, &hKey); 151 152 if (ERROR_SUCCESS == rc) 153 { 154 DWORD nSubKeys = 0; 155 156 rc = RegQueryInfoKeyA(hKey, 0, 0, 0, &nSubKeys, 0, 0, 0, 0, 0, 0, 0); 157 158 bResult = (nSubKeys > 0); 159 } 160 161 return (ERROR_SUCCESS == rc); 162 } 163 164 // Convert a CLSID to a char string. 165 std::string ClsidToString(const CLSID& clsid) 166 { 167 // Get CLSID 168 LPOLESTR wszCLSID = NULL; 169 StringFromCLSID(clsid, &wszCLSID); 170 171 char buff[39]; 172 // Covert from wide characters to non-wide. 173 wcstombs(buff, wszCLSID, sizeof(buff)); 174 175 // Free memory. 176 CoTaskMemFree(wszCLSID) ; 177 178 return std::string(buff); 179 } 180 181 //--------------------------------------- 182 // 183 //--------------------------------------- 184 185 bool QueryRegistryKey(HKEY RootKey, const char* KeyName, const char* ValueName, char *pszData, DWORD dwBufLen) 186 { 187 HKEY hKey; 188 189 int rc = RegOpenKeyExA( 190 RootKey, 191 KeyName, 192 0, 193 KEY_READ, 194 &hKey); 195 196 if (ERROR_SUCCESS == rc) 197 { 198 rc = RegQueryValueExA( 199 hKey, ValueName, NULL, NULL, (LPBYTE)pszData,&dwBufLen); 200 201 RegCloseKey(hKey); 202 } 203 204 return (ERROR_SUCCESS == rc); 205 } 206