Solved

Update registry...

Posted on 2002-05-23
25
424 Views
Last Modified: 2008-02-01
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
Comment
Question by:TommyN14
  • 13
  • 8
  • 3
  • +1
25 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 7030211
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
 
LVL 1

Author Comment

by:TommyN14
ID: 7030338
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
 
LVL 1

Author Comment

by:TommyN14
ID: 7030482
Well, b value always FALSE.  What have i done wrong?
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

 
LVL 2

Expert Comment

by:mirtol
ID: 7030628
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
 
LVL 2

Expert Comment

by:zechis
ID: 7030629
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
 
LVL 1

Author Comment

by:TommyN14
ID: 7030645
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
 
LVL 2

Expert Comment

by:mirtol
ID: 7030656
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
 
LVL 1

Author Comment

by:TommyN14
ID: 7030769
'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
 
LVL 2

Expert Comment

by:zechis
ID: 7030801
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
 
LVL 1

Author Comment

by:TommyN14
ID: 7030903
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
 
LVL 2

Expert Comment

by:mirtol
ID: 7031441
Is the failure coming from the RegOpenKeyEx function or from the RegSetValueEx function?
0
 
LVL 1

Author Comment

by:TommyN14
ID: 7032138
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
 
LVL 2

Expert Comment

by:mirtol
ID: 7032195
Does the key exist? If not, it will fail. You MUST use RegCreateKeyEx to create keys.
0
 
LVL 1

Author Comment

by:TommyN14
ID: 7032404
The Key is already there.  I just need to update the value of the key
0
 
LVL 2

Expert Comment

by:mirtol
ID: 7032491
and you've tried

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

??
0
 
LVL 2

Expert Comment

by:zechis
ID: 7032554
sprintf() returns the number of characters written to the buffer.  I don't think that's what Tommy is intending.
0
 
LVL 1

Author Comment

by:TommyN14
ID: 7032557
what is the type of Buf?
0
 
LVL 2

Accepted Solution

by:
mirtol earned 100 total points
ID: 7032572
zechis - you;re right, sorry...

char Buf[16];

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

Author Comment

by:TommyN14
ID: 7032606
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
 
LVL 2

Expert Comment

by:mirtol
ID: 7032612
!

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
 
LVL 1

Author Comment

by:TommyN14
ID: 7032614
I'm also tried the approach of Zechis, and samething happened.
0
 
LVL 1

Author Comment

by:TommyN14
ID: 7032643
Still not working, gave me the same error
0
 
LVL 1

Author Comment

by:TommyN14
ID: 7032665
Still not working, gave me the same error
0
 
LVL 2

Expert Comment

by:mirtol
ID: 7032682
As in the 'ref mem at 0x00000000' error?

If so, can you list the exact code in your function then please...
0
 
LVL 1

Author Comment

by:TommyN14
ID: 7032697
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question