RegQueryValueEx Problem ** HELP ME

the last two parameters to the function are the buffer and the size of buffer.
the buffer gets the value -- what should be the type of buffer if the reg type is reg_dword? what should be the type of sizeofbuffer variable?
heres my code
Byte buffer

Dword sizeofbuffer = sizeof(buffer) and the last two parameters of the function regquesryvalueex are &buffer,&sizeofbuffer but the function doesnt return error_success nor ERROR_MORE_DATA nor ERROR_FILE_NOT_FOUND.
what is the problem????

edit: another question! the first parameter to the function is the handle returned from regopenkeyex when should i call regclosekey()?? should i call it after determine that regopenkeyex return error_success or after determined that both regopenkeyex and regqueryvaluex retun error_success????

*******PLEASE HELP!!******
Last edited on
1
2
3
4
5
6
7
8
LONG WINAPI RegQueryValueEx(
  _In_         HKEY hKey,
  _In_opt_     LPCTSTR lpValueName,
  _Reserved_   LPDWORD lpReserved,
  _Out_opt_    LPDWORD lpType,
  _Out_opt_    LPBYTE lpData,
  _Inout_opt_  LPDWORD lpcbData
);


lpType is the type of the actual registry value; REG_SZ, REG_DWORD etc.
lpData is the buffer where the data is returned, but you need the value in plType to interpret that data.
lpcbData is the size of the buffer, lpData.

The article explains all and has an example.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms724911%28v=vs.85%29.aspx
i read the article and looked at it lots of times.
what is LPDWORD AND LPBYTE.
If u read closely u see i dont need iptype it can be null i dont want the get the reg type i already know its reg_dword.
answer my questions in my first post
thanks
what is LPDWORD AND LPBYTE
LPDWORD is a pointer to (or the address of) a DWORD, LPBYTE is a pointer to (or the address of) an BYTE.

If u read closely u see i dont need iptype it can be null i dont want the get the reg type i already know its reg_dword.
You always need to know the type that's been read or else you can't interpret what's come back. In your example, you may set a REG_DWORD value in the registry, but what if someone else sets the value as a REG_SZ? The two representations of 10 have different binary layouts.

I haven't written an example for you because I'm not on Windows and so can't test it. But there's an example here. http://www.cplusplus.com/forum/general/76879/

i did this
DWORD type=REG_DWORD;
BYTE data;
DWORD size=sizeof(data);


i got handle from regopekeyex that called path

i did
if(RegQueryValueEx(path,"photoshop",NULL,&type,&data,&size)==ERROR_SUCCESS)
{
if (data==0) cout <<"value is 0";
else cout <<"value is " <<data;

}
but nothing happed why???
when should i call regclosekey(path)???????
why its not working???? how i determine the value of data???

HELP
Last edited on
Ok, you need to decode data.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
DWORD dwValue = 0;

DWORD dwRet;
DWORD dwType;
BYTE bData[64];  // 64 bytes long
DWORD dwDataLen = sizeof(bData);

dwRet = RegQueryValueEx(path, "EnableLUA", NULL, &dwType, bData, &dwDataLen);
if (dwRet == ERROR_SUCCESS)
{
    switch (dwType)
    {
    case REG_SZ:  // convert string to number
//      dwValue = std::to_string(static_cast<const char*>(bData));  // C++11
        dwValue = atol(static_cast<const char*>(bData));  // C++98
        break;
    case REG_DWORD:  // extract a DWORD from a binary buffer
        dwValue = *((DWORD*)bData);  // C cast for brevity
        break;
    }
}
There is a problem on line 15 in that code snippet, it is NOT guaranteed that the buffer is nul terminated, so atol() can overflow.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms724911%28v=vs.85%29.aspx
If the data has the REG_SZ, REG_MULTI_SZ or REG_EXPAND_SZ type, the string may not have been stored with the proper terminating null characters. Therefore, even if the function returns ERROR_SUCCESS, the application should ensure that the string is properly terminated before using it; otherwise, it may overwrite a buffer. (Note that REG_MULTI_SZ strings should have two terminating null characters.) One way an application can ensure that the string is properly terminated is to use RegGetValue, which adds terminating null characters if needed.
Topic archived. No new replies allowed.