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, const char *default_value )
134 char *tempValue = NULL;
135 char *tempValue2 = NULL;
140 if( RegOpenKeyEx( m_RootKey, path, 0, KEY_READ, &keyHandle ) == ERROR_SUCCESS )
142 if( RegQueryValueEx( keyHandle, valueName, NULL, &valueType, NULL, &size1 ) == ERROR_SUCCESS )
144 if( valueType == REG_SZ )
147 tempValue = ( char * )malloc( size1+1 ); // +1 für NullByte`?
148 if( RegQueryValueEx( keyHandle, valueName, NULL, &valueType, (LPBYTE)tempValue, &size1 ) == ERROR_SUCCESS )
150 tempValue2 = tempValue;
154 RegCloseKey( keyHandle );
157 return tempValue == NULL ? strdup( default_value ) : tempValue2;
160 double QVLCRegistry::ReadRegistryDouble( const char *path, const char *valueName, double default_value )
167 if( RegOpenKeyEx( m_RootKey, path, 0, KEY_READ, &keyHandle ) == ERROR_SUCCESS )
169 if( RegQueryValueEx( keyHandle, valueName, NULL, &valueType,
170 NULL, &size1 ) == ERROR_SUCCESS )
172 if( ( valueType == REG_BINARY ) && ( size1 == sizeof( double ) ) )
174 if( RegQueryValueEx( keyHandle, valueName, NULL, &valueType,
175 (LPBYTE)&tempValue, &size1 ) == ERROR_SUCCESS )
177 default_value = tempValue;
181 RegCloseKey( keyHandle );
183 return default_value;
186 int QVLCRegistry::DeleteValue( const char *path, const char *valueName )
190 if( (result = RegOpenKeyEx(m_RootKey, path, 0, KEY_WRITE, &keyHandle)) == ERROR_SUCCESS)
192 result = RegDeleteValue(keyHandle, valueName);
193 RegCloseKey(keyHandle);
195 //ERROR_SUCCESS = ok everything else you have a problem*g*,
199 long QVLCRegistry::DeleteKey( const char *path, const char *keyName )
203 if( (result = RegOpenKeyEx(m_RootKey, path, 0, KEY_WRITE, &keyHandle)) == ERROR_SUCCESS)
205 // be warned the key "keyName" will not be deleted if there are subkeys below him, values
206 // I think are ok and will be recusively deleted, but not keys...
207 // for this case we have to do a little bit more work!
208 result = RegDeleteKey(keyHandle, keyName);
209 RegCloseKey(keyHandle);
211 //ERROR_SUCCESS = ok everything else you have a problem*g*,