Repository: microsoft/PowerToys
File: src/common/utils/gpo.h (C/C++)

Code before:
            RegCloseKey(key);

            if (res != ERROR_SUCCESS) {
                return gpo_rule_configured_not_configured;
            }
        }

        switch (value)
        {
        case 0:
            return gpo_rule_configured_disabled;
        case 1:
            return gpo_rule_configured_enabled;
        default:
            return gpo_rule_configured_wrong_value;
        }
    }

    inline std::string getPolicyListValue(const std::wstring& registry_list_path, const std::wstring& registry_list_value_name)
    {
        // This function returns the value of an entry of an policy list. The user scope is only checked, if the list is not enabled for the machine to not mix the lists. If there is no value or no list is found in the registry we return an empty string.

        HKEY key{};
        DWORD reg_value_type = REG_SZ;
        char* string_value{};
        DWORD string_value_length = sizeof(char*);

        // Try to read from the machine list.
        bool machine_list_found = false;
        if (auto res = RegOpenKeyExW(POLICIES_SCOPE_MACHINE, registry_list_path.c_str(), 0, KEY_READ, &key); res == ERROR_SUCCESS)
        {
            machine_list_found = true;

            // If the path exists in the machine registry, we try to read the value.
            auto resValue = RegQueryValueEx(key, registry_list_value_name.c_str(), NULL, &reg_value_type, reinterpret_cast<LPBYTE>(&string_value), &string_value_length);
            RegCloseKey(key);

            if (resValue == ERROR_SUCCESS)
            {
                // Return the value from the machine list.
                return string_value;
            }
        }

        // If no list exists for machine, we try to read from the user list.
        if (!machine_list_found)
        {
            if (auto res = RegOpenKeyExW(POLICIES_SCOPE_USER, registry_list_path.c_str(), 0, KEY_READ, &key); res == ERROR_SUCCESS)
            {
                // If the path exists in the user registry, we try to read the value.
                auto resValue = RegQueryValueEx(key, registry_list_value_name.c_str(), NULL, &reg_value_type, reinterpret_cast<LPBYTE>(&string_value), &string_value_length);

Review comment (suggestion):
@jaimecbernardo 
Is using `char*` the correct way of doing it for reading a REG_SZ?

Btw, the GPO and setting ui parts are ready and working. (I still have to implement the wox part.) So if you are interested, you can try it out. 😉

Code after:
        HKEY key{};
        DWORD value = 0xFFFFFFFE;
        DWORD valueSize = sizeof(value);

        bool machine_key_found = true;
        if (auto res = RegOpenKeyExW(POLICIES_SCOPE_MACHINE, POLICIES_PATH.c_str(), 0, KEY_READ, &key); res != ERROR_SUCCESS)
        {
            machine_key_found = false;
        }

        if(machine_key_found)
        {
            // If the path was found in the machine, we need to check if the value for the policy exists.
            auto res = RegQueryValueExW(key, registry_value_name.c_str(), nullptr, nullptr, reinterpret_cast<LPBYTE>(&value), &valueSize);

            RegCloseKey(key);

            if (res != ERROR_SUCCESS) {
                // Value not found on the path.
                machine_key_found=false;
            }
        }

        if (!machine_key_found)
        {
            // If there's no value found on the machine scope, try to get it from the user scope.
            if (auto res = RegOpenKeyExW(POLICIES_SCOPE_USER, POLICIES_PATH.c_str(), 0, KEY_READ, &key); res != ERROR_SUCCESS)
            {
                if (res == ERROR_FILE_NOT_FOUND) {
                    return gpo_rule_configured_not_configured;
                }
                return gpo_rule_configured_unavailable;
            }
            auto res = RegQueryValueExW(key, registry_value_name.c_str(), nullptr, nullptr, reinterpret_cast<LPBYTE>(&value), &valueSize);
            RegCloseKey(key);

            if (res != ERROR_SUCCESS) {
                return gpo_rule_configured_not_configured;
            }
        }

        switch (value)
        {
        case 0:
            return gpo_rule_configured_disabled;
        case 1:
            return gpo_rule_configured_enabled;
        default:
            return gpo_rule_configured_wrong_value;
        }
    }