Using CRegKey

Hello,

I was wondering if someone can provide me with an example on how to write/read from the registry using the CRegKey class.  I have never worked with the registry so please provide as much detail as possible. I basically need is to save a bunch of values from different dialog boxes, in other words to save a configuration.  I want to have an entry in registry with my company name, and under there and entry for the software program and then the configurations.  So I was to view it using regedit it would look something like this:

myComanyName
      softwareName
            Configurations
                 ConfigurationName1   (name of configuration as saved by the user)
                      checkbox1 = TRUE or FALSE (some data stored so when I read it I can tell if it was selected or not)
                      checkbox2 = TRUE or FALSE (TRUE means it was checked)
                      checkbox3 = TRUE or FALSE
                      .......   (I will be storing about 40+ different data for each configuration)
                 ConfigurationName2
                      checkbox1 = TRUE or FALSE
                      checkbox2= TRUE or FALSE
                      checkbox3 = TRUE or FALSE

                             
I want to call the functions (method) to save to the registry when the user clicks on "Save" button from within my own child dialog box I created, is that best place to write to the registry?

User will be able to click on a "Load Configuration" button from the main window or from the File menu, which will cause a child dialog box to appear(I already build the dialog box), I want to display all the current configurations saved in the registry, so I need to iterate through each one, get the configuration name and add it to my list.  After that the user will select a configuration, I then need to query each checkbox value to set my checkbox controls accordingly.  Is there an easy way to do this using CRegKey? and if so, example(s)?

Thanks.
uriveraAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

srini_vcCommented:
This is the sample query to check the value present. and write your own value

acess value using QueryValue
Set ur values using SetValue. Setiing Key value using SetKey value

u need to open corresponding, before doing any and need to close

int MakeSQLServerODBCDSN(LPCTSTR strDBServer,LPCTSTR strDBName,LPCTSTR strDSN,LPCTSTR strUID)
{
 BOOL  bInstallDriver=TRUE;
 CRegKey  regKey;

 LONG lRet=regKey.Open(HKEY_LOCAL_MACHINE,"Software\\ODBC\\ODBCINST.INI\\SQL Server");
 if(lRet!=ERROR_SUCCESS)
 {
  bInstallDriver=FALSE;
 }
 else
 {
  char  szDirverPath[MAX_PATH]="";
  DWORD  dwCount=100;
  lRet=regKey.QueryValue(szDirverPath,"Driver",&dwCount);
  if((lRet!=ERROR_SUCCESS)||(dwCount<1))
  {
   DWORD dwErr=GetLastError();
   bInstallDriver=FALSE;
  }
  regKey.Close();
 }
 if(!bInstallDriver)
 {
  return -1;
 }
 CString  strKeyValueName="Software\\ODBC\\ODBC.INI\\";
 strKeyValueName+=strDSN;
 lRet=regKey.Create(HKEY_LOCAL_MACHINE,strKeyValueName);
 if(lRet!=ERROR_SUCCESS)
 {
  return -2;
 }
 regKey.SetValue(strDBName,"Database");
 regKey.SetValue("SQLSrv32.dll","Driver");
 regKey.SetValue(strDBServer,"Server");
 regKey.SetValue(strUID,"LastUser");
 regKey.m_hKey=HKEY_LOCAL_MACHINE;
 regKey.SetKeyValue("Software\\ODBC\\ODBC.INI\\ODBC Data Sources","SQL Server",strDSN);
 regKey.Close();
 return 0;
}


these r some of the api functions available, u can also access using these APIS

RegCloseKey()
RegConnectRegistry()
RegCreateKey()
RegCreateKeyEx()
RegDeleteKey()
RegDeleteValue()
RegEnumKey()
RegEnumKeyEx()
RegEnumValue()
RegFlushKey()
RegGetKeySecurity()
RegLoadKey()
RegNotifyChangeKeyValue()
RegOpenKey()
RegOpenKeyEx()
RegQueryInfoKey()
RegQueryMultipleValues()
RegQueryValue()
RegQueryValueEx()
RegReplaceKey()
RegRestoreKey()
RegSaveKey()
RegSetKeySecurity()
RegSetValue()
RegSetValueEx()
RegUnLoadKey()
 
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
uriveraAuthor Commented:
I also forgot to mention that I will need to delete a configuration from the registry.  Any other examples?
0
srini_vcCommented:
Same thing man.
u have to just call the deleteKey() function of CRegKey.

get the help from msdn. The basic things r discussed there. if u got any doubt in using them ask here.

but u have to be aware of things, before deleting. adding ref key may not cause error. If we deleted some system variable, then it cause even windows can't start.

be safe before use it.

try to delete ur own created keys only.

u can also call RegDeleteKey to delete the entire key.
and u can call RegDeleteValue to delete the value stored in particular key.

if u r still finding hard to implemet , we will help u soon.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

cool_alokCommented:
Hello Buddy,

 you are storing very large amount of data , the retriving and storing take much amoun t of time. i have a smaller solution for storing the options.hope you like it.

#define GLOBALREGKEY HKEY_LOCAL_MACHINE
#define GLOBALREGLOCATION "myComanyName\\softwareName\\Configurations"

class MyOption
{
      
public:
      BOOL      m_bRunAtStartup;
      BOOL      m_bShowPopup;
      UINT      m_nMinute;

      MyOption():m_bRunAtStartup(TRUE),m_bShowPopup(FALSE),m_nMinute(30)
      {}

      BOOL FnSaveData()
      {
      CRegKey l_regKey;

       if(l_regKey.Create(GLOBALREGKEY,GLOBALREGLOCATION)==ERROR_SUCCESS)
      {
            //save key
            ::RegSetValueEx(l_regKey.m_hKey,
                                    REG_OPTION_NAME,
                                          NULL,
                                          REG_BINARY,
                                          (LPBYTE)(this),
                                          sizeof(*this));
                  //close the key
                  l_regKey.Close();
                  return TRUE;
              }
            return FALSE;
      }

      BOOL FnRetrieveData()
      {
            CRegKey l_regKey;
            DWORD dwDataSize;
            DWORD dwtype=0;
      if(l_regKey.Create(GLOBALREGKEY,GLOBALREGLOCATION)==ERROR_SUCCESS)
            {
            
                  if(::RegQueryValueEx(l_regKey.m_hKey,
                                      REG_OPTION_NAME,
                                            NULL,&dwtype,
                                            NULL,&dwDataSize)!=ERROR_SUCCESS)
                  {
#ifdef _DEBUG
                        AfxMessageBox("Query Failure");
#endif
                        l_regKey.Close();
                        return FALSE;

                  }
                  if(dwDataSize!=sizeof(*this))
                  {
#ifdef _DEBUG
                        AfxMessageBox("Size Problem");
#endif
                        l_regKey.Close();
                        return FALSE;
                  }
                  
                  
                  if(::RegQueryValueEx(l_regKey.m_hKey,
                                    REG_OPTION_NAME,
                                          NULL,
                                          &dwtype,
                                          (LPBYTE)this,
                                          &dwDataSize)==ERROR_SUCCESS)
                  {
#ifdef _DEBUG
                        CString str;
                        str.Format("popup Limit: %d",m_nMinute);
                        AfxMessageBox(str);
#endif
                  }

                  //close the key
                  l_regKey.Close();
                  return TRUE;
            }
            return FALSE;
      }

};

this class will help you save your all option in single key,this will make you data more secure and little difficult to crack as it stored in binay format.
0
uriveraAuthor Commented:
Any other solutions?
0
cool_alokCommented:
What Else Solution your need Ms Univera, could you please state your need here!!!

Thanks
0
cool_alokCommented:
Hope So No Answer!!!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.