Solved

Obtaining Registry Values

Posted on 1998-07-22
3
371 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Get filename and folder into excel 7 75
Excel Use VBA to get user's Mac Address for their computer 5 171
isEverywhere  challenge 19 75
java ^ examples 8 66
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: 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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

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