Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

Registry.cpp

Go to the documentation of this file.
00001 //
00002 // Registry.cpp
00003 //
00004 // Copyright (c) Shareaza Development Team, 2002-2005.
00005 // This file is part of SHAREAZA (www.shareaza.com)
00006 //
00007 // Shareaza is free software; you can redistribute it
00008 // and/or modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2 of
00010 // the License, or (at your option) any later version.
00011 //
00012 // Shareaza is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with Shareaza; if not, write to the Free Software
00019 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 //
00021 
00022 
00023 #include "StdAfx.h"
00024 #include "Registry.h"
00025 
00026 
00028 // CRegistry construction
00029 
00030 CRegistry::CRegistry()
00031 {
00032 }
00033 
00034 CRegistry::~CRegistry()
00035 {
00036 }
00037 
00039 // CRegistry read a string value
00040 
00041 CString CRegistry::GetString(LPCTSTR pszSection, LPCTSTR pszName, LPCTSTR pszDefault)
00042 {
00043         CString strKey, strValue;
00044         DWORD nErrorCode;
00045         HKEY hKey;
00046 
00047         if ( pszDefault != NULL ) strValue = pszDefault;
00048         strKey.Format( _T("Software\\Shareaza\\Shareaza\\%s"), pszSection );
00049 
00050         nErrorCode = RegOpenKeyEx( HKEY_CURRENT_USER, strKey, 0, KEY_READ, &hKey );
00051 
00052         if ( nErrorCode == ERROR_SUCCESS )
00053         {
00054                 DWORD nType = 0, nSize = 0;
00055 
00056                 nErrorCode = RegQueryValueEx( hKey, pszName, 0, &nType, NULL, &nSize );
00057 
00058                 if ( nErrorCode == ERROR_SUCCESS && nType == REG_SZ && nSize >= sizeof(TCHAR) )
00059                 {
00060                         LPTSTR pszValue = strValue.GetBuffer( nSize / sizeof(TCHAR) - 1 );
00061                         nErrorCode = RegQueryValueEx( hKey, pszName, 0, &nType, (PBYTE)pszValue, &nSize );
00062                         strValue.ReleaseBuffer( nSize / sizeof(TCHAR) - 1 );
00063                 }
00064 
00065                 RegCloseKey( hKey );
00066         }
00067 
00068         if ( nErrorCode != ERROR_SUCCESS ) DisplayErrorMessageBox( pszName, nErrorCode );
00069 
00070         return strValue;
00071 }
00072 
00074 // CRegistry read an integer value
00075 
00076 int CRegistry::GetInt(LPCTSTR pszSection, LPCTSTR pszName, int nDefault)
00077 {
00078         int nValue = nDefault;
00079         DWORD nErrorCode;
00080         CString strKey;
00081         HKEY hKey;
00082 
00083         strKey.Format( _T("Software\\Shareaza\\Shareaza\\%s"), pszSection );
00084 
00085         nErrorCode = RegOpenKeyEx( HKEY_CURRENT_USER, strKey, 0, KEY_READ, &hKey );
00086 
00087         if ( nErrorCode == ERROR_SUCCESS )
00088         {
00089                 DWORD nType = 0, nSize = sizeof(nValue);
00090 
00091                 nErrorCode = RegQueryValueEx( hKey, pszName, 0, &nType, (PBYTE)&nValue, &nSize );
00092 
00093                 if ( nType != REG_DWORD || nSize != sizeof(nValue) )
00094                 {
00095                         nErrorCode = ERROR_MORE_DATA;
00096                         nValue = nDefault;
00097                 }
00098 
00099                 RegCloseKey( hKey );
00100         }
00101 
00102         if ( nErrorCode != ERROR_SUCCESS ) DisplayErrorMessageBox( pszName, nErrorCode );
00103 
00104         return nValue;
00105 }
00106 
00107 DWORD CRegistry::GetDword(LPCTSTR pszSection, LPCTSTR pszName, DWORD dwDefault)
00108 {
00109         return (int)GetInt( pszSection, pszName, (int)dwDefault );
00110 }
00111 
00113 // CRegistry read a float value
00114 
00115 double CRegistry::GetFloat(LPCTSTR pszSection, LPCTSTR pszName, double fDefault)
00116 {
00117         double fValue = fDefault;
00118         _stscanf( GetString( pszSection, pszName ), _T("%lf"), &fValue );
00119         return fValue;
00120 }
00121 
00123 // CRegistry write a string value
00124 
00125 BOOL CRegistry::SetString(LPCTSTR pszSection, LPCTSTR pszName, LPCTSTR pszValue)
00126 {
00127         DWORD nErrorCode;
00128         CString strKey;
00129         HKEY hKey;
00130 
00131         strKey.Format( _T("Software\\Shareaza\\Shareaza\\%s"), pszSection );
00132 
00133         nErrorCode = RegCreateKeyEx( HKEY_CURRENT_USER, strKey, 0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL );
00134 
00135         if ( nErrorCode == ERROR_SUCCESS )
00136         {
00137                 nErrorCode = RegSetValueEx( hKey, pszName, 0, REG_SZ, (const BYTE *)pszValue,
00138                                                                 _tcslen(pszValue) * sizeof(TCHAR) + sizeof(TCHAR) );
00139 
00140                 RegCloseKey( hKey );
00141         }
00142 
00143         if ( nErrorCode == ERROR_SUCCESS )
00144         {
00145                 return TRUE;
00146         }
00147         else
00148         {
00149                 DisplayErrorMessageBox( pszName, nErrorCode );
00150                 return FALSE;
00151         }
00152 }
00153 
00155 // CRegistry write an int value
00156 
00157 BOOL CRegistry::SetInt(LPCTSTR pszSection, LPCTSTR pszName, int nValue)
00158 {
00159         DWORD nErrorCode;
00160         CString strKey;
00161         HKEY hKey;
00162 
00163         strKey.Format( _T("Software\\Shareaza\\Shareaza\\%s"), pszSection );
00164 
00165         nErrorCode = RegCreateKeyEx( HKEY_CURRENT_USER, strKey, 0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL );
00166 
00167         if ( nErrorCode == ERROR_SUCCESS )
00168         {
00169                 nErrorCode = RegSetValueEx( hKey, pszName, 0, REG_DWORD,
00170                                                         (const BYTE *)&nValue, sizeof(nValue) );
00171 
00172                 RegCloseKey( hKey );
00173         }
00174 
00175         if ( nErrorCode == ERROR_SUCCESS )
00176         {
00177                 return TRUE;
00178         }
00179         else
00180         {
00181                 DisplayErrorMessageBox( pszName, nErrorCode );
00182                 return FALSE;
00183         }
00184 }
00185 
00187 // Helper function to display a message box holding an error code
00188 
00189 void CRegistry::DisplayErrorMessageBox(LPCTSTR pszName, DWORD nErrorCode)
00190 {
00191 #ifdef _DEBUG
00192         CString sMessage;
00193         LPVOID lpMsgBuf;
00194         FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
00195                 FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
00196                 NULL, nErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
00197                 (LPTSTR) &lpMsgBuf,     0, NULL );
00198         sMessage.Format( _T("%s returned error: %s"), pszName, (LPCTSTR)lpMsgBuf );
00199         LocalFree( lpMsgBuf );
00200         MessageBox( NULL, sMessage, _T("Warning"), MB_OK | MB_ICONINFORMATION );
00201 #endif
00202 }

Generated on Thu Dec 15 10:39:46 2005 for Shareaza 2.2.1.0 by  doxygen 1.4.2