RegSetValueEx: create a DWORD value with the data type BYTE

1

On the official Microsoft documentation the function RegSetValueEx of the Windows API is defined as follows:

LONG WINAPI RegSetValueEx(
  _In_             HKEY    hKey,
  _In_opt_         LPCTSTR lpValueName,
  _Reserved_       DWORD   Reserved,
  _In_             DWORD   dwType,
  _In_       const BYTE    *lpData,
  _In_             DWORD   cbData
);

As you can see, the value *lpData , created to store the data of the value, is defined as a const BYTE , which is declared in WinDef.h as unsigned char .

Here are my definitions:

//Variables needed for registry value creation.
PTCHAR regValueName = TEXT("dwValue");
DWORD regValueType = REG_DWORD;
BYTE regData = 1;
const BYTE *pRegData = &regData;
DWORD dataSize = sizeof(regData);

This is how I am calling the RegSetValueEx function:

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

The *pHandleResult is a HANDLE returned by RegOpenKeyEx , but I am not displaying that code here so as not to make the code too long.

I want to create a DWORD value with content of 1, and the function creates it, but the content ends up being this text:

  

«Invalid (DWORD 32 bit) value»

I know that I can define the regData as a DWORD , assign 1 and that would work, but I want to know basically why it does not work for me using the data that is declared by the official Microsoft documentation. What am I doing wrong?

I appreciate the help. If you need any other information, I will gladly give it.

    
asked by Sergio Calderon 19.05.2016 в 19:30
source

1 answer

0

An integer in the jargon of the API is a DWORD that is a double word. Each word (WORD) is 2 bytes, therefore a DWORD is 4 bytes, that is 32 bits, a 32 bit integer.

So things need to define the following variables and call the function

HKEY hKey = HKEY_CURRENT_USER;  
LPCTSTR lpValueName = L"MyDemoValue";//L es para indicar que es una cadena larga (L=long) equivale a decir una cadena unicode  
DWORD Reserved=0;  
DWORD dwType = REG_DWORD;  
DWORD Value = 1;  
DWORD cbData = sizeof(Value);  
RegSetValueEx(hKey, lpValueName, Reserved, dwType, (const BYTE *) &Value, cbData);  

Note that the function requires that lpData be an array of BYTE but we are passing it a DWORD (integer) applying some kind of HELL CAST! ... recueden C ++ = granular control of memory.

All data, ANYTHING THAT IS, at the end is just a bunch of bytes, so we've done the following:

Our data is the number 1 saved in Value &Value is not more than the memory address of the variable Value.

But that memory address would be type DWORD *, of course the data type is DWORD therefore the puncher should be like this The function does not expect a DWORD * expect a BYTE *

But to the end EVERYTHING is bytes so a DWORD * can cast it to BYTE * without problem

Someone asked me the same question a couple of years ago, so I have a complete and super detailed article about it.

A look at the RegSetValueEx function of the Windows API

    
answered by 19.05.2016 / 22:34
source