// @descr: enumerates all variables(user and system) by index // USER SCOPE: [0 .. user var count - 1] // SYSTEM SCOPE: [user var count .. system var count - 1] BOOL winapi_read_by_index(int index, int* out_scope, char** out_name, char** out_value) { /// vars /// HKEY hKey; int current_scope = -1; /// decide which scope index belongs to /// /// and open key containing the requested variable /// int user_scope_var_count = winapi_var_count_scope(USER_SCOPE); if (index >= user_scope_var_count) // system range { index = index - user_scope_var_count; // convert index to system range if (index >= winapi_var_count_scope(SYSTEM_SCOPE)) return FALSE; // out of range -> fail open_variables_key(SYSTEM_SCOPE, &hKey); current_scope = SYSTEM_SCOPE; } else if (index >= 0) // user range { // index doesn't need adjustment open_variables_key(USER_SCOPE, &hKey); current_scope = USER_SCOPE; } else { return FALSE; // out of range -> fail } // allocate temp buffers on stack for name and value char name_buffer [sizeof(char)*(get_longest_name(hKey)+1)]; char value_buffer [sizeof(char)*(get_longest_value(hKey)+1)]; name_buffer[0]=0; // null terminate value_buffer[0]=0; // buffer size / bytes copied to buffer DWORD in_out_name_size = sizeof(name_buffer); DWORD in_out_value_size = sizeof(value_buffer); /// retrieve name and value into buffers /// RegEnumValue(hKey, index, name_buffer, &in_out_name_size, // size not including the terminating 0 0, NULL, value_buffer, &in_out_value_size); // likewise RegCloseKey(hKey); /// allocate and copy to new heap buffers, return them through output arguments /// *out_name = malloc(in_out_name_size + 1); // + 1 for null terminator memcpy(*out_name, name_buffer, in_out_name_size + 1); // copy string + null terminator *out_value = malloc(in_out_value_size+1); memcpy(*out_value, value_buffer, in_out_value_size + 1); *out_scope = current_scope; return TRUE; };
/** * Get the longest name (by UTF-8 length) in the set. * * Returns: The longest nontrivial name, or "" if none. */ const std::string& get_longest_name() { return get_longest_name(set::utflength_rate); }