Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6685
  • Last Modified:

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.
0
urivera
Asked:
urivera
  • 3
  • 2
  • 2
2 Solutions
 
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
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now