Solved

Update registry...

Posted on 2002-05-23
25
421 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
 
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now