Solved

Obtaining Registry Values

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

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.

Question has a verified solution.

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

Suggested Solutions

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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…
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

792 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