RegSetValueEx

This is a discussion on RegSetValueEx within the C++ Programming forums, part of the General Programming Boards category; I am trying to modify the windows registry PATH with the following code. When I get to the RegSetValueEx, it ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Location
    Mattoon, IL
    Posts
    4

    RegSetValueEx

    I am trying to modify the windows registry PATH with the following code. When I get to the RegSetValueEx, it bombs out and can't figure out why. When I display the error code it is a '5' but when I look in Winerror.h they are displayed in hex and decimal. 1) does anybody see anything wrong with the RegSetValueEx call and 2) how can I display the return code in a readable format as in Winerror.h

    Code:
    #include "stdafx.h"
    #include <stdlib.h>
    #include <stdio.h>
    #include <windows.h>
    #include <tchar.h>
    #include <iostream>
    #include <string.h>
    
    int checkKey(HKEY tree, const char *folder, char *key) {
    	long lRet,Rval;
    	HKEY hKey;
    	char temp[10000];
    	DWORD dwBufLen;
    
    	// Open location
    	lRet = RegOpenKeyEx( tree, folder, 0, KEY_QUERY_VALUE, &hKey );
    	if (lRet != ERROR_SUCCESS)
    		return 0;
    
    	// Get key
    	dwBufLen = sizeof(temp);
    	lRet = RegQueryValueEx( hKey, key, NULL, NULL, (BYTE*)&temp, &dwBufLen );
    	if (lRet != ERROR_SUCCESS)
    		return 0;
    	// Append to the Path
        strcat(temp, ";c:instantclient");
    	dwBufLen = sizeof(temp);
    	//Update Key
    	Rval = RegSetValueEx(hKey,key,NULL,REG_EXPAND_SZ,(LPBYTE)temp,strlen(temp));
    	if (Rval != ERROR_SUCCESS)
    	{
    		printf("set error: %ld\n",Rval);
    	}
    
    	// Close key
    	lRet = RegCloseKey( hKey );
    	if (lRet != ERROR_SUCCESS)
    		return 0;
    
    	// Got this far, then key exists
    	return 1;
    }
    
    // This is the entry point for this application
    int _tmain(void)
    {
       
    	printf("Checking for key: HKEY_LOCAL_MACHINE -> SYSTEM -> CurrentControlSet -> Control -> Session Manager -> Environment -> Path\n");
    
    	if (checkKey(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment", "Path"))
    		printf("Key Exists.\n");
    	else
    		printf("Key Does Not Exist.\n");
    
    	return 0;
    }
    Thanks!

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Error 5 is an access denial, Windows is saying you do not have sufficient permissions to do what you are doing.

    It's late now, I'll look at it in the morning.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Welcome to the forums!

    Here is a list of Win32 error codes.

    You are getting the dreaded ERROR_ACCESS_DENIED because, when you open the key, you only request the KEY_QUERY_VALUE right. If you want to set a value, you need to combine this with the KEY_SET_VALUE right:
    Code:
    	lRet = RegOpenKeyEx( tree, folder, 0, KEY_QUERY_VALUE | KEY_SET_VALUE, &hKey );
    You will need to be logged on as an administrator to modify most keys or values under HKEY_LOCAL_MACHINE. If you wanted to change an environment variable for the current user only, you could look under HKEY_CURRENT_USER\Environment.

    On another matter, you should include the nul terminator when you write out a string to the registry:
    Code:
    	Rval = RegSetValueEx(hKey,key,NULL,REG_EXPAND_SZ,(LPBYTE)  temp,strlen(temp) + 1);
    Finally, you are retrieving and modifying the path string in ansi (char) rather than unicode (wchar_t). If the original path string contained unicode characters, they may be lost.
    Last edited by anonytmouse; 03-30-2005 at 02:37 AM.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Mattoon, IL
    Posts
    4
    Works like a charm! Thanks!!!!!

  5. #5
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Cheers anon, I was just going to look at this one! I'll do somebody elses now!
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. RegSetValueEx and REG_MULTI_SZ
    By Tuborgrules in forum Windows Programming
    Replies: 6
    Last Post: 09-29-2009, 02:50 AM
  2. RegSetValueEx
    By George2 in forum Windows Programming
    Replies: 3
    Last Post: 08-23-2007, 09:52 PM
  3. Registry manipulation
    By tezcatlipooca in forum Windows Programming
    Replies: 3
    Last Post: 04-29-2007, 04:45 PM
  4. RegSetValueEx doesn't do anything
    By Xzyx987X in forum Windows Programming
    Replies: 2
    Last Post: 09-15-2003, 11:28 AM
  5. RegSetValueEx() problem
    By Kupo in forum Windows Programming
    Replies: 1
    Last Post: 02-09-2002, 09:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21