I'm trying to use C++ to query registry values like the following
$ reg query HKLM\SOFTWARE\GitForWindows
HKEY_LOCAL_MACHINE\SOFTWARE\GitForWindows
CurrentVersion REG_SZ 2.31.1
InstallPath REG_SZ C:\Program Files\Git
LibexecPath REG_SZ C:\Program Files\Git\mingw64\libexec\git-core
I've found many solutions but I can't seem to get it to work, I think the issue is that i'm using the code incorrectly.
Example 1 from Microsoft documentation
// get the size of the string
DWORD dataSize{};
LONG retCode = ::RegGetValue(
HKEY_LOCAL_MACHINE,
L"SOFTWARE", // subKey.c_str(),
L"GitForWindows", //value.c_str(),
RRF_RT_REG_SZ,
nullptr,
nullptr,
&dataSize
);
if (retCode != ERROR_SUCCESS)
{
printf("Error1\n");
return;
}
std::wstring data;
data.resize(dataSize / sizeof(wchar_t));
// store the string in data
retCode = ::RegGetValue(
HKEY_LOCAL_MACHINE,
L"SOFTWARE", // subKey.c_str(),
L"GitForWindows", //value.c_str(),
RRF_RT_REG_SZ,
nullptr,
&data[0],
&dataSize
);
if (retCode != ERROR_SUCCESS)
{
printf("Error2\n");
return;
}
printf("%s\n", data[0]);
This gets stuck at Error1
Example 2 from code online
int BUFFER = 1024;
char value[255];
DWORD BufferSize = BUFFER;
RegGetValue(HKEY_LOCAL_MACHINE, L"SOFTWARE", L"GitForWindows", RRF_RT_ANY, NULL, (PVOID)&value, &BufferSize);
std::cout << value << std::endl;
return;
This prints
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
The function for RegGetValue
is
LONG WINAPI RegGetValue(
_In_ HKEY hkey,
_In_opt_ LPCTSTR lpSubKey,
_In_opt_ LPCTSTR lpValue,
_In_opt_ DWORD dwFlags,
_Out_opt_ LPDWORD pdwType,
_Out_opt_ PVOID pvData,
_Inout_opt_ LPDWORD pcbData
);
I think the hkey
is HKEY_LOCAL_MACHINE
, lpSubKey
is this SOFTWARE
and if lpValue
is GitForWindows
, if this is correct then how do I access the values inside, e.g. CurrentVersion
?