SourceForge.jp

dkutilRegistry.h

説明を見る。
00001 #ifndef __dkutil_Registry_h__
00002 #define __dkutil_Registry_h__
00003 
00004 #include "dkutilFileSystem.h"
00005 
00006 namespace dkutil{
00007 
00008 
00009 inline bool DirectoryPlusRegistryKeyName(std::string &get,const char *key)
00010 {
00011     return DirectoryPlusFileName(get,key);
00012 }
00013 
00014 
00015 class scoped_create_registry{
00016     HKEY mKey;
00017     bool mCreated;
00018     LONG mResult;
00019     void __init_(){
00020         mResult = -1;//間違いという意味で・・・
00021         mCreated = false;
00022         memset(&mKey,0,sizeof(mKey));
00023     }
00024 public:
00025     bool reset(
00026         HKEY hKey, // handle of an open key
00027         LPCTSTR lpSubKey, // address of subkey name
00028         DWORD Reserved, // reserved
00029         LPTSTR lpClass, // address of class string
00030         DWORD dwOptions, // special options flag
00031         REGSAM samDesired, // desired security access
00032         LPSECURITY_ATTRIBUTES lpSecurityAttributes,// address of key security structure
00033         //PHKEY phkResult, // address of buffer for opened handle
00034         LPDWORD lpdwDisposition // address of disposition value buffer
00035     ){
00036 
00037         close();
00038         
00039 
00040 
00041         LONG r= RegCreateKeyEx(
00042             hKey, // handle of an open key
00043             lpSubKey, // address of subkey name
00044             Reserved, // reserved
00045             lpClass, // address of class string
00046             dwOptions, // special options flag
00047             samDesired, // desired security access
00048             lpSecurityAttributes,// address of key security structure
00049             (PHKEY)&mKey,
00050             //phkResult, // address of buffer for opened handle
00051             lpdwDisposition // address of disposition value buffer
00052         );
00053         //結果を入れる。
00054         mResult = r;
00055         if(r == ERROR_SUCCESS){
00056             return false;
00057         }
00058         mCreated = true;
00059         return true;
00060     }
00061 
00062 
00063 
00064     scoped_create_registry(
00065         HKEY hKey, // handle of an open key
00066         LPCTSTR lpSubKey, // address of subkey name
00067         DWORD Reserved, // reserved
00068         LPTSTR lpClass, // address of class string
00069         DWORD dwOptions, // special options flag
00070         REGSAM samDesired, // desired security access
00071         LPSECURITY_ATTRIBUTES lpSecurityAttributes,// address of key security structure
00072         //PHKEY phkResult, // address of buffer for opened handle
00073         LPDWORD lpdwDisposition // address of disposition value buffer
00074         ){
00075         try{
00076             __init_();
00077             reset(
00078                 hKey,
00079                 lpSubKey, // address of subkey name
00080                 Reserved, // reserved
00081                 lpClass, // address of class string
00082                 dwOptions, // special options flag
00083                 samDesired, // desired security access
00084                 lpSecurityAttributes,// address of key security structure
00085                 //phkResult, // address of buffer for opened handle
00086                 lpdwDisposition // address of disposition value buffer
00087             );
00088         }catch(...){
00089             close();
00090             throw;
00091         }
00092     }
00093     scoped_create_registry(){
00094         __init_();
00095     }
00096     virtual ~scoped_create_registry(){
00097         close();
00098     }
00099     bool empty()const{return !mCreated;}
00100     const HKEY &handle()const{return mKey;}
00101     HKEY handle(){return mKey;}
00102     const LONG state()const{return mResult;}
00103     void close(){
00104         if(mCreated){
00105             RegCloseKey(mKey);
00106             mCreated=false;
00107             mResult = -1;
00108         }
00109     }
00110 
00111 };
00112 
00113 class scoped_open_registry{
00114     HKEY mKey;
00115     bool mCreated;
00116     LONG mResult;
00117     void __init_(){
00118         mResult = 1;//間違いという意味で・・・
00119         mCreated = false;
00120         memset(&mKey,0,sizeof(mKey));
00121     }
00122 public:
00123     bool reset(
00124         HKEY hKey,// handle of an open key
00125         LPCTSTR lpSubKey, // address of name of subkey to open
00126         DWORD ulOptions, // reserved
00127         REGSAM samDesired // security access mask
00128         //PHKEY phkResult // address of handle of open key
00129 
00130     ){
00131 
00132         close();
00133 
00134         LONG r= RegOpenKeyEx(
00135             mKey,// handle of an open key
00136             lpSubKey, // address of name of subkey to open
00137             ulOptions, // reserved
00138             samDesired, // security access mask
00139             &mKey
00140             //phkResult // address of handle of open key
00141 
00142         );
00143         //結果を入れる。
00144         mResult = r;
00145         if(r == ERROR_SUCCESS){
00146             return false;
00147         }
00148         mCreated = true;
00149         return true;
00150     }
00151 
00152 
00153 
00154     scoped_open_registry(
00155             HKEY hKey,// handle of an open key
00156           LPCTSTR lpSubKey, // address of name of subkey to open
00157             DWORD ulOptions, // reserved
00158             REGSAM samDesired // security access mask
00159             //PHKEY phkResult // address of handle of open key
00160 
00161         ){
00162         try{
00163             __init_();
00164             reset(
00165                 hKey,
00166                 lpSubKey, // address of name of subkey to open
00167                 ulOptions, // reserved
00168                 samDesired // security access mask
00169                 //phkResult // address of handle of open key
00170             );
00171         }catch(...){
00172             close();
00173             throw;
00174         }
00175     }
00176     scoped_open_registry(){
00177         __init_();
00178     }
00179     virtual ~scoped_open_registry(){
00180         close();
00181     }
00182     bool empty()const{return !mCreated;}
00183     const HKEY &handle()const{return mKey;}
00184     HKEY handle(){return mKey;}
00185     const LONG state()const{return mResult;}
00186     void close(){
00187         if(mCreated){
00188             RegCloseKey(mKey);
00189             mCreated=false;
00190             mResult = -1;
00191         }
00192     }
00193 
00194 };
00195 
00196 //***********************************
00197 //忌み嫌われるレジストリ系
00198 //gotoとかを使用すればもっとキレイなプログラムにナルハズ。
00199 //いや、try catchかな?
00200 //なお、関数類のテストは行っていません。(爆!
00201 //だれかテストしてくれい^^;(2002/12/7)
00202 
00203 
00204 //レジストリ系
00205 extern bool SaveRegData(LPCTSTR lpSubKey,LPCTSTR lpValueName,
00206                  DWORD dwType,CONST BYTE *lpData,   DWORD cbData);
00207 extern bool GetRegData(LPCTSTR lpSubKey,LPTSTR lpValueName,LPDWORD lpType,
00208                 LPBYTE lpData,LPDWORD lpcbData);
00209 extern bool DeleteValueRegData(LPCTSTR lpSubKey,LPTSTR lpValueName);
00210 extern bool DeleteRegData(LPCTSTR lpSubKey);
00211 
00212 
00213 
00214 
00215 
00216 class RegistryManager{
00217     std::string mPath;
00218     HKEY mRootKey;
00219     HKEY mKey;
00220     bool mInited;
00221 public:
00223     struct ResultRegistryData{
00225         bool result;
00226         DWORD ResultType;
00228         DWORD DataType;
00230         union{
00231             DWORD uniLoadedSize;
00232             DWORD uniNeedSize;
00233         };
00234     };
00235     RegistryManager(const char *path,HKEY key=HKEY_CURRENT_USER){
00236         DKUTIL_STRUCTURE_INIT(mRootKey);
00237         DKUTIL_STRUCTURE_INIT(mKey);
00238         mInited = false;
00239         //ZeroMemory(&mRootKey,sizeof(mRootKey));
00240         //ZeroMemory(&mKey,sizeof(mKey));
00241 
00242     }
00243     virtual ~RegistryManager(){
00244         close();
00245     }
00247     bool reset(const char *path,HKEY key){
00248         //クリティカルセクションでロック
00249         scoped_criticalsection lock();
00250         close();
00251         mPath = path;
00252         mRootKey=key;
00253         mInited = true;//フラグを立てる
00254     }
00259 
00260     bool CreateFolder(const char *Section){
00261         if(empty()) return false;
00262         if(!Section) return false;
00263         std::string strTemp = mPath;
00264         DirectoryPlusRegistryKeyName(strTemp,Section);
00265 
00266         DWORD dwDisposition;
00267         scoped_create_registry reg(
00268                         mRootKey,   strTemp.c_str(),
00269                         0, "", REG_OPTION_NON_VOLATILE,
00270                         KEY_ALL_ACCESS, NULL, &dwDisposition
00271         );
00272         if(reg.empty()) return false;
00273         return true;
00274     }
00279 
00280     bool DeleteFolder(const char *Section){
00281         if(empty()) return false;
00282         if(!Section) return false; 
00283         scoped_open_registry reg(
00284             mRootKey,mPath.c_str(),
00285             0, KEY_ALL_ACCESS);
00286         //エラーチェック。
00287         if(reg.empty()) return false;
00288         
00289         if (::RegDeleteKey (reg.handle(), Section) != ERROR_SUCCESS)
00290         {
00291             return false;
00292         }
00293         return true;
00294     }
00299 
00300     bool DeleteKey (const char *Section,const char *Key)
00301     {
00302         if(empty()) return false;
00303         if(!(Section || Key)) return false;
00304         std::string strTemp = mPath;
00305         DirectoryPlusRegistryKeyName(strTemp,Section);
00306         
00307         scoped_open_registry reg(
00308             mRootKey,strTemp.c_str(),0,KEY_WRITE);
00309         if(reg.empty()) return false;
00310         
00311 
00312         if (::RegDeleteValue (reg.handle(), Key) != ERROR_SUCCESS)
00313         {
00314             return false;
00315         }
00316         return true;
00317     }
00324 
00325     ResultRegistryData GetData(const char *Section,const char *Key,void *buff,size_t buffsize)
00326     {
00327         if(empty()) goto fail;
00328         if(buff==NULL || buffsize==0 || !Section || !Key) goto fail;
00329         ResultRegistryData lResult;
00330         DKUTIL_STRUCTURE_INIT(lResult);
00331         {
00332 
00333             std::string strTemp = mPath;
00334             DirectoryPlusRegistryKeyName(strTemp,Section);
00335 
00336             scoped_open_registry reg;
00337             reg.reset(mRootKey,strTemp.c_str() , 0, KEY_READ);
00338             if(reg.empty()){
00339                 goto fail;
00340             }
00341             
00342             BYTE *ptr;
00343             ptr = (BYTE *)buff;
00344             DWORD dwSize;
00345             dwSize = buffsize;
00346             DWORD dwType;
00347             LONG lR;
00348             lR = ::RegQueryValueEx (reg.handle(), Key, NULL, &dwType, ptr, &dwSize);
00349             if (lR != ERROR_SUCCESS)
00350             {
00351                 goto fail;
00352             }
00353 
00354 
00355             //結果を入れる
00356             lResult.result = true;
00357             lResult.ResultType = lR;
00358             lResult.DataType = dwType;
00359             lResult.uniLoadedSize = dwSize;
00360             return lResult;
00361         }
00362     fail:
00363         //結果は失敗
00364         lResult.result = false;
00365         return lResult;
00366     }
00374 
00375     bool SetData (const char *Section,const char *Key, 
00376         const void *data,size_t datasize,DWORD dwType)
00377     {   
00378         if(empty()) return false;
00379         if(data==NULL || datasize==0 || !Section || !Key) return false;
00380         std::string strTemp = mPath;
00381         DirectoryPlusRegistryKeyName(strTemp,Section);
00382 
00383         HKEY refHandle;
00384         scoped_open_registry oreg(mRootKey,strTemp.c_str(),0,KEY_WRITE);
00385         scoped_create_registry creg;
00386 
00387         if(oreg.empty()){
00388             DWORD dwDisposition;
00389             creg.reset(mRootKey,strTemp.c_str(),
00390                 0,"" , REG_OPTION_NON_VOLATILE,
00391                     KEY_ALL_ACCESS, NULL,  &dwDisposition) ;
00392             if (creg.empty())
00393             {
00394                  return false;
00395             }
00396             refHandle = creg.handle();
00397         }else{
00398             refHandle = oreg.handle();
00399         }
00400 
00401         if (::RegSetValueEx (refHandle, Key, NULL, dwType,
00402                 (const BYTE*)data, datasize)
00403                 !=  ERROR_SUCCESS)
00404         {
00405             return false;
00406         }
00407         return true;
00408     }
00410     void close(){
00411         //クリティカルセクションでロック
00412         scoped_criticalsection lock;
00413 
00414         DKUTIL_STRUCTURE_INIT(mRootKey);
00415         DKUTIL_STRUCTURE_INIT(mKey);
00416         mPath.clear();
00417         mInited = false;
00418     }
00420     bool empty()const{return !(mInited);}
00421 
00422 
00423 };
00424 
00425 
00426 
00427 
00428 
00429 
00430 }//end of dkutil namespace
00431 
00432 
00433 
00434 
00435 
00436 #endif

dkutil 1.02リリース前 d金魚専用マニュアルバージョンに対してSun Dec 28 21:23:08 2003に生成されました。 doxygen 1.3.5