#include <Windows.h>
#include <String>
std::wstring ReadRegistryKey(HKEY wsRoot, std::wstring wsPath, std::wstring wsKey)
{
HKEY hKey;
DWORD dwType;
DWORD dwData;
if (RegOpenKeyEx(wsRoot, wsPath.c_str(), 0, KEY_READ, &hKey) != ERROR_SUCCESS)
{
printf("ERROR: Register Path");
RegCloseKey(hKey);
}
else if (RegQueryValueEx(hKey, wsKey.c_str(), NULL, &dwType, NULL, &dwData) != ERROR_SUCCESS)
{
printf("ERROR: Register Key");
RegCloseKey(hKey);
}
else if (dwType != REG_SZ)
{
printf("ERROR: Register Type");
RegCloseKey(hKey);
}
std::wstring wsValue(dwData / sizeof(wchar_t), L'\0');
if (RegQueryValueEx(hKey, wsKey.c_str(), NULL, NULL, reinterpret_cast<LPBYTE>(&wsValue[0]), &dwData) != ERROR_SUCCESS)
{
printf("ERROR: Register Value");
RegCloseKey(hKey);
}
RegCloseKey(hKey);
size_t szNullTerminator = wsValue.find_first_of(L'\0');
if (szNullTerminator != std::string::npos)
{
wsValue.resize(szNullTerminator);
}
return wsValue;
}
int main()
{
std::wstring wsRegisterKey = ReadRegistryKey(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", L"CurrentBuild");
LPCWSTR lpRegisterKey = wsRegisterKey.c_str();
wprintf(lpRegisterKey);
getchar();
}