Important alert: (current site time 7/15/2013 5:48:12 PM EDT)
 

VB icon

DOS registry viewer/modifier

Email
Submitted on: 7/17/2001 2:56:25 PM
By: Razvan Petrescu 
Level: Intermediate
User Rating: By 3 Users
Compatibility: C
Views: 21529
(About the author)
 
     I needed to be able to view/change registry keys from BAT files, the DOS command line, or even VB/VBScript, so I came up with this short program to do just that.
 

INCLUDE files:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
//INCLUDE files for :DOS registry viewer/modifier
//**************************************
//////////////////////////////////////
// registry.h
//////////////////////////////////////
#define WIN32_LEAN_AND_MEAN
#define MAXNOKEYS 255
#define SIZEOFRET 255
#include <stdio.h>
#include <windows.h>
#include <memory.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
#include <crtdbg.h>
void err_out( char*, PHKEY, int );
void close_keys( PHKEY, int );
//////////////////////////////////////
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: DOS registry viewer/modifier
// Description:I needed to be able to view/change registry keys from BAT files, the DOS command line, or even VB/VBScript, so I came up with this short program to do just that.
// By: Razvan Petrescu
//
// Inputs:Usage:
(1)registry /v basekey key1 key2... subkeyn
(2)registry /r basekey key1 key2... subkeyn value
//
// Returns:(1)value of subkeyn or error message
(2)value of subkeyn before it was replaced with parameter 'value' or error message
//
// Assumes:Current version only works with string (REG_SZ) keys.
//
// Side Effects:None.
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1972&lngWId=3//for details.//**************************************

/////////////////////////////////////////////////////////////////////////
// registry.c
//
//
// DOS Registry key viewer/replacer.
// by Razvan Petrescu 07/16/01
//
// Current version only works with string keys.
//
// Returns value of key (old if /r) or error message.
//
/////////////////////////////////////////////////////////////////////////
#include "registry.h"
/////////////////////////////////////////////////////////////////////////
int main( int argc, char* argv[] )
{
	static char*	help = "Use: registry /function key1 key2... (subkey/parameter) n\
					\n\twhere /function = /v view value of subkey n \
					\n\t /r replace subkey n-1's value with parameter n \
					\n\n Only string parameters are accepted at the current time. \
					\n\n Key 1 has to be one of the following: \
					\n\t R for HKEY_CLASSES_ROOT \
					\n\t C for HKEY_CURRENT_CONFIG \
					\n\t U for HKEY_CURRENT_USER \
					\n\t M for HKEY_LOCAL_MACHINE \
					\n\t S for HKEY_USERS. \
					\n";
	static HKEY		root_keys[] = { HKEY_CLASSES_ROOT,
						HKEY_CURRENT_CONFIG,
						HKEY_CURRENT_USER,
						HKEY_LOCAL_MACHINE,
						HKEY_USERS };
	static char		root_id[] = { 'R', 'C', 'U','M', 'S' };
	static int		ELEMS = sizeof( root_id )/sizeof( root_id[0] );;
	BYTE			lpsval[SIZEOFRET];
	DWORD			cbval = SIZEOFRET;
	DWORD			tbuff = REG_SZ;
	LONG			retv;
	BOOL			bReplace;
	HKEY			hkey[MAXNOKEYS];
	int				argcounter, argcursor;
	char			szErr[300];
	if( !lpsval )
	{
		printf( "Can't alloc storage.", NULL, 0 );
		exit( 1 );
	}
	if( argc < 4 )
		err_out( help, NULL, 0 );
	if( argc > 2 + MAXNOKEYS )
		err_out( "Too many parameters.", NULL, 0 );
	if( argv[1][1] == 'r' )
		bReplace = TRUE;
	else
	{
		if( argv[1][1] != 'v' )
		{
			sprintf( szErr, "Unknown function %s. Type registry for help.",
				argv[1] );
			err_out( szErr, NULL, 0 );
		}
		else
			bReplace = FALSE;
	}
	for( argcounter = 0; argcounter < ELEMS; argcounter++ )
		if( argv[2][0] == root_id[ argcounter ] )
			break;
	if( argcounter == ELEMS )
	{
		sprintf( szErr, "Wrong root key %s.", argv[2] );
		err_out( szErr, NULL, 0 );
	}
	hkey[ 0 ] = root_keys[ argcounter ];
	for( argcursor = 1;
		argcursor < argc - 3 - bReplace;
		argcursor ++ )
	{
		retv = RegOpenKeyEx(
			hkey[argcursor - 1],
			argv[argcursor + 2],
			(DWORD)0,
			KEY_ALL_ACCESS,
			&hkey[argcursor]
			);
		if( retv != ERROR_SUCCESS )
		{
			sprintf( szErr, "Failed to open key %s.",
				argv[argcursor + 2]);
			err_out( szErr, hkey, argcursor );
		}
	}
	retv = RegQueryValueEx(
		hkey[argcursor - 1],
		argv[argcursor + 2],
		NULL,
		&tbuff,
		lpsval,
		&cbval );
	if( retv != ERROR_SUCCESS )
	{
		sprintf( szErr, "Failed to read subkey %s.",
			argv[argcursor + 2]);
		err_out( szErr, hkey, argcursor );
	}
	if( bReplace ){
		retv = RegSetValueEx(
			hkey[argcursor - 1],
			argv[argcursor + 2],
			(DWORD)0,
			tbuff,
			(PBYTE)argv[argcursor + 3],
			strlen( argv[argcursor + 3]) + 1);
		if( retv != ERROR_SUCCESS )
		{
			sprintf( szErr, "Failed to set subkey %s to value %s.",
				argv[argcursor + 2],
				argv[argcursor + 3]);
			err_out( szErr, hkey, argcursor );
		}
	}
	printf( "%s", (char*)lpsval );
	close_keys( hkey, argc - 2 );
	return 0;
}
/////////////////////////////////////////////////////////////////////////
static void err_out( char* szErrMsg, PHKEY hkeyarray, int howmany )
{
	_ASSERTE( szErrMsg );
	printf( "\n%s", szErrMsg );
	close_keys( hkeyarray, howmany );
	exit( 1 );
}
/////////////////////////////////////////////////////////////////////////
static void close_keys( PHKEY hkeyarray, int howmany )
{
	_ASSERTE ( hkeyarray );
	while( howmany )
		RegCloseKey( hkeyarray[howmany - 1] ), howmany--;
	return;
}
/////////////////////////////////////////////////////////////////////////


Other 6 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

7/17/2001 3:10:46 PMRazvan

Of course, the !lpsval test right at the beginning is a leftover from when lpsval was malloc'd. Pls discard that test.
(If this comment was disrespectful, please report it.)

 
8/22/2001 9:32:18 AMMarcos Velasco

Why do you dont change the Title of the your program to
(If this comment was disrespectful, please report it.)

 
8/22/2001 9:33:38 AMSenna

Why do you dont change the title to:
Console registry viewer/modifier... because your program use many Win32 API functions... and dont run in ms-dos mode...

(If this comment was disrespectful, please report it.)

 
9/11/2002 12:10:47 AMopello

just to reply to that -- you can't edit the registry in MSDOS, because to do so would involve manually parsing user.dat and system.dat ... good luck there buddy -- nice project dood, i'm just getting into c++ and i understood most of it :)
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.