?
Solved

Using CRegKey

Posted on 2005-02-23
7
Medium Priority
?
6,543 Views
Last Modified: 2013-11-20
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.
0
Comment
Question by:urivera
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 2

Accepted Solution

by:
srini_vc earned 600 total points
ID: 13390929
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
 

Author Comment

by:urivera
ID: 13397088
I also forgot to mention that I will need to delete a configuration from the registry.  Any other examples?
0
 
LVL 2

Expert Comment

by:srini_vc
ID: 13401845
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Assisted Solution

by:cool_alok
cool_alok earned 150 total points
ID: 13409342
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
 

Author Comment

by:urivera
ID: 13410917
Any other solutions?
0
 
LVL 1

Expert Comment

by:cool_alok
ID: 13524040
What Else Solution your need Ms Univera, could you please state your need here!!!

Thanks
0
 
LVL 1

Expert Comment

by:cool_alok
ID: 13572293
Hope So No Answer!!!!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question