00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "StdAfx.h"
00024 #include "Registry.h"
00025
00026
00028
00029
00030 CRegistry::CRegistry()
00031 {
00032 }
00033
00034 CRegistry::~CRegistry()
00035 {
00036 }
00037
00039
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
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
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
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
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
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 }