registry.cpp

00001 
00002 #include "registry.h"
00003 #include <windows.h>
00004 
00005 using namespace irr;
00006 using namespace irr::core;
00007 
00008 // we want the logger, declared somewhere else
00009 // again, this is because we cannot inlucde "main.h" here
00010 extern ILogger* logger;
00011 
00012 
00013 // constructor
00014 Registry::Registry()
00015 {
00016         logger->log( "Registry ctor()" );
00017 }
00018 
00019 // destructor
00020 Registry::~Registry()
00021 {
00022         logger->log( "Registry dtor()" );
00023 }
00024 
00025 
00026 // get value (type void*)
00027 ERR_TYPE Registry::queryValue( stringc key, stringc name, void* outBuffer, u32 bufferSize )
00028 {
00029         LONG result = 0;
00030         HKEY hKey;
00031 
00032         result = RegOpenKeyEx(  HKEY_LOCAL_MACHINE,
00033                                                         TEXT(key.c_str()),
00034                                                         0,
00035                                                         KEY_READ,
00036                                                         &hKey );
00037 
00038         if ( ERROR_SUCCESS != result )
00039         {
00040                 stringc err("queryKey() cannot open handle to key: ");
00041                 err += key;
00042                 err += " in HKEY_LOCAL_MACHINE. Error (see Winerror.h) is: ";
00043                 err += result;
00044                 logger->log( err.c_str() );
00045                 return ERR_REGISTRY_OPEN_KEY_FAILED;
00046         }
00047 
00048         result = RegQueryValueEx(       hKey,
00049                                                                 TEXT(name.c_str()),
00050                                                                 NULL,
00051                                                                 NULL,
00052                                                                 (LPBYTE) outBuffer,
00053                                                                 (LPDWORD) &bufferSize );
00054         if ( ERROR_FILE_NOT_FOUND == result )
00055         {
00056                 stringc err("queryKey() name not found: ");
00057                 err += name;
00058                 err += " in HKEY_LOCAL_MACHINE\\";
00059                 err += key;
00060                 logger->log( err.c_str() );
00061                 RegCloseKey( hKey );
00062                 return ERR_REGISTRY_NAME_NOT_FOUND;
00063         }
00064         else if ( ERROR_SUCCESS != result )
00065         {
00066                 stringc err("queryKey() cannot query name: ");
00067                 err += name;
00068                 err += " in HKEY_LOCAL_MACHINE\\";
00069                 err += key;
00070                 err += ". Error (see Winerror.h) is: ";
00071                 err += result;
00072                 logger->log( err.c_str() );
00073                 RegCloseKey( hKey );
00074                 return ERR_REGISTRY_NAME_NOT_FOUND;
00075         }
00076 
00077         RegCloseKey( hKey );
00078         return ERR_OK;
00079 }
00080 
00081 
00082 // get value (type REG_SZ)
00083 ERR_TYPE Registry::queryValue( stringc key, stringc name, stringc* outValue )
00084 {
00085         ERR_TYPE result = ERR_OK;
00086         stringc valueString("");
00087         c8 data[512];
00088         u32 dataSize = sizeof( data );
00089 
00090         result = this->queryValue( key, name, &data, dataSize );
00091         if ( result == ERR_OK )
00092         {
00093                 outValue->append( data );
00094         }
00095 
00096         return result;
00097 }
00098 
00099 
00100 // get value (type REG_DWORD)
00101 ERR_TYPE Registry::queryValue( stringc key, stringc name, u32* outValue )
00102 {
00103         ERR_TYPE result = ERR_OK;
00104         u32 data = 0;
00105         u32 dataSize = sizeof( data );
00106 
00107         result = this->queryValue( key, name, &data, dataSize );
00108         if ( result == ERR_OK )
00109         {
00110                 *outValue = data;
00111         }
00112 
00113         return result;
00114 }
00115 
00116 
00117 ERR_TYPE Registry::addValue( stringc key, stringc name, stringc value )
00118 {
00119         LONG result = 0;
00120         HKEY hKey;
00121         DWORD dwDisp;
00122 
00123         result = RegCreateKeyEx(        HKEY_LOCAL_MACHINE,
00124                                                                 (LPCTSTR) key.c_str(),
00125                                                                 0,
00126                                                                 NULL,
00127                                                                 REG_OPTION_VOLATILE,    // only store in memory, do not write to disk
00128                                                                 KEY_WRITE,
00129                                                                 NULL,
00130                                                                 &hKey,
00131                                                                 &dwDisp );
00132 
00133         if ( ERROR_SUCCESS != result )
00134         {
00135                 stringc err("addValue() cannot create key: ");
00136                 err += key;
00137                 err += " in HKEY_LOCAL_MACHINE. Error (see Winerror.h) is: ";
00138                 err += result;
00139                 logger->log( err.c_str() );
00140                 return ERR_REGISTRY_CREATE_KEY_FAILED;
00141         }
00142 
00143         result = RegSetValueEx( hKey,
00144                                                         (LPCTSTR) name.c_str(),
00145                                                         0,
00146                                                         REG_SZ,
00147                                                         (LPBYTE) value.c_str(),
00148                                                         value.size() + 1 );                     // must include terminating null character
00149         if ( ERROR_SUCCESS != result )
00150         {
00151                 stringc err("addValue() cannot create value: ");
00152                 err += value;
00153                 err += " with name ";
00154                 err += name;
00155                 err += " at key ";
00156                 err += key;
00157                 err += " in HKEY_LOCAL_MACHINE. Error (see Winerror.h) is: ";
00158                 err += result;
00159                 logger->log( err.c_str() );
00160                 RegCloseKey( hKey );
00161                 return ERR_REGISTRY_CREATE_KEY_FAILED;
00162         }
00163 
00164         RegCloseKey( hKey );
00165         return ERR_OK;
00166 }
00167 
00168 
00169 ERR_TYPE Registry::addValue( stringc key, stringc name, u32 value )
00170 {
00171         LONG result = 0;
00172         HKEY hKey;
00173         DWORD dwDisp;
00174 
00175         result = RegCreateKeyEx(        HKEY_LOCAL_MACHINE,
00176                                                                 (LPCTSTR) key.c_str(),
00177                                                                 0,
00178                                                                 NULL,
00179                                                                 REG_OPTION_VOLATILE,    // only store in memory, do not write to disk
00180                                                                 KEY_WRITE,
00181                                                                 NULL,
00182                                                                 &hKey,
00183                                                                 &dwDisp );
00184 
00185         if ( ERROR_SUCCESS != result )
00186         {
00187                 stringc err("addValue() cannot create key: ");
00188                 err += key;
00189                 err += " in HKEY_LOCAL_MACHINE. Error (see Winerror.h) is: ";
00190                 err += result;
00191                 logger->log( err.c_str() );
00192                 return ERR_REGISTRY_CREATE_KEY_FAILED;
00193         }
00194 
00195         result = RegSetValueEx( hKey,
00196                                                         (LPCTSTR) name.c_str(),
00197                                                         0,
00198                                                         REG_DWORD,
00199                                                         (LPBYTE) &value,
00200                                                         sizeof( u32 ) );
00201         if ( ERROR_SUCCESS != result )
00202         {
00203                 stringc err("addValue() cannot create value: ");
00204                 err += value;
00205                 err += " with name ";
00206                 err += name;
00207                 err += " at key ";
00208                 err += key;
00209                 err += " in HKEY_LOCAL_MACHINE. Error (see Winerror.h) is: ";
00210                 err += result;
00211                 logger->log( err.c_str() );
00212                 RegCloseKey( hKey );
00213                 return ERR_REGISTRY_CREATE_KEY_FAILED;
00214         }
00215 
00216         RegCloseKey( hKey );
00217         return ERR_OK;
00218 }
00219 
00220 
00221 ERR_TYPE Registry::removeKey( stringc key, stringc name )
00222 {
00223         return ERR_OK;
00224 }
00225 
00226 
00227 ERR_TYPE Registry::getProcessPath( stringc* outPath )
00228 {
00229         LONG result = 0;
00230         c8 buffer[1024];
00231         u32 bufferSize = sizeof( buffer );
00232         
00233         memset( buffer, 0, bufferSize );
00234 
00235         result = GetModuleFileName( NULL, buffer, bufferSize );
00236         if ( 0 == result )
00237         {
00238                 stringc err("getProcessPath() cannot get module file name.");
00239                 logger->log( err.c_str() );
00240                 return ERR_REGISTRY_GET_EXE_PATH_FAILED;
00241         }
00242         
00243         // strip off exe filename
00244         stringc temp( buffer );
00245         s32 last = temp.findLast( (c8) 92 );    // 92 = '\'
00246         if ( -1 == last )
00247         {
00248                 stringc err("getProcessPath() cannot get module file name");
00249                 logger->log( err.c_str() );
00250                 return ERR_REGISTRY_GET_EXE_PATH_FAILED;
00251         }
00252         
00253         buffer[last] = 0;
00254 
00255         // copy to caller
00256         outPath->append( buffer );
00257 
00258         return ERR_OK;
00259 }

Generated on Sun Dec 2 03:10:23 2007 for TableTop by  doxygen 1.5.4