We help IT Professionals succeed at work.

How to set REG_DWORD value?

bansaldeep
bansaldeep asked
on
I want to update a registry key of type REG_DWORD.I'm using

BYTE val[1];
val[0] = 5;
RegSetValueEx(pKey,key_name,0,REG_DWORD,val,1);

but when i check registry it shows 'invalid DWORD value'.
Comment
Watch Question

Try

 RegSetValueEx(pKey,key_name,0,REG_DWORD,val,sizeof(DWORD));

I think pKey is handle, not a pointer to handle... right...?

GOOD LUCK
More info

RegSetValueEx(pKey,     // subkey handle
               key_name,     // value name
               0,          // must be zero
               REG_DWORD,     // value type
               val,          // pointer to value data
               sizeof(DWORD));     // length of value data

cheers

Author

Commented:
Getting some value but not accurate.
if val[0] = 5, Registry entry is 77f84f05
if val[0] = 6, Registry entry is 77f84f06
if val[0] = 7, Registry entry is 77f84f07

You can see the last 2 digits is what i actually need....
That is becoz of
"BYTE val[1];
val[0] = 5;"

Use

DWORD dwVal = 5;

RegSetValueEx(pKey,     // subkey handle
              key_name,     // value name
              0,          // must be zero
              REG_DWORD,     // value type
              (const BYTE *)&dwVal,// pointer to value data
              sizeof(DWORD));     // length of value data

Commented:
You're doing this WRONG!

DWORD val = 5;
LONG lResult;

lResult = RegSetValueEx(hKey, key_name, 0, REG_DWORD, (LPBYTE)&val, sizeof(DWORD));


Your problem is with:

BYTE val[1];

val[0] = 5;

A BYTE is a ONE BYTE value.  a DWORD is a 4 BYTE VALUE.  If you do:

val[0] = 5;

you set ONLY one byte to be 5.  What is in the other 3 bytes??  I dunno?  It looks like they end up being "77f84f" or some other such garbage.

Don't tell RegSetValueEx to write a REG_DWORD if you are only going to supply it with a single BYTE.  Use a REG_BINARY type to write one byte if that's what you really need.
Hi jhance,

    I write the same... Y u repeating?

Roshmon

Commented:
roshmon,

I don't see where you wrote what I wrote or vice-versa....

Whatever, you got the points, quit griping!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.