C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old August 27th, 2004, 03:00 PM
boogrog boogrog is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 1 boogrog User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #2  
Old December 24th, 2004, 04:26 AM
Kovi.Essop Kovi.Essop is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 1 Kovi.Essop User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
How to read from the registry using VC++

Quote:
Originally Posted by boogrog
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

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

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Reading the registry


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT