From c238b5afc88ead55aca680931d2b847bf689b625 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Thu, 15 Sep 2016 20:32:55 +0300 Subject: refactoring --- main.cpp | 88 ++++++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 55 insertions(+), 33 deletions(-) diff --git a/main.cpp b/main.cpp index 322626c..2d2ba1a 100644 --- a/main.cpp +++ b/main.cpp @@ -102,43 +102,47 @@ BOOL on_init_dialog(HWND wnd, HWND, LPARAM) return TRUE; } -void on_command(HWND wnd, int id, HWND, unsigned int) +void on_button_elevate_click(HWND wnd) { - switch (id) + bool as_admin = false; + + try { - case IDC_BUTTON_ELEVATE: - { - bool as_admin = false; - try - { - as_admin = is_run_as_administrator(); - } - catch (const Error& e) - { - error::report(e); - break; - } + as_admin = is_run_as_administrator(); + } + catch (const Error& e) + { + error::report(e); + return; + } - if (as_admin) - { - MessageBoxW(wnd, L"Already elevated!", L"Elevation", MB_OK); - break; - } + if (as_admin) + { + MessageBoxW(wnd, L"Already elevated!", L"Elevation", MB_OK); + return; + } - try - { - process::runas(process::get_executable_path()); - } - catch (const Error& e) - { - if (error::get_code(e) != ERROR_CANCELLED) - error::report(e); - break; - } + try + { + process::runas(process::get_executable_path()); + } + catch (const Error& e) + { + if (error::get_code(e) != ERROR_CANCELLED) + error::report(e); + return; + } - EndDialog(wnd, 1); + EndDialog(wnd, 1); +} + +void on_command(HWND wnd, int id, HWND, unsigned int) +{ + switch (id) + { + case IDC_BUTTON_ELEVATE: + on_button_elevate_click(wnd); break; - } case IDOK: case IDCANCEL: @@ -175,9 +179,27 @@ int APIENTRY wWinMain( wchar_t*, int) { - return static_cast(DialogBox( + const auto ret = DialogBoxW( instance, MAKEINTRESOURCE(IDD_MAINDIALOG), NULL, - dialog_main)); + dialog_main); + + switch (ret) + { + case -1: + try + { + error::raise("DialogBoxW"); + } + catch (const Error& e) + { + error::report(e); + return 1; + } + break; + + default: + return static_cast(ret); + } } -- cgit v1.2.3