Skip to content

Instantly share code, notes, and snippets.

@caiorss
Created September 8, 2018 00:08
Show Gist options
  • Save caiorss/0a994cb739994ade349d50de8db12d11 to your computer and use it in GitHub Desktop.
Save caiorss/0a994cb739994ade349d50de8db12d11 to your computer and use it in GitHub Desktop.
Query Windows Registry with Win32 API and C++11
Compile with MSVC-2017
$ cl.exe registry.cpp /EHsc /Zi /nologo /Fe:out.exe Advapi32.lib
Compile with G++ / Mingw
$ g++ registry.cpp -o out.exe -std=c++11 && out.exe
Sample output:
$ out2.exe
========= Test 1 ======
Registry value of ProgramFilesDir = C:\Program Files
========= Test 2 ======
CommonFilesDir (x86) = C:\Program Files (x86)\Common Files
FailedKeyCommonFilesDir =
InprocServer32 = C:\Windows\System32\wbem\Microsoft.Uev.AgentWmi.dll
========= Test 3 ======
Value of InProcServer32[Default] = C:\Windows\System32\wbem\Microsoft.Uev.AgentWmi.dll
Value of Invalid Key =
========= Test 4 ======
Show information about HKEY_CLASSES_ROOT\CLSID
Subkeys = 5563
Max subkey length = 83
Max subkey len = 83
Number of valus = 0
Max value length = 0
nKeys = 10
Show first 10 Subkeys =
- *
- .386
- .3g2
- .3gp
- .3gp2
- .3gpp
- .3mf
- .a
- .aac
- .ac3
Show information about HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion
Subkeys = 6
Max subkey length = 11
Max subkey len = 11
Number of valus = 0
Max value length = 0
nKeys = 6
Show first 10 Subkeys =
- BCD0000000
// Author: Caio Rodrigues
// Description: Query Windows Registry with Win32 API and C++11
// Dependency: Advapi32.lib
#include <iostream>
#include <string>
#include <windows.h>
bool readRegistryA(HKEY hkey, const std::string& path, const std::string& key, std::string& result);
std::string readRegistryB(HKEY hkey, const std::string& path, const std::string& key);
std::string readRegistryC(const std::string& pathToKey, const std::string& key);
void showKeyInfo(HKEY hkey, const std::string& path);
int main()
{
std::cout << "\n========= Test 1 ====== " << std::endl;
HKEY hkey = HKEY_LOCAL_MACHINE;
std::string path = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion";
std::string value;
bool status;
status = readRegistryA(hkey, path, "ProgramFilesDir", value);
if(status)
std::cerr << "Registry value of ProgramFilesDir = " << value << std::endl;
else
std::cerr << "Reading registry failed" << std::endl;
std::cout << "\n========= Test 2 ====== " << std::endl;
std::cout << "CommonFilesDir (x86) = " << readRegistryB(hkey, path, "CommonFilesDir (x86)") << std::endl;
std::cout << "FailedKeyCommonFilesDir = " << readRegistryB(hkey, path, "Faileasdx757(x86)") << std::endl;
HKEY hKey2 = HKEY_CLASSES_ROOT;
std::string path2 = "CLSID\\{EC231970-6AFD-4215-A72E-97242BB08680}\\InProcServer32";
std::cout << "InprocServer32 = " << readRegistryB(hKey2, path2, "") << std::endl;
std::cout << "\n========= Test 3 ====== " << std::endl;
std::string pathToKey = "HKEY_CLASSES_ROOT\\CLSID\\{EC231970-6AFD-4215-A72E-97242BB08680}\\InProcServer32";
std::cout << "Value of InProcServer32[Default] = " << readRegistryC(pathToKey, "") << std::endl;
std::cout << "Value of Invalid Key = " << readRegistryC("InvalidKey", "") << std::endl;
std::cout << "\n========= Test 4 ====== " << std::endl;
std::cout << "\n Show information about HKEY_CLASSES_ROOT\\CLSID" << std::endl;
showKeyInfo(HKEY_CLASSES_ROOT, "CLSID");
std::cout << "\n Show information about HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion" << std::endl;
showKeyInfo(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion");
return 0;
}
bool readRegistryA(
HKEY hkey
,const std::string& path
,const std::string& key
,std::string& result
){
HKEY key_handle;
DWORD Type = REG_SZ;
result = std::string(1024, 0);
DWORD size = result.size();
if(RegOpenKeyExA(hkey, path.c_str(), 0, KEY_QUERY_VALUE, &key_handle) != ERROR_SUCCESS)
return false;
if(RegQueryValueEx(key_handle, key.c_str(), NULL, &Type, (LPBYTE) &result[0], &size) != ERROR_SUCCESS)
{
RegCloseKey (key_handle);
return false;
}
RegCloseKey (key_handle);
result.resize(size-1);
return true;
}
std::string readRegistryB(
HKEY hkey
,const std::string& path
,const std::string& key
){
HKEY key_handle;
DWORD Type = REG_SZ;
auto result = std::string(1024, 0);
DWORD size = result.size();
if(RegOpenKeyExA(hkey, path.c_str(), 0, KEY_QUERY_VALUE, &key_handle) != ERROR_SUCCESS)
return "";
if(RegQueryValueEx(key_handle, key.c_str(), NULL, &Type, (LPBYTE) &result[0], &size) != ERROR_SUCCESS)
{
RegCloseKey (key_handle);
return "";
}
RegCloseKey (key_handle);
result.resize(size-1);
return result;
}
std::string readRegistryC(const std::string& pathToKey, const std::string& key){
HKEY key_handle;
DWORD Type = REG_SZ;
auto result = std::string(1024, 0);
DWORD size = result.size();
HKEY hkey;
int npos = pathToKey.find_first_of('\\');
std::string hKeyStr = pathToKey.substr(0, npos);
std::string path = pathToKey.substr(npos + 1);
if(hKeyStr == "HKEY_CLASSES_ROOT")
hkey = HKEY_CLASSES_ROOT;
else if (hKeyStr == "HKEY_LOCAL_MACHINE")
hkey = HKEY_LOCAL_MACHINE;
else if (hKeyStr == "HKEY_CURRENT_USER")
hkey = HKEY_CURRENT_USER;
else if (hKeyStr == "HKEY_USRS")
hkey = HKEY_USERS;
else if (hKeyStr == "HKEY_PERFORMANCE_DATA")
hkey = HKEY_PERFORMANCE_DATA;
else
return "";
if(RegOpenKeyExA(hkey, path.c_str(), 0, KEY_QUERY_VALUE, &key_handle) != ERROR_SUCCESS)
return "";
if(RegQueryValueEx(key_handle, key.c_str(), NULL, &Type, (LPBYTE) &result[0], &size) != ERROR_SUCCESS)
{
RegCloseKey (key_handle);
return "";
}
RegCloseKey (key_handle);
result.resize(size-1);
return result;
}
void showKeyInfo(HKEY hkey, const std::string& path){
std::string path2 = path;
DWORD size = path2.length();
DWORD subKeys;
DWORD maxSubKeysLen;
DWORD maxClassLen;
DWORD maxValueLen;
DWORD values;
LSTATUS result =
RegQueryInfoKeyA(
hkey, // key
&path2[0], // lpClass
&size, // lpcchClass
NULL, // lpReserved
&subKeys, // lpcSubKeys
&maxSubKeysLen, // lpcbMaxSubKeyLen
&maxClassLen, // lpcbMaxClassLen
&values, // lpcValues
nullptr, // lpcbMaxValueNameLen
&maxValueLen, // lpcbMaxValueLen
nullptr, // lpcbSecurityDescriptor
nullptr // lpftLastWriteTime
);
std::cout << "Subkeys = " << subKeys << std::endl;
std::cout << "Max subkey length = " << maxSubKeysLen << std::endl;
std::cout << "Max subkey len = " << maxSubKeysLen << std::endl;
std::cout << "Number of valus = " << values << std::endl;
std::cout << "Max value length = " << maxValueLen << std::endl;
DWORD nKeys = subKeys >= 10 ? 10 : subKeys;
std::cout << "nKeys = " << nKeys << std::endl;
std::cout << "Show first 10 Subkeys = " << std::endl;
for(DWORD index = 0; index < nKeys; index++){
std::string buffer(static_cast<int>(maxSubKeysLen), 0);
DWORD bufferLen = buffer.length();
RegEnumKeyExA(
hkey,
index,
&buffer[0],
&bufferLen,
nullptr,
&path2[0],
&size,
nullptr
);
buffer.resize(bufferLen);
if(bufferLen != 0)
std::cout << " - " << buffer << std::endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment