c++ Read Regsitery Value

LWY 1 Reputation point
2022-08-31T00:30:06.373+00:00

When getting Value using RegQueryValueEx(),
If the number of Value characters is more than 100, a problem occurs. (Value is between 100 and 200 characters)
Less than 100 characters is no problem.
How can I solve this?

include <stdio.h>

include <string.h>

include <iostream>

include <Windows.h>

include <winreg.h>

include <WinUser.h>

include "tchar.h""

int main()
{
LONG lResult;
HKEY hKey;
DWORD dwType;
DWORD dwBytes = 100;
char buffer[200];

lResult = RegOpenKeyEx(HKEY_CURRENT_USER, "MyPrg\\TEST", 0, KEY_ALL_ACCESS | KEY_WOW64_64KEY, &hKey);  
if (lResult != ERROR_SUCCESS)  
{  
    MessageBox(NULL, "Register Open Error", "Error", MB_OK);  
}  

lResult = RegQueryValueEx(hKey, "FilePath", 0, &dwType, (LPBYTE)buffer, &dwBytes);  

if(lResult == ERROR_SUCCESS)  
    MessageBox(NULL, buffer, "OK", MB_OK);  
else  
    MessageBox(NULL, "Register Value Error", "Error", MB_OK);  

RegCloseKey(hKey);  

return 0;  

}

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,636 questions
{count} votes

2 answers

Sort by: Most helpful
  1. RLWA32 43,306 Reputation points
    2022-08-31T01:16:30.87+00:00

    The function documentation says "If the buffer specified by lpData parameter is not large enough to hold the data, the function returns ERROR_MORE_DATA and stores the required buffer size in the variable pointed to by lpcbData. In this case, the contents of the lpData buffer are undefined."

    The posted code calls RegQueryValueEx and tells the function that the size of the output buffer is 100 bytes even though the buffer size is actually 200 bytes. So understating the size of the buffer will cause RegQueryValueEx to fail if the data stored in the registry requires a buffer greater than 100 bytes. Initialize dwBytes to the actual size of the buffer.

    0 comments No comments

  2. jos Well 1 Reputation point
    2022-09-03T21:56:52.047+00:00

    I simultaneously learned a lot about C++ from this site and from my secondary school.

    0 comments No comments