Solved

Obtaining Registry Values

Posted on 1998-07-22
3
365 Views
Last Modified: 2013-11-20
I need to be able to obtain any value from any key in the registry.  I currently use the SetRegistryKey function along with the GetProfileInt, GetProfileString, WriteProfileInt, and WriteProfileString functions, but this only looks in an area of the registry specific to that application. Specifically, I need the value of the key:

HKEY_CURRENT_USER\Software\Cimple Systems\BasePath

Where BasePath is a String (i.e. "D:\Cimple" ).  How do I do this?  Also, how would I then change it if I wanted to?
0
Comment
Question by:ardolino
3 Comments
 
LVL 32

Accepted Solution

by:
jhance earned 70 total points
ID: 1319613
//
// Read a value from the Windows Registry
//
char *ReadRegistryValue(HKEY HK, char *Node, char *Key)
{
    HKEY hKey;
      DWORD s_len;
      long regStatus;

      static unsigned char *kval = NULL;

    if(
        RegOpenKeyEx(
            HK,
            Node,
            0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS){

            // Get the length of the data value first
            if((regStatus = RegQueryValueEx(hKey, Key, NULL, NULL, NULL, &s_len)) != ERROR_SUCCESS){
                return NULL;
            }

            // Make space to hold it
            if(kval != NULL){
                  delete [] kval;
            }

            kval = new unsigned char[s_len];

            // Get the actual data from registry
            if(RegQueryValueEx(hKey, Key, NULL, NULL, kval, &s_len) != ERROR_SUCCESS){
                return NULL;
            }
        RegCloseKey(hKey);
            return (char *)kval;
    }
    else{
            return NULL;
    }
}
0
 
LVL 7

Expert Comment

by:psdavis
ID: 1319614
Sorry for butting in...

Although the above answer does work, you can change your "CSimple Systems" with CWinApp::m_pszExeName and CWinApp::m_pszProfileName.  I have included my override functions if it would help.

// In the CWinApp::InitInstance

   SetRegistryKeyName    ( _T( "The Phoenix Group" ));
   SetRegistryProfileName( );


void CWinAppEx::SetRegistryKeyName( const char* pszRegistryKey )
{
// First free the string allocated by MFC at CWinApp startup.
// The string is allocated before InitInstance is called.

   free( (void*) m_pszRegistryKey );

// Change the name of the registry key.
// The CWinApp destructor will free the memory.

   if( pszRegistryKey == NULL || *pszRegistryKey == NULL )
      m_pszRegistryKey = _tcsdup( _T( "CompanyName" ));
   else
      m_pszRegistryKey = _tcsdup( pszRegistryKey );
   }

void CWinAppEx::SetRegistryExeName( const char* pszExeName )
{
// First free the string allocated by MFC at CWinApp startup.
// The string is allocated before InitInstance is called.

   free( (void*) m_pszExeName );

// Change the name of the registry key.
// The CWinApp destructor will free the memory.

   if( pszExeName == NULL || *pszExeName == NULL )
      m_pszExeName = _tcsdup( _T( "ExeName" ));
   else
      m_pszExeName = _tcsdup( pszExeName );
   }

void CWinAppEx::SetRegistryProfileName( const char* pszProfileName )
{
// First free the string allocated by MFC at CWinApp startup.
// The string is allocated before InitInstance is called.

   free( (void*) m_pszProfileName );

// Change the name of the registry key.
// The CWinApp destructor will free the memory.

   if( pszProfileName == NULL || *pszProfileName == NULL )
      m_pszProfileName = _tcsdup( _T( "ExeName\\Profile" ));
   else
      m_pszProfileName = _tcsdup( pszProfileName );
   }

Phillip
0
 

Author Comment

by:ardolino
ID: 1319615
Thanks.  I checked out the www.codegyru.com (I think thats the name) web site and found a CRegistry class that I can use (freeware).  Thanks anyway.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now