• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 432
  • Last Modified:

Update registry...

Hi,

How can i update the registry?

value to be updated, ex:   3232343423
registry key:              "Software\\Test\\test"
HKEY:                      HKEY_LOCAL_MACHINE
(Default) value:           some #

I want to write a class setRegValue to handle update the registry value for that key.  So that somewhere in my other function, i would call this class passing the needed parameters to update the registry.
I tried RegSetValueEx, but for some reason it doesn't work.

Please provide me with code.  Thanks!

TommyN14
0
TommyN14
Asked:
TommyN14
  • 13
  • 8
  • 3
  • +1
1 Solution
 
jkrCommented:
Could you post your code? The following should work:

BOOL SetValue ( DWORD dw)
{
  HKEY    hKey;
  DWORD   dwErr;

  char*   pszName = "Software\\Test\\test";

  if ( ERROR_SUCCESS != RegOpenKeyEx ( HKEY_LOCAL_MACHINE, pszName, 0, KEY_ALL_ACCESS, &hKey) {

     return ( FALSE);
  }

 dwErr = RegSetValueEx ( hKey,
                               "",
                               NULL,
                               REG_DWORD,
                               ( LPBYTE) dw,
                               sizeof     ( DWORD)
                              );

  RegCloseKey ( hKey);

  return ( dwErr == ERROR_SUCCESS ? TRUE : FALSE);
}
0
 
TommyN14Author Commented:
JKR,

I tested your function and it doesnt work.

i copied your function into 1 of my fileA.cpp file and and make it a static function.  I called it from the another file, and here is how i call it...
BOOL b = fileA::setValue(ulValue); where ulValue is an unsigned long value (10 digits)
and i printed out the value of b.  It gave me a (null) value for b.

Why?

TommyN14
0
 
TommyN14Author Commented:
Well, b value always FALSE.  What have i done wrong?
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
mirtolCommented:
If 'test' is the name of a value within the key 'Test' it will fail (you said it was a key in your q but...)

If so you need to change pszName to "Software\\Test"

And change the second param of RegSetValueEx to "test"
0
 
zechisCommented:
What system are you doing this on?

On Win 95/98/ME the default value MUST be REG_SZ.

You can use what you have above for 2K+ only.
0
 
TommyN14Author Commented:
No, 'test' is the name of the key.  It locates under Software\Test\.  And the (default) value of 'test' is '0'.

Now i want to update the value of key 'test' from '0' to an unsigned long number.

I'm using Win NT4
0
 
mirtolCommented:
Just to be clear...

If in regedit the 'test' appears in the right-hand pane, it is a value not a key so you need to use the above method.

Otherwise you must have it set to a string "0" as zechis is 100% top-notch correct - Keys can only contain string values unless you're in win2k
0
 
TommyN14Author Commented:
'test' is on the left-hand pane, on the right-hand pane, i have the icon [ab](Default) for the NAME column, and "0" for the DATA column.
0
 
zechisCommented:
That "0" in the Data column is a string value "\x48\0", not the number 0.

You only have to modify your code like this to get it to work:

char szValueData[16];

wsprintf( szValueData, "%u", dw );
dwErr = RegSetValueEx ( hKey,
                        "",
                        NULL,
                        REG_SZ,
                        (LPBYTE)szValueData,
                        strlen( szValueData )
                       );

and when reading it, just convert it back to a number before using it (with atoi(..) or something)
0
 
TommyN14Author Commented:
Ok this is what i did

BOOL fileA::setRegValue(DWORD dwValue)
{
  HKEY     hKey;
  DWORD     dwErr;
  char*     regKey = "Software\\Test\\test";

  if ( ERROR_SUCCESS != RegOpenKeyEx ( HKEY_LOCAL_MACHINE, regKey, 0, KEY_ALL_ACCESS, &hKey) )
  {
    return ( FALSE);
  }

  dwErr = RegSetValueEx(hKey, "", NULL, REG_DWORD, (LPBYTE) dwValue, sizeof( DWORD ));

  RegCloseKey(hKey);

  return (dwErr == ERROR_SUCCESS ? TRUE : FALSE);    
}

in my other function, i have a loop to call this function frequently to check the value and update it.  This is how i call the function by...

BOOL b = fileA::setRegValue(ulCurrentTime); // ulCurrentTime is an usigned long value.

It's work once, and then it doesn't work anymore.  I don't know why.  Please help!

TommyN14
0
 
mirtolCommented:
Is the failure coming from the RegOpenKeyEx function or from the RegSetValueEx function?
0
 
TommyN14Author Commented:
I have a fprintf line inside the RegOpenKeyEx 'if' condition, but it never print out.  So i guess it Open the Reg successfully.  I think the failure occurs at the RegSetValueEx function.

Please help!  Thanks!

TommyN14
0
 
mirtolCommented:
Does the key exist? If not, it will fail. You MUST use RegCreateKeyEx to create keys.
0
 
TommyN14Author Commented:
The Key is already there.  I just need to update the value of the key
0
 
mirtolCommented:
and you've tried

dwErr = RegSetValueEx(hKey, "", NULL, REG_SZ, (LPBYTE)sprintf(Buf, "%ld", dw), strlen(Buf));

??
0
 
zechisCommented:
sprintf() returns the number of characters written to the buffer.  I don't think that's what Tommy is intending.
0
 
TommyN14Author Commented:
what is the type of Buf?
0
 
mirtolCommented:
zechis - you;re right, sorry...

char Buf[16];

dwErr = RegSetValueEx(hKey, "", NULL, REG_SZ, (LPBYTE)Buf, sprintf(Buf, "%ld", dw));
0
 
TommyN14Author Commented:
I'm very new to dealing with the Registry and C++ here.
What i'm trying to do is to update the key value (key is 'test' and value is the "2323232323" some number) with the current time value (in milliseconds).

I tried to declare Buf as char* Buf = NULL; and do what mirtol said, i got an error when it read that line and stop the service.  
FYI: the error was "The instruction at '0x10217a50' referenced memory at '0x00000000'.  The memory could not be read"

0
 
mirtolCommented:
!

You don't don't want char *Buf = NULL;

that will not work!

you need

char Buf[16];

To actually ALLOCATE 16 bytes of RAM...
0
 
TommyN14Author Commented:
I'm also tried the approach of Zechis, and samething happened.
0
 
TommyN14Author Commented:
Still not working, gave me the same error
0
 
TommyN14Author Commented:
Still not working, gave me the same error
0
 
mirtolCommented:
As in the 'ref mem at 0x00000000' error?

If so, can you list the exact code in your function then please...
0
 
TommyN14Author Commented:
I got it.  Turn out, one of my fprintf, i assigned a wrong printf format specification.  That's why it gave me an memory error.
Thanks you all for your great effort!  Have a great holidays!

TommyN14
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 13
  • 8
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now