1 /*****************************************************************************
2 * registry.cpp: Windows Registry Manipulation
3 ****************************************************************************
4 * Copyright (C) 2008 the VideoLAN team
7 * Authors: Andre Weber <WeberAndre # gmx - de>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
30 #include "registry.hpp"
32 QVLCRegistry::QVLCRegistry( HKEY rootKey )
37 QVLCRegistry::~QVLCRegistry( void )
41 bool QVLCRegistry::RegistryKeyExists( const char *path )
44 if( RegOpenKeyEx( m_RootKey, path, 0, KEY_READ, &keyHandle ) == ERROR_SUCCESS )
46 RegCloseKey( keyHandle );
52 bool QVLCRegistry::RegistryValueExists( const char *path, const char *valueName )
59 if( RegOpenKeyEx( m_RootKey, path, 0, KEY_READ, &keyHandle ) == ERROR_SUCCESS )
61 if( RegQueryValueEx( keyHandle, valueName, NULL,
62 &valueType, NULL, &size1 ) == ERROR_SUCCESS )
66 RegCloseKey( keyHandle );
71 void QVLCRegistry::WriteRegistryInt( const char *path, const char *valueName, int value )
75 if( RegCreateKeyEx( m_RootKey, path, 0, NULL, REG_OPTION_NON_VOLATILE,
76 KEY_WRITE, NULL, &keyHandle, NULL ) == ERROR_SUCCESS )
78 RegSetValueEx( keyHandle, valueName, 0, REG_DWORD,
79 (LPBYTE)&value, sizeof( int ) );
80 RegCloseKey( keyHandle );
84 void QVLCRegistry::WriteRegistryString( const char *path, const char *valueName, const char *value )
88 if( RegCreateKeyEx( m_RootKey, path, 0, NULL, REG_OPTION_NON_VOLATILE,
89 KEY_WRITE, NULL, &keyHandle, NULL ) == ERROR_SUCCESS )
91 RegSetValueEx( keyHandle, valueName, 0, REG_SZ, (LPBYTE)value,
92 (DWORD)( strlen( value ) + 1 ) );
93 RegCloseKey( keyHandle );
97 void QVLCRegistry::WriteRegistryDouble( const char *path, const char *valueName, double value )
100 if( RegCreateKeyEx( m_RootKey, path, 0, NULL, REG_OPTION_NON_VOLATILE,
101 KEY_WRITE, NULL, &keyHandle, NULL ) == ERROR_SUCCESS )
103 RegSetValueEx( keyHandle, valueName, 0, REG_BINARY, (LPBYTE)&value, sizeof( double ) );
104 RegCloseKey( keyHandle );
108 int QVLCRegistry::ReadRegistryInt( const char *path, const char *valueName, int default_value ) {
114 if( RegOpenKeyEx( m_RootKey, path, 0, KEY_READ, &keyHandle ) == ERROR_SUCCESS )
116 if( RegQueryValueEx( keyHandle, valueName, NULL, &valueType, NULL, &size1 ) == ERROR_SUCCESS )
118 if( valueType == REG_DWORD )
120 if( RegQueryValueEx( keyHandle, valueName, NULL, &valueType, (LPBYTE)&tempValue, &size1 ) == ERROR_SUCCESS )
122 default_value = tempValue;
126 RegCloseKey( keyHandle );
128 return default_value;
131 char * QVLCRegistry::ReadRegistryString( const char *path, const char *valueName, char *default_value )
134 char *tempValue = NULL;
138 if( RegOpenKeyEx( m_RootKey, path, 0, KEY_READ, &keyHandle ) == ERROR_SUCCESS )
140 if( RegQueryValueEx( keyHandle, valueName, NULL, &valueType, NULL, &size1 ) == ERROR_SUCCESS )
142 if( valueType == REG_SZ )
145 tempValue = ( char * )malloc( size1+1 ); // +1 für NullByte`?
146 if( RegQueryValueEx( keyHandle, valueName, NULL, &valueType, (LPBYTE)tempValue, &size1 ) == ERROR_SUCCESS )
148 default_value = tempValue;
152 RegCloseKey( keyHandle );
154 if( tempValue == NULL )
156 // wenn tempValue nicht aus registry gelesen wurde dafür sorgen das ein neuer String mit der Kopie von DefaultValue
157 // geliefert wird - das macht das Handling des Rückgabewertes der Funktion einfacher - immer schön mit free freigeben!
158 default_value = strdup( default_value );
161 return default_value;
164 double QVLCRegistry::ReadRegistryDouble( const char *path, const char *valueName, double default_value )
171 if( RegOpenKeyEx( m_RootKey, path, 0, KEY_READ, &keyHandle ) == ERROR_SUCCESS )
173 if( RegQueryValueEx( keyHandle, valueName, NULL, &valueType,
174 NULL, &size1 ) == ERROR_SUCCESS )
176 if( ( valueType == REG_BINARY ) && ( size1 == sizeof( double ) ) )
178 if( RegQueryValueEx( keyHandle, valueName, NULL, &valueType,
179 (LPBYTE)&tempValue, &size1 ) == ERROR_SUCCESS )
181 default_value = tempValue;
185 RegCloseKey( keyHandle );
187 return default_value;
190 int QVLCRegistry::DeleteValue( const char *path, const char *valueName )
194 if( (result = RegOpenKeyEx(m_RootKey, path, 0, KEY_WRITE, &keyHandle)) == ERROR_SUCCESS)
196 result = RegDeleteValue(keyHandle, valueName);
197 RegCloseKey(keyHandle);
199 //ERROR_SUCCESS = ok everything else you have a problem*g*,
203 long QVLCRegistry::DeleteKey( const char *path, const char *keyName )
207 if( (result = RegOpenKeyEx(m_RootKey, path, 0, KEY_WRITE, &keyHandle)) == ERROR_SUCCESS)
209 // be warned the key "keyName" will not be deleted if there are subkeys below him, values
210 // I think are ok and will be recusively deleted, but not keys...
211 // for this case we have to do a little bit more work!
212 result = RegDeleteKey(keyHandle, keyName);
213 RegCloseKey(keyHandle);
215 //ERROR_SUCCESS = ok everything else you have a problem*g*,