bool verify_event_log_registry(std::basic_string< CharT > const& reg_key, bool force, registry_params< CharT > const& params) { typedef std::basic_string< CharT > string_type; typedef registry_traits< CharT > registry; // Open the key HKEY hkey = 0; LSTATUS res = registry::open_key( HKEY_LOCAL_MACHINE, reg_key.c_str(), REG_OPTION_NON_VOLATILE, KEY_READ, &hkey); if (res != ERROR_SUCCESS) return false; auto_hkey_close hkey_guard(hkey); if (force) { // Verify key values if (!!params.event_message_file) { string_type module_name; res = registry::get_value(hkey, registry::get_event_message_file_param_name(), module_name); if (res != ERROR_SUCCESS || module_name != params.event_message_file.get()) return false; } if (!!params.category_message_file) { string_type module_name; res = registry::get_value(hkey, registry::get_category_message_file_param_name(), module_name); if (res != ERROR_SUCCESS || module_name != params.category_message_file.get()) return false; } if (!!params.category_count) { // Set number of categories DWORD category_count = 0; res = registry::get_value(hkey, registry::get_category_count_param_name(), category_count); if (res != ERROR_SUCCESS || category_count != params.category_count.get()) return false; } if (!!params.types_supported) { // Set the supported event types DWORD event_types = 0; res = registry::get_value(hkey, registry::get_types_supported_param_name(), event_types); if (res != ERROR_SUCCESS || event_types != params.types_supported.get()) return false; } } return true; }
void init_event_log_registry( std::basic_string< CharT > const& log_name, std::basic_string< CharT > const& source_name, bool force, registry_params< CharT > const& params) { typedef std::basic_string< CharT > string_type; typedef registry_traits< CharT > registry; // Registry key name that contains log description string_type reg_key = registry::make_event_log_key(log_name, source_name); // First check the registry keys and values in read-only mode. // This allows to avoid UAC asking for elevated permissions to modify HKLM registry when no modification is actually needed. if (verify_event_log_registry(reg_key, force, params)) return; // Create or open the key HKEY hkey = 0; DWORD disposition = 0; LSTATUS res = registry::create_key( HKEY_LOCAL_MACHINE, reg_key.c_str(), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkey, &disposition); if (res != ERROR_SUCCESS) BOOST_LOG_THROW_DESCR(system_error, "Could not create registry key for the event log"); auto_hkey_close hkey_guard(hkey); if (disposition != REG_OPENED_EXISTING_KEY || force) { // Fill registry values if (!!params.event_message_file) { // Set the module file name that contains event resources string_type const& module_name = params.event_message_file.get(); res = registry::set_value( hkey, registry::get_event_message_file_param_name(), 0, REG_EXPAND_SZ, reinterpret_cast< LPBYTE >(const_cast< CharT* >(module_name.c_str())), static_cast< DWORD >((module_name.size() + 1) * sizeof(CharT))); if (res != ERROR_SUCCESS) { BOOST_LOG_THROW_DESCR(system_error, "Could not create registry value " + log::aux::to_narrow(string_type(registry::get_event_message_file_param_name()))); } } if (!!params.category_message_file) { // Set the module file name that contains event category resources string_type const& module_name = params.category_message_file.get(); res = registry::set_value( hkey, registry::get_category_message_file_param_name(), 0, REG_SZ, reinterpret_cast< LPBYTE >(const_cast< CharT* >(module_name.c_str())), static_cast< DWORD >((module_name.size() + 1) * sizeof(CharT))); if (res != ERROR_SUCCESS) { BOOST_LOG_THROW_DESCR(system_error, "Could not create registry value " + log::aux::to_narrow(string_type(registry::get_category_message_file_param_name()))); } } if (!!params.category_count) { // Set number of categories DWORD category_count = params.category_count.get(); res = registry::set_value( hkey, registry::get_category_count_param_name(), 0, REG_DWORD, reinterpret_cast< LPBYTE >(&category_count), static_cast< DWORD >(sizeof(category_count))); if (res != ERROR_SUCCESS) { BOOST_LOG_THROW_DESCR(system_error, "Could not create registry value " + log::aux::to_narrow(string_type(registry::get_category_count_param_name()))); } } if (!!params.types_supported) { // Set the supported event types DWORD event_types = params.types_supported.get(); res = registry::set_value( hkey, registry::get_types_supported_param_name(), 0, REG_DWORD, reinterpret_cast< LPBYTE >(&event_types), static_cast< DWORD >(sizeof(event_types))); if (res != ERROR_SUCCESS) { BOOST_LOG_THROW_DESCR(system_error, "Could not create registry value " + log::aux::to_narrow(string_type(registry::get_types_supported_param_name()))); } } } }
void init_event_log_registry( std::basic_string< CharT > const& log_name, std::basic_string< CharT > const& source_name, bool force, registry_params< CharT > const& params) { typedef std::basic_string< CharT > string_type; typedef registry_traits< CharT > registry; // Registry key name that contains log description string_type reg_key = registry::make_event_log_key(log_name, source_name); // Create or open the key HKEY hkey = 0; DWORD disposition = 0; LSTATUS res = registry::create_key( HKEY_LOCAL_MACHINE, reg_key.c_str(), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkey, &disposition); if (res != ERROR_SUCCESS) boost::log::aux::throw_exception(std::runtime_error("Could not create registry key for the event log")); auto_hkey_close hkey_guard(hkey); if (disposition != REG_OPENED_EXISTING_KEY || force) { // Fill registry values if (!!params.event_message_file) { // Set the module file name that contains event resources string_type const& module_name = params.event_message_file.get(); res = registry::set_value( hkey, registry::get_event_message_file_param_name(), 0, REG_EXPAND_SZ, reinterpret_cast< LPBYTE >(const_cast< CharT* >(module_name.c_str())), static_cast< DWORD >((module_name.size() + 1) * sizeof(CharT))); if (res != ERROR_SUCCESS) { boost::log::aux::throw_exception(std::runtime_error("Could not create registry value " + log::aux::to_narrow(string_type(registry::get_event_message_file_param_name())))); } } if (!!params.category_message_file) { // Set the module file name that contains event category resources string_type const& module_name = params.category_message_file.get(); res = registry::set_value( hkey, registry::get_category_message_file_param_name(), 0, REG_SZ, reinterpret_cast< LPBYTE >(const_cast< CharT* >(module_name.c_str())), static_cast< DWORD >((module_name.size() + 1) * sizeof(CharT))); if (res != ERROR_SUCCESS) { boost::log::aux::throw_exception(std::runtime_error("Could not create registry value " + log::aux::to_narrow(string_type(registry::get_category_message_file_param_name())))); } } if (!!params.category_count) { // Set number of categories DWORD category_count = params.category_count.get(); res = registry::set_value( hkey, registry::get_category_count_param_name(), 0, REG_DWORD, reinterpret_cast< LPBYTE >(&category_count), static_cast< DWORD >(sizeof(category_count))); if (res != ERROR_SUCCESS) { boost::log::aux::throw_exception(std::runtime_error("Could not create registry value " + log::aux::to_narrow(string_type(registry::get_category_count_param_name())))); } } if (!!params.types_supported) { // Set the supported event types DWORD event_types = params.types_supported.get(); res = registry::set_value( hkey, registry::get_types_supported_param_name(), 0, REG_DWORD, reinterpret_cast< LPBYTE >(&event_types), static_cast< DWORD >(sizeof(event_types))); if (res != ERROR_SUCCESS) { boost::log::aux::throw_exception(std::runtime_error("Could not create registry value " + log::aux::to_narrow(string_type(registry::get_types_supported_param_name())))); } } } }