Sergio Calderon
Sergio Calderon

Reputation: 867

RegSetValueEx: creating a DWORD value with the const BYTE data type

I tried to search for this question, but did not find anything so here I go:

I am trying to create a Registry value using C code and the RegSetValueEx function from the WinAPI, and data types that are defined in the official documentation. I used these definitions:

PTCHAR regValueName = TEXT("dwValue");
DWORD regValueType = REG_DWORD;
BYTE regData = 1;
const BYTE *pRegData = &regData;
DWORD dataSize = sizeof(regData);

The HKEY value was returned by the RegCreateKeyEx that I am not showing here.

This is how I am using the function:

setValueKey = RegSetValueEx(*pHandleResult, regValueName, reserved,
                            regValueType, pRegData, dataSize);

setValueEx is a LONG type.

As you can notice, I am trying to create a DWORD value, so I defined the BYTE regData value as 1, and then a const BYTE pointer to that value, but it is not working. The value is created but the data says:

Invalid DWORD 32 bit value.

Registry view

I know that BYTE data type is defined as unsiged char, but I tried '1' and did not work. I also know that using a DWORD value as regData type works, but I would like to know why the official data type does not work for a DWORD value.

Can anyone tell me how can I create the value using the BYTE data type?

I did not put all the code, but I will if needed.

Thanks.

Upvotes: 0

Views: 1036

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 597101

Can anyone tell me how can I create the value using the BYTE data type?

A BYTE is only 1 byte in size, but the Registry is expecting a REG_DWORD value to be 4 bytes in size. So you must read/write 4 bytes, no more and no less, when dealing with a REG_DWORD value.

If your code has a value stored in a BYTE and you want to write it to the Registry as a REG_DWORD, you must extend it to a DWORD first, eg:

BYTE value = 1;
...
DWORD regDATA = value; // <-- here
BYTE *pRegData = (BYTE*) &regData;

Otherwise, if you truly want to write a BYTE value to the Registry using only 1 byte, you must use REG_BINARY instead of REG_DWORD.

See MSDN for more details:

Registry Value Types

Upvotes: 1

Paul Ogilvie
Paul Ogilvie

Reputation: 25286

You want to store a DWORD but give it a BYTE. First, this means that a part of the DWORD will be undefined. Second, and more serious, datasize will be 1 but that is not valid for a DWORD value. It needs to be 4 instead.

The type to be stored, the pointer to that value and the size for that value all must be correct.

LPCTSTR regValueName = TEXT("dwValue");
DWORD regValueType = REG_DWORD;
DWORD regData = 1;
const BYTE *pRegData = (BYTE *) &regData;
DWORD dataSize = sizeof(regData);

setValueKey = RegSetValueEx(*pHandleResult, regValueName, reserved,
                            regValueType, pRegData, dataSize);

Or simply:

DWORD regData = 1;
setValueKey = RegSetValueEx(*pHandleResult, TEXT("dwValue"), reserved,
                            REG_DWORD, (BYTE *) &regData, sizeof(regData));

Upvotes: 5

Related Questions