Thread: RegSetValueEx doesn't do anything

  1. #1
    Registered User Xzyx987X's Avatar
    Join Date
    Sep 2003
    Posts
    107

    RegSetValueEx doesn't do anything

    Sorry to ask another newbie question so soon, but this is completely driving me nuts. I want to set a registry value to a number. Here is the code:
    Code:
    RegSetValueEx(
    	hKey,
    	"/*Name of value*/",
    	0,
    	REG_DWORD,
    	(const BYTE *) 1,
    	sizeof(DWORD));
    This does absolutely nothing... The weird part is it doesn't even return an error so I can't tell what I did wrong, and none of the example code I've been able to turn up has told me anything useful. Another weird thing, if I make the code:
    Code:
    RegSetValueEx(
    	hKey,
    	"/*Name of value*/",
    	0,
    	REG_DWORD,
    	(const BYTE *) "1",
    	sizeof(DWORD));
    it works, but it's the ASCII value for "1" and not the number 1. I tried to do this with the char(1) too, but that produced the same result as just 1. I really can't figure out what's going on here, so I need some help. Also my code for querying a registry value doesn't seem to work either, and since the problems are probobly related I'll post that code too:
    Code:
    LPBYTE   registryDataReturn = 0;
    LPDWORD  registryDataSize = 0;
    
    RegQueryValueEx(
    	hKey,
    	"/*Any value within the current key*/",
    	0,
    	NULL,
    	registryDataReturn,
    	registryDataSize);
    
    printf("%d\n", (int) registryDataReturn);
    No matter what, this always prints 0. I'm resonably sure my hKey value is correct, otherwise I wouldn't have been able to set string data successfully, but just in case here's that code too:
    Code:
    	RegCreateKeyEx(
    		HKEY_CURRENT_USER,
    		"/*Registry key here */",
    		0,
    		(LPTSTR) NULL,
    		REG_OPTION_NON_VOLATILE,
    		KEY_READ | KEY_WRITE,
    		(LPSECURITY_ATTRIBUTES) NULL,
    		&hKey,
    		(LPDWORD) NULL);
    If you can shed any light on this whole mess, I'd really appreciate it.

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    1,619

    Re: RegSetValueEx doesn't do anything

    Originally posted by Xzyx987X
    I really can't figure out what's going on here, so I need some help.
    You need a POINTER to the data, not the data itself. When you pass in (BYTE *) 1, it tries to read the value from address 0x00000001. This is not what you want. Try:
    Code:
    DWORD data = 1;
    RegSetValueEx(
    	hKey,
    	"/*Name of value*/",
    	0,
    	REG_DWORD,
    	&data,
    	sizeof(DWORD));

    Also my code for querying a registry value doesn't seem to work either, and since the problems are probobly related I'll post that code too:

    It's very related, and again, this is about pointers. This:

    LPBYTE registryDataReturn = 0;
    LPDWORD registryDataSize = 0;

    creates two pointers, and makes them both NULL pointers. When RegQueryValueEx() gets NULL pointers, apparently it's smart enough to not try to use them (which would crash the program). You need to actually create a buffer and DWORD variable, and pass their addresses. For example, to read a DWORD variable:

    Code:
    DWORD registryDataReturn;
    DWORD registryDataSize = sizeof(DWORD);
    
    RegQueryValueEx(
    	hKey,
    	"/*Any value within the current key*/",
    	0,
    	NULL,
    	&registryDataReturn,
    	&registryDataSize);
    
    printf("%d\n", registryDataReturn);
    If you needed to read a string variable, you would need to allocate a buffer large enough to handle the string. The following could handle any string of 255 or less characters:

    Code:
    BYTE registryStringReturn[256];
    DWORD registryDataSize = sizeof(registryStringReturn);
    
    RegQueryValueEx(
    	hKey,
    	"/*Any value within the current key*/",
    	0,
    	NULL,
    	&registryStringReturn,
    	&registryDataSize);
    Last edited by Cat; 09-14-2003 at 10:46 PM.

  3. #3
    Registered User Xzyx987X's Avatar
    Join Date
    Sep 2003
    Posts
    107
    Ah thanks, what confused my there was that I was able to give it a string as raw data and that worked fine, but come to think of it, when you send a string as a constant it is treated like a pointer.

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
    By RhondaNichols in forum C++ Programming
    Replies: 4
    Last Post: 03-30-2005, 08:26 AM
  5. RegSetValueEx() problem
    By Kupo in forum Windows Programming
    Replies: 1
    Last Post: 02-09-2002, 10:16 PM