• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 45,  comments - 232,  trackbacks - 0

            .NET 里面操作注冊表使用RegistryKey,我用C++也實現(xiàn)一個RegistryKey,發(fā)現(xiàn)確實很好用,不敢獨享,讓大家也看看也體驗一下。

             

            /********************************************************************

                (c) 2003-2005 C2217 Studio

                Module:    Registry.h

                Author:     Yangjun D.

                Created:   9/5/2005   13:24

                Purpose:   Wrapper registry operation like RegistryKey class in .NET

                History:

            *********************************************************************/

             

            #pragma once

             

            #include <string>

            #include <vector>

            using namespace std;

             

            namespace C2217

            {

            namespace Win32

            {

                   class RegistryKey

                   {

                   public:

                          RegistryKey(HKEY key=NULL, string name= "");

                          virtual ~RegistryKey(void);

             

                          void Close();

             

                          RegistryKey OpenSubKey(const string &keyName, bool writable= false) const ;

                          RegistryKey CreateSubKey(const string &keyName) const ;

                          void DelSubKey(const string &keyName) const ;

             

                          void SetValue(const string &valueName, const string &newStrValue) const;

                          void SetValue(const string &valueName, int newIntValue) const ;

                          void DelValue(const string &valueName) const ;

             

                          string GetStrValue(const string &keyName) const ;

                          int GetIntValue(const string &keyName) const ;

                          const string &get_name() const  { return m_name; }

             

                          void Backup(const string &file) const ;

                          void Restore(const string &file) const;

             

                          void GetSubKeyNames(vector<string> & keyNames) const ;

                          void GetValueNames(vector<string> &valueNames) const ;

                          void Flush() const;

                   private:

                          HKEY m_key;

                          string m_name;

                   };

             

            class Registry

            {

            public:

                   const static RegistryKey LocalMachine;

                   const static RegistryKey ClassesRoot;

                   const static RegistryKey CurrentConfig;

                   const static RegistryKey CurrentUser;

                   const static RegistryKey DynData;

                   const static RegistryKey PerformanceData;

                   const static RegistryKey Users;

             

            };   

             

            }

            }

             

            /********************************************************************

                (c) 2003-2005 C2217 Studio

                Module:    registry.cpp

                Author:     Yangjun D.

                Created:    9/5/2005   13:24

                Purpose:    Implement  the RegistryKey class

                History:

            *********************************************************************/

             

            #include "StdAfx.h"

            #include ".\registry.h"

            #include "winexception.h"

            #include "winex.h"

            using namespace C2217::StdLib;

             

            namespace C2217

            {

            namespace Win32

            {

                   //Class RegistryKey

                   RegistryKey::RegistryKey(HKEY key/*=NULL*/, string name/*= ""*/)

                   {

                          m_key = key;

                          m_name = name;

                   }

                   RegistryKey::~RegistryKey(void)

                   {

                          //Close();

                   }

             

                   RegistryKey RegistryKey::OpenSubKey(const string &keyName,bool writable/*= false*/) const

                   {

                          DWORD dwAccess = KEY_READ;

                          if(writable)

                          {

                                 dwAccess |=KEY_WRITE;

                          }

             

                          RegistryKey openKey;

                          int result =::RegOpenKeyEx(m_key,keyName.c_str(),0,dwAccess, &openKey.m_key);

                          if(result != ERROR_SUCCESS)

                          {

                                 THROW_EX_CODE(result);

                          }

                         

                          if (!m_name.empty())

                          {

                                 openKey.m_name = keyName;

                          }

                          else

                          {

                                 openKey.m_name = m_name +"\\" + keyName;

                          }

             

                          return openKey;

                   }

             

                   RegistryKey RegistryKey::CreateSubKey(const string &keyName) const

                   {

                          RegistryKey newKey;

                          int result = ::RegCreateKeyEx(

                                 m_key,

                                 keyName.c_str(),

                                 0,

                                 NULL,

                                 REG_OPTION_NON_VOLATILE,

                                 KEY_ALL_ACCESS,

                                 NULL,

                                 &newKey.m_key,

                                 NULL

                                 );

             

                          if(result != ERROR_SUCCESS ) {

                                 THROW_EX_CODE(result);

                          }

                         

                          if (!m_name.empty()) {

                                 newKey.m_name = keyName;

                          }

                          else {

                                 newKey.m_name = m_name +"\\" + keyName;

                          }

             

                          return newKey;

                   }

             

                   void RegistryKey::Close()

                   {

                          if(NULL!=m_key)

                          {

                                 ::RegCloseKey(m_key);

                          }

                   }

             

                   LPBYTE STRING_To_LPBYTE(string str)

                   {

                          LPBYTE lpb=new BYTE[str.length()+1];

                          for(size_t i=0;i<str.length();i++)

                                 lpb[i]=str[i];

                          lpb[str.length()]=0;

                          return lpb;

                   }

             

                   void RegistryKey::SetValue(const string &valueName,const string &newStrValue) const

                   {

                          LPBYTE lpData = STRING_To_LPBYTE(newStrValue);

                  

                          int result = ::RegSetValueEx(m_key, valueName.c_str(),0,REG_SZ,

                                 lpData,

                                 (int)newStrValue.length()+1);

             

                          delete[] lpData;

             

                          if(result != ERROR_SUCCESS)

                          {

                                 THROW_EX_CODE(result);

                          }

                         

                   }

                   void RegistryKey::SetValue( const string &valueName,int newIntValue) const

                   {

                          int result = ::RegSetValueEx(m_key, valueName.c_str() ,

                                 0L,REG_DWORD,(const BYTE *) &newIntValue,

                                 sizeof(int));

             

                          if(result != ERROR_SUCCESS)

                          {

                                 THROW_EX_CODE(result);

                          }

                   }

             

                   string RegistryKey::GetStrValue(const string &valueName) const

                   {

                          DWORD dwType;

                          static TCHAR szValue[2048] = {0};

                          DWORD dwSize=sizeof(szValue);

             

                          int result = RegQueryValueEx(m_key, valueName.c_str(),

                                 NULL,&dwType,(BYTE *)&szValue,&dwSize);

             

                          if(result != ERROR_SUCCESS)

                          {

                                 THROW_EX_CODE(result);

                          }

             

                          return szValue;

                   }

                   int RegistryKey::GetIntValue(const string &valueName) const

                   {

                          DWORD dwType;

                          DWORD dwSize=sizeof(DWORD);

                          DWORD dwDest;

             

                          int result = RegQueryValueEx(m_key, valueName.c_str(),

                                 NULL,&dwType,(BYTE *)&dwDest,&dwSize);

             

                          if(result != ERROR_SUCCESS)

                          {

                                 THROW_EX_CODE(result);

                          }

             

                          return (int)dwDest;

                   }

             

                   void RegistryKey::DelSubKey(const string &keyName) const

                   {

                          int result = ::RegDeleteKey(m_key,keyName.c_str());

             

                          if(result != ERROR_SUCCESS)

                          {

                                 THROW_EX_CODE(result);

                          }

                   }

             

                   void RegistryKey::DelValue(const string &valueName) const

                   {

                          int result = ::RegDeleteValue(m_key,valueName.c_str());

             

                          if(result != ERROR_SUCCESS)

                          {

                                 THROW_EX_CODE(result);

                          }

                   }

             

                   void RegistryKey::Backup(const string &file) const

                   {

                          int result = RegSaveKey(m_key, file.c_str(),NULL);

             

                          if(result != ERROR_SUCCESS)

                          {

                                 THROW_EX_CODE(result);

                          }

                   }

                   void RegistryKey::Restore(const string &file) const

                   {

                          int result = ::RegRestoreKey(m_key, file.c_str() ,REG_WHOLE_HIVE_VOLATILE);

             

                          if(result != ERROR_SUCCESS)

                          {

                                 THROW_EX_CODE(result);

                          }

                   }

             

                   const static int ITEM_MAX_COUNT = 1024;

             

                   void RegistryKey::GetSubKeyNames(vector<string> & keyNames) const

                   {

                          int result=0;

                          TCHAR keyName [256] ={0};

                          DWORD nameSize;

             

                          for(int i=0; i< ITEM_MAX_COUNT; i++)

                          {

                                 nameSize= sizeof(keyName);

                                 memset(keyName, 0 , nameSize);

             

                                 result = ::RegEnumKeyEx( m_key, i, keyName, &nameSize, NULL,NULL,NULL,NULL);

                                 if(ERROR_SUCCESS == result)

                                 {

                                        keyNames.push_back(keyName);

                                 }

                                 else if(ERROR_NO_MORE_ITEMS == result)

                                 {

                                        break;

                                 }

                                 else

                                 {

                                        THROW_EX_CODE(result);

                                 }

                          }

                   }

             

                   void RegistryKey::Flush() const

                   {

                          int result = ::RegFlushKey(m_key);

             

                          if(result != ERROR_SUCCESS)

                          {

                                 THROW_EX_CODE(result);

                          }

                   }

                  

                   const RegistryKey Registry::LocalMachine(HKEY_LOCAL_MACHINE);

                   const RegistryKey Registry::ClassesRoot(HKEY_CLASSES_ROOT);

                   const RegistryKey Registry::CurrentConfig(HKEY_CURRENT_CONFIG);

                   const RegistryKey Registry::CurrentUser(HKEY_CURRENT_USER);

                   const RegistryKey Registry::DynData(HKEY_DYN_DATA);

                   const RegistryKey Registry::PerformanceData(HKEY_PERFORMANCE_DATA);

                   const RegistryKey Registry::Users(HKEY_USERS);

             

                  

            }

            }

             

            操作方法跟.NET里面的RegistryKey 幾乎相同,就不多說了。

                  

            posted on 2005-09-21 09:20 天下無雙 閱讀(1250) 評論(0)  編輯 收藏 引用

            常用鏈接

            留言簿(15)

            隨筆分類

            隨筆檔案

            相冊

            我的其它領域Blog

            搜索

            •  

            積分與排名

            • 積分 - 205756
            • 排名 - 130

            最新評論

            閱讀排行榜

            評論排行榜

            久久久久亚洲av无码专区导航 | 久久久久亚洲av综合波多野结衣| 亚洲AV伊人久久青青草原| 亚洲综合伊人久久综合| 久久99精品久久久久久| 偷偷做久久久久网站| 香港aa三级久久三级| 久久精品国产AV一区二区三区| 青青青青久久精品国产 | 欧美午夜A∨大片久久| 亚洲国产精品无码久久SM| 精品久久久久一区二区三区| 久久久久久国产精品免费无码| 久久99精品久久久久久齐齐| 国产精品美女久久久久久2018| 国产精品久久久久久久久久影院 | 久久精品一区二区国产| 2019久久久高清456| 国产精品一区二区久久精品无码 | 久久久免费观成人影院| 久久精品国产99国产精品澳门| 熟妇人妻久久中文字幕| 精品伊人久久大线蕉色首页| 久久精品夜色噜噜亚洲A∨| 久久天堂电影网| 一本久久a久久精品综合夜夜| 久久精品人人做人人爽电影蜜月| 国产精品久久久久久五月尺| 亚洲国产成人久久精品99 | 久久国产精品成人片免费| 久久精品国产男包| 久久人人爽人人爽人人片AV麻烦| 亚洲欧美精品一区久久中文字幕| 久久青青草原亚洲av无码| 久久久99精品成人片中文字幕| 久久精品国产亚洲5555| 久久99精品国产麻豆不卡| 久久久黄片| 久久天天躁狠狠躁夜夜不卡| 国产毛片欧美毛片久久久| 狠狠综合久久综合88亚洲|