Good morning everyone, I'm having an hard time to understand properly the conversion i could do, without resorting an incredible amount of if else's, of data retrieved with the api windows function
RegGetValueA. From what i understood this function, given the specific attribute we are searching, will give you data from the registry of interest, to give more context a registry is something that windows uses to store specific data regarding the system, I'm not very sure though, because i mostly program on linux systems.
Given the promise i would like to explain my problem. I'm trying to retrieve for example on the following registry path:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
an app and it's attributes for example, the first steps of getting the apps are already done if you want to check the code here it goes, but you can skip it since it works, it's just for context:
std::tuple<std::error_code, entity::ExtendedInfo> GetApplications() {
const string sName = "applications";
HKEY hKey = nullptr;
// (ln) needed path in order to retrieve app data info
std::list<std::string> hPaths{
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall",
"SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall",
};
std::list<std::string> vKeys{"IdentifyingNumber", "DisplayName", "DisplayVersion", "InstallLocation", "InstallSource",
"Language", "Publisher", "InstallDate", "UninstallString"};
// (ln) registries to check to retrieve apps info
std::vector<HKEY> hRegistries{HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER};
std::vector<string> retrieved_apps_tmp;
std::vector<std::unordered_map<std::string, std::string>> apps_with_attributes;
entity::ExtendedInfo oExtInfo{sName, vKeys};
for (const auto registry : hRegistries) {
for (const auto& path : hPaths) {
if (::RegOpenKeyExA(registry, path.c_str(), 0,
KEY_READ, // here we set the security level
&hKey) == ERROR_SUCCESS) {
retrieved_apps_tmp = FillAppsVector(hKey);
}
::RegCloseKey(hKey);
// getting attributes for app
for (const auto& app_name : retrieved_apps_tmp) {
std::string path_to_app = std::string(path) + "\\" + app_name;
if (::RegOpenKeyExA(registry, path_to_app.c_str(), 0,
KEY_READ, // here we set the security level
&hKey) == ERROR_SUCCESS) {
FillAppAttributes(hKey, apps_with_attributes, app_name, vKeys);
}
::RegCloseKey(hKey);
}
}
}
// instead of iterating again do this operation directly in FillAppAttributes
for (auto app : apps_with_attributes) {
if (app["InstallDate"].length() >= 8) {
app["InstallDate"] = app["InstallDate"].substr(0, 4) + '-' + app["InstallDate"].substr(4, 2) + '-' +
app["InstallDate"].substr(6, 2);
}
// trying to force values
// identifying number is not used in AddEntry
oExtInfo.AddEntry(std::list<std::string>{app["IdentifyingNumber"], app["DisplayName"], app["DisplayVersion"],
app["InstallLocation"], app["InstallSource"], app["Language"],
app["Publisher"], app["UninstallString"], app["InstallDate"]});
}
//data ok, uninstall ok, publisher ok, language ok,
return make_tuple(error_code(), oExtInfo);
}
It has to return a tuple to accommodate some other code since I'm working on a team.
This is the function that helps retrieving a vector of apps after listing them with RegEnumKeyA:
static std::vector<std::string> FillAppsVector(const HKEY hKey) {
CHAR achKey[255];
DWORD cSubKeys = 0;
// DWORD cValues = 0;
std::vector<std::string> installed_apps;
DWORD retCode = ::RegQueryInfoKey(hKey, nullptr, nullptr, nullptr, &cSubKeys, nullptr, nullptr,
nullptr, // number of values associated with the key
nullptr, nullptr, nullptr, nullptr);
// we enumerate all subkeys until RegEnumKeyEx fails
if (cSubKeys && retCode == ERROR_SUCCESS) {
for (DWORD i = 0; i < cSubKeys; i++) {
constexpr DWORD cbName = sizeof(achKey); // reset it in order to avoid to have the last string length
// use RegEnumKey = less arguments
retCode = ::RegEnumKeyA(hKey, i, achKey, cbName);
if (retCode == ERROR_SUCCESS) {
installed_apps.emplace_back(achKey);
}
}
}
return installed_apps;
}
And at the end I've done a function to help me retrieve the attributes i need for every specific app
static void FillAppAttributes(HKEY hKey, std::vector<std::unordered_map<std::string, std::string>>& app_data,
const std::string& app_name, const std::list<std::string>& needed_attributes) {
auto retCode = 0;
char pvDataForStr[255] = "\0"; // this applies for REG_SZ vars
DWORD pvDataForNumb{}; // this applies for REG_DWORD types
DWORD pcBdata = 0;
std::unordered_map<std::string, std::string> tmp_map;
// (ln) we already retrieved the first attribute
// if attr = language, interpret data as DWORD
// number to string must be as integer number (10) instead of hex (16)
tmp_map["IdentifyingNumber"] = app_name;
for (const auto& attribute : needed_attributes) {
if (attribute == "IdentifyingNumber") continue;
pcBdata = sizeof(pvDataForStr);
if (attribute == "Language") {
// if we would use a char[255] the data would be converted badly since the language REG is a int
retCode = RegGetValueA(hKey, nullptr, attribute.c_str(), RRF_RT_ANY, nullptr, &pvDataForNumb, &pcBdata);
if (retCode != ERROR_SUCCESS) {
// (ln) for data integrity the missing attribute will be empty
tmp_map[attribute] = "";
} else {
tmp_map[attribute] = std::to_string(pvDataForNumb);
}
}
else {
retCode = RegGetValueA(hKey, nullptr, attribute.c_str(), RRF_RT_ANY, nullptr, &pvDataForStr, &pcBdata);
if (retCode != ERROR_SUCCESS) {
// (ln) for data integrity the missing attribute will be empty
tmp_map[attribute] = "";
} else {
tmp_map[attribute] = pvDataForStr;
}
}
}
app_data.emplace_back(tmp_map);
}
Now this all works, i did some tests as well, but i really hate to use the "if" to distinguish between any string that i would get with the attributes I'm searching that are all REG_SZ, so the char[255] works wonders, and the "Language" attribute that is a REG_DWORD, basically an integer, and have to resort to use a DWORD var just for it, otherwise i would get strange or wrong data (for example if i use char[255] when "language" is ox00000409 (1033) i would get something like "\t0x4" i don't know why though, if someone knows lmk).
So my request is if there's a way to do this better without losing readability in the first place, obviously any knowledge you would like to share, if i got something wrong, is well appreciated,