| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Reading the registry
How would I go about reading a registry key then saving it to a .txt file? If someone could point me in the right direction or even post sample source code it would be greatly appreciated.
Thanks for any help, Brian |
|
#2
|
|||
|
|||
|
How to read from the registry using VC++
Quote:
Hi Brian, I've include the source for a console app (MyFile.cpp). You can copy & paste the contents of the source file into your own cpp file. Open the file with VC++ and build it. Click ok for all the confirmations to create a project ... You will get a link error. You must select "Use MFC in a shared Dll" from Proect->Settings. The example demonstrates reading a String Value and an Integer value from the registry. It contains two overloaded functions GetRegVal (one for getting strings & one for getting integers). MyFile.cpp ------------ #include <afxwin.h> #include <atlbase.h> #define MAX_VAL_DATA_LEN 100 CString GetRegVal( const HKEY & hRootKey, const char * szSubKeyPath, const char * szValName, char * szValData ) { CString strRetMsg; HKEY hKey; DWORD dwCount = -1; if ( RegCreateKey( hRootKey, szSubKeyPath, &hKey ) != ERROR_SUCCESS ) { strRetMsg.Format( "Error Opening Key (%s)", szSubKeyPath ); return strRetMsg; } CRegKey regKey; regKey.Attach( hKey ); if ( regKey.QueryValue( szValData, szValName, &dwCount ) != ERROR_SUCCESS ) { strRetMsg.Format( "Error Querrying Value (%s)", szValName ); } return strRetMsg; } CString GetRegVal( const HKEY & hRootKey, const char * szSubKeyPath, const char * szValName, DWORD & dwValData ) { CString strRetMsg; HKEY hKey; if ( RegCreateKey( hRootKey, szSubKeyPath, &hKey ) != ERROR_SUCCESS ) { strRetMsg.Format( "Error Opening Key (%s)", szSubKeyPath ); return strRetMsg; } CRegKey regKey; regKey.Attach( hKey ); if ( regKey.QueryValue( dwValData, szValName ) != ERROR_SUCCESS ) { strRetMsg.Format( "Error Querrying Value (%s)", szValName ); } return strRetMsg; } void main() { // Get a String Registry Value char szSubKeyPath[] = "SYSTEM\\Setup\\AnswerFileMap"; char szValName[] = "MS_Server"; char szValData[ MAX_VAL_DATA_LEN ]; CString strRetMsg = GetRegVal( HKEY_LOCAL_MACHINE, szSubKeyPath, szValName, szValData ); if ( strRetMsg == "" ) printf( "szValData = %s\n", szValData ); else printf( "%s\n", strRetMsg ); // Get an Integer Registry Value strcpy( szSubKeyPath, "SYSTEM\\Select" ); strcpy( szValName, "Current" ); DWORD dwValData; strRetMsg = GetRegVal( HKEY_LOCAL_MACHINE, szSubKeyPath, szValName, dwValData ); if ( strRetMsg == "" ) printf( "szValData = %d\n", dwValData ); else printf( "%s\n", strRetMsg ); } I hope this works for you. Kovi |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Reading the registry |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|