1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
#include "RegistryClass.h"
#include <sstream>
#include <iomanip>
using namespace std ;
/* Checks Whethether Path Exists or not */
bool RegistryClass::CheckPathExists(std::string _path)
{
HKEY tempKey ;
int ret = (int)RegOpenKeyEx( HKEY_LOCAL_MACHINE,_path.c_str() , 0, KEY_READ ,&tempKey);
RegCloseKey( tempKey );
return( ret == ERROR_SUCCESS );
}
// Eksik
bool RegistryClass::CheckKeyExists(std::string _path, std::string _key, DWORD _type)
{
RegistryClass reg = RegistryClass( _path,_key,_type);
string temp = string();
return reg.readKey(temp);
}
std::string RegistryClass::FindPath(string _keyName ,string value , DWORD _type,string mainPath)
{
bool flag = true;
for( int i = 0 ; flag ; i++)
{
ostringstream temp(ostringstream::out);
temp << mainPath << setfill('0') << setw(4) << i ;
flag = RegistryClass::CheckPathExists(temp.str());
if( flag )
{
RegistryClass reg = RegistryClass(temp.str(),_keyName,_type);
string valStr;
if( reg.readKey(valStr) && valStr == value)
return temp.str();
}
}
return string();
}
RegistryClass::RegistryClass(std::string _path , std::string _key , DWORD _type)
{
this->path = _path ;
this->key = _key ;
this->type = _type ;
}
DWORD RegistryClass::getSize(std::string _path , std::string _key,DWORD _type)
{
HKEY tempKey ;
DWORD size = 0 ;
int ret = (int)RegOpenKeyEx( HKEY_LOCAL_MACHINE,_path.c_str() , 0, KEY_READ ,&tempKey);
if( ret != ERROR_SUCCESS)
return size ;
RegQueryValueEx( tempKey, _key.c_str() , NULL , &_type ,NULL , &size );
return size ;
}
bool RegistryClass::openKey()
{
int flag = (int)RegOpenKeyEx( HKEY_LOCAL_MACHINE,path.c_str() , 0, KEY_ALL_ACCESS,&hKey);
return ( flag == ERROR_SUCCESS );
}
bool RegistryClass::createKey(std::string value)
{
if(!this->openKey())
return false;
//If key exists delete it
if( RegistryClass::CheckKeyExists(this->path,this->key,this->type))
if(!deleteKey(this->hKey,this->key))
return false;
//Create&Set key
int flag = (int)RegSetValueEx(this->hKey,key.c_str(),0,REG_SZ,(const BYTE*)value.c_str(),sizeof(TCHAR)*(value.size()+1));
RegCloseKey(this->hKey);
return flag == ERROR_SUCCESS;
}
bool RegistryClass::deleteKey(HKEY hKey , std::string keyName)
{
int flag = (int)RegDeleteValue(hKey,keyName.c_str());
return ( flag == ERROR_SUCCESS );
}
bool RegistryClass::deleteKey()
{
if(!this->openKey())
return false;
return RegistryClass::deleteKey(this->hKey,this->key);
}
bool RegistryClass::readKey(std::string &value)
{
DWORD size = getSize( this->path,this->key , this->type);
if( size == 0 )
return false;
if(!this->openKey())
return false;
unsigned char *temp = new unsigned char[ (int)size ];
int flag = (int)RegQueryValueEx(this->hKey,this->key.c_str(),NULL,&type,temp,&size);
if( flag != ERROR_SUCCESS )
return false;
value = std::string((char *)temp);
RegCloseKey(this->hKey);
return true;
}
|