[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Update registry...

Posted on 2002-05-23
25
Medium Priority
?
430 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 400 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

831 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