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

x
?
Solved

reading registry contents

Posted on 2003-03-27
3
Medium Priority
?
263 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
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

580 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