?
Solved

reading registry contents

Posted on 2003-03-27
3
Medium Priority
?
254 Views
Last Modified: 2010-04-15
can anyone tell me how to read and modify windows registry contents using 'C' language. It would be better, if explained with example

Thanks in advance
0
Comment
Question by:rsekhar03
[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 Comments
 
LVL 2

Accepted Solution

by:
corduroy9 earned 60 total points
ID: 8217994

Here is one example for reading.  Note that anyone (any application) can read the registry, but only a user with admin privs can modify/write to the registry.

This example reads information from the location:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Test\Parameters

It expects to find 3 string values there, RequestQ, ResponseQ, and IniPathName.

Note also that I use a little C++ MFC in the the code (CString objects and cout) which you could easily change to char[] variables and printf() functions.


BOOL m_fnRegErrors( int rc, CString sFunctionName );


int main ( void )
{
            int rc = 0;

      //--------------------------------------------------------------------------------
      //            Open a known key, in this case
      //--------------------------------------------------------------------------------

      //variables for RegOpenKeyEx
      HKEY hKey                        = HKEY_LOCAL_MACHINE;                  // handle to key to open
      char lpSubKey[300]            = "SYSTEM\\CurrentControlSet\\Services\\";       // subkey name, NULL will open hKey (above), else it will open subkey
      REGSAM samDesired            = KEY_READ;                        // security access mask, what action to perform
      HKEY phkResult;                                                                  // handle to open key, OUT value, use it to close the Key later


      sprintf( lpSubKey, "%s%s\\Parameters", lpSubKey, "Test" );

      cout<<"Started lpSubKey ...:"<<lpSubKey<<endl;

      rc = RegOpenKeyEx(  hKey,                  // handle to open key
                                    lpSubKey,            // subkey name
                                    0,                        // reserved
                                    samDesired,            // security access mask
                                    &phkResult);      // handle to open key

      if ( rc )
            return ( m_fnRegErrors( rc, "RegOpenKeyEx" ) );

      //variables for RegEnumValue
      DWORD dwValueNameSize      = 256;                                                      // IN/OUT size of value name
      char* szValueName            = (char*)malloc( dwValueNameSize );            // IN value name
      DWORD dwIndex                  = 0;                                                      // IN index of subkey to query
      LPDWORD dwType                  = NULL;                                                      // OUT type buffer, data type of the value
      DWORD cbDataSize            = 256;                                                      // IN/OUT size of data buffer
      unsigned char* szData      = (unsigned char*)malloc( cbDataSize );      // data buffer

      //--------------------------------------------------------------------------------
      //            Loop through the key, returning the values
      //--------------------------------------------------------------------------------
      
      //priming read
      rc = RegEnumValue( phkResult, dwIndex, szValueName, &dwValueNameSize, NULL, dwType, szData, &cbDataSize );

      while ( ( rc == 0 ) || ( rc == ERROR_MORE_DATA ) )
      {
            if ( rc ) //this happens if buffer passed in is too small, reallocate for larger buffer area
            {
                  szValueName = (char*)realloc( szValueName, ++dwValueNameSize );
                  szData = (unsigned char*)realloc( szData, ++cbDataSize );
            }
            else      //found the next "complete" value, now assign it to our variables
            {
                  //assign value to variables for later use
                  if   ( strcmp( "ResponseQ"       , szValueName) == 0 ) {
                        printf( "Found ResponseQ [%s]", szData );
                  }
                  else if ( strcmp( "RequestQ" , szValueName) == 0 ) {
                        printf( "Found RequestQ [%s]", szData );
                  }
                  else if ( strcmp( "IniPathName" , szValueName) == 0 ) {
                        printf( "Found IniPathName [%s]", szData );
                  }
                  
                  dwIndex++; //increment key index to look for next value
            }

            //follow-on reads
            rc = RegEnumValue( phkResult, dwIndex, szValueName, &dwValueNameSize, NULL, dwType, szData, &cbDataSize );

      }

      free( (void*)szValueName );
      free( (void*)szData );

      RegCloseKey( hKey );
      
      cout<<"Done    lpSubKey ...:"<<lpSubKey<<rc<<endl;

      if ( rc == ERROR_NO_MORE_ITEMS ) {
            return TRUE;
      }
      else {
            return ( m_fnRegErrors( rc, "RegEnumValue" ) );
      }
      return 0;
}





BOOL m_fnRegErrors( int rc, CString sFunctionName )
{
      //variables for error messages
      DWORD dwFlags            = FORMAT_MESSAGE_FROM_SYSTEM;      // IN source and processing options
      LPTSTR lpBuffer            = (char*)malloc( 256 );                  // OUT message buffer
      DWORD nSize                  = 255;                                        // IN maximum size of message buffer

      CString sErrorMessage;

      FormatMessage(      dwFlags,            // source and processing options
                              NULL,                  // message source
                              rc,                        // message identifier
                              0,                        // language identifier
                              lpBuffer,            // message buffer
                              nSize,                  // maximum size of message buffer
                              NULL      );            // array of message inserts

      
      sErrorMessage = "fnLocalInit " + sFunctionName + " failed.  Registry function.  Reason: " + lpBuffer;
      cout<<"Error: "<< sErrorMessage <<endl;
      
      free( (void*)lpBuffer );
      return FALSE;


}



There are examples of writing to the registry in MSDN.  Hope this example helps...




0
 
LVL 2

Expert Comment

by:bkrahmer
ID: 8223025
Corduroy9 stated: "Note that anyone (any application) can read the registry, but only a user with admin privs can modify/write to the registry."  

This is not correct.  Each key can have it's own ACL (access control list).  Each ACL can control many aspects of operation on the key or it's values.  There are 11 rights that can be controlled.

Note that this information may not apply to lesser windoze OSes.  I've only been using NT (and 2k, xp) since about '96.

brian
0
 

Author Comment

by:rsekhar03
ID: 8260178
Thanks for the help.

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

770 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