diff options
Diffstat (limited to 'src/main.cpp')
-rw-r--r-- | src/main.cpp | 292 |
1 files changed, 146 insertions, 146 deletions
diff --git a/src/main.cpp b/src/main.cpp index d13325c..511cb02 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,211 +18,211 @@ #include <exception> #include <string> +#if defined(_MSC_VER) #pragma comment(linker, "/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"") +#endif namespace { + HWND get_dialog_item(HWND wnd, int id) + { + const auto lbl = GetDlgItem(wnd, id); + if (lbl == NULL) + error::raise("GetDlgItem"); + return lbl; + } -HWND get_dialog_item(HWND wnd, int id) -{ - const auto lbl = GetDlgItem(wnd, id); - if (lbl == NULL) - error::raise("GetDlgItem"); - return lbl; -} - -bool is_administrator() -{ - auto token = token::open_for_current_process( - token::permissions::query() | token::permissions::duplicate()); - if (!token::get_linked(token)) - token = token::get_for_identification(token); - - return token::check_belongs(token, sid::builtin_administrators()); -} - -bool is_run_as_administrator() -{ - return token::check_belongs(token::dumb(), sid::builtin_administrators()); -} - -bool is_elevated() -{ - return token::query_elevation(token::open_for_current_process()); -} - -void decorate_elevate_button(HWND wnd, bool decorate) -{ - const auto btn = get_dialog_item(wnd, IDC_BUTTON_ELEVATE); - Button_SetElevationRequiredState(btn, !decorate); -} + bool is_administrator() + { + auto token = token::open_for_current_process( + token::permissions::query() | token::permissions::duplicate()); + if (!token::get_linked(token)) + token = token::get_for_identification(token); -DWORD get_integrity_level() -{ - return token::query_integrity_level(token::open_for_current_process()); -} + return token::check_belongs(token, sid::builtin_administrators()); + } -std::wstring get_integrity_level_as_string() -{ - return token::integrity_level_to_string(get_integrity_level()); -} + bool is_run_as_administrator() + { + return token::check_belongs(token::dumb(), sid::builtin_administrators()); + } -HWND get_label(HWND wnd, int id) -{ - return get_dialog_item(wnd, id); -} + bool is_elevated() + { + return token::query_elevation(token::open_for_current_process()); + } -void set_label(HWND wnd, int id, bool val) -{ - SetWindowTextW(get_label(wnd, id), val ? L"True" : L"False"); -} + void decorate_elevate_button(HWND wnd, bool decorate) + { + const auto btn = get_dialog_item(wnd, IDC_BUTTON_ELEVATE); + Button_SetElevationRequiredState(btn, !decorate); + } -void set_label(HWND wnd, int id, const wchar_t* s) -{ - SetWindowTextW(get_label(wnd, id), s); -} + DWORD get_integrity_level() + { + return token::query_integrity_level(token::open_for_current_process()); + } -void set_label(HWND wnd, int id, const std::wstring& s) -{ - SetWindowTextW(get_label(wnd, id), s.c_str()); -} + std::wstring get_integrity_level_as_string() + { + return token::integrity_level_to_string(get_integrity_level()); + } -BOOL on_init_dialog(HWND wnd, HWND, LPARAM) -{ - try + HWND get_label(HWND wnd, int id) { - set_label(wnd, IDC_ADMINISTRATOR, is_administrator()); + return get_dialog_item(wnd, id); } - catch (const Error& e) + + void set_label(HWND wnd, int id, bool val) { - set_label(wnd, IDC_ADMINISTRATOR, L"N/A"); - error::report(e); + SetWindowTextW(get_label(wnd, id), val ? L"True" : L"False"); } - try + void set_label(HWND wnd, int id, const wchar_t* s) { - set_label(wnd, IDC_RUN_AS_ADMINISTRATOR, is_run_as_administrator()); + SetWindowTextW(get_label(wnd, id), s); } - catch (const Error& e) + + void set_label(HWND wnd, int id, const std::wstring& s) { - set_label(wnd, IDC_RUN_AS_ADMINISTRATOR, L"N/A"); - error::report(e); + SetWindowTextW(get_label(wnd, id), s.c_str()); } - if (os::is_vista_or_later()) + BOOL on_init_dialog(HWND wnd, HWND, LPARAM) { try { - const auto elevated = is_elevated(); - set_label(wnd, IDC_ELEVATED, elevated); - decorate_elevate_button(wnd, elevated); + set_label(wnd, IDC_ADMINISTRATOR, is_administrator()); } catch (const Error& e) { - set_label(wnd, IDC_ELEVATED, L"N/A"); + set_label(wnd, IDC_ADMINISTRATOR, L"N/A"); error::report(e); } try { - set_label(wnd, IDC_INTEGRITY_LEVEL, get_integrity_level_as_string()); + set_label(wnd, IDC_RUN_AS_ADMINISTRATOR, is_run_as_administrator()); } catch (const Error& e) { - set_label(wnd, IDC_INTEGRITY_LEVEL, L"N/A"); + set_label(wnd, IDC_RUN_AS_ADMINISTRATOR, L"N/A"); error::report(e); } + + if (os::is_vista_or_later()) + { + try + { + const auto elevated = is_elevated(); + set_label(wnd, IDC_ELEVATED, elevated); + decorate_elevate_button(wnd, elevated); + } + catch (const Error& e) + { + set_label(wnd, IDC_ELEVATED, L"N/A"); + error::report(e); + } + + try + { + set_label(wnd, IDC_INTEGRITY_LEVEL, get_integrity_level_as_string()); + } + catch (const Error& e) + { + set_label(wnd, IDC_INTEGRITY_LEVEL, L"N/A"); + error::report(e); + } + } + else + { + set_label(wnd, IDC_ELEVATED, L"N/A"); + set_label(wnd, IDC_INTEGRITY_LEVEL, L"N/A"); + } + + return TRUE; } - else + + void report_already_elevated(HWND wnd) { - set_label(wnd, IDC_ELEVATED, L"N/A"); - set_label(wnd, IDC_INTEGRITY_LEVEL, L"N/A"); + MessageBoxW(wnd, + resource::load_string(IDS_ALREADY_ELEVATED_TEXT).c_str(), + resource::load_string(IDS_ALREADY_ELEVATED_CAPTION).c_str(), + MB_OK); } - return TRUE; -} + void on_button_elevate_click(HWND wnd) + { + bool as_admin = false; -void report_already_elevated(HWND wnd) -{ - MessageBoxW(wnd, - resource::load_string(IDS_ALREADY_ELEVATED_TEXT).c_str(), - resource::load_string(IDS_ALREADY_ELEVATED_CAPTION).c_str(), - MB_OK); -} + try + { + as_admin = is_run_as_administrator(); + } + catch (const Error& e) + { + error::report(e); + return; + } -void on_button_elevate_click(HWND wnd) -{ - bool as_admin = false; + if (as_admin) + { + report_already_elevated(wnd); + return; + } - try - { - as_admin = is_run_as_administrator(); - } - catch (const Error& e) - { - error::report(e); - return; - } + try + { + process::runas_self(wnd); + } + catch (const Error& e) + { + if (error::get_code(e) != ERROR_CANCELLED) + error::report(e); + return; + } - if (as_admin) - { - report_already_elevated(wnd); - return; + EndDialog(wnd, 1); } - try + void on_command(HWND wnd, int id, HWND, unsigned int) { - process::runas_self(wnd); - } - catch (const Error& e) - { - if (error::get_code(e) != ERROR_CANCELLED) - error::report(e); - return; + switch (id) + { + case IDC_BUTTON_ELEVATE: + on_button_elevate_click(wnd); + break; + + case IDOK: + case IDCANCEL: + EndDialog(wnd, 0); + break; + } } - EndDialog(wnd, 1); -} - -void on_command(HWND wnd, int id, HWND, unsigned int) -{ - switch (id) + void on_close(HWND wnd) { - case IDC_BUTTON_ELEVATE: - on_button_elevate_click(wnd); - break; - - case IDOK: - case IDCANCEL: - EndDialog(wnd, 0); - break; + EndDialog(wnd, 0); } -} - -void on_close(HWND wnd) -{ - EndDialog(wnd, 0); -} -INT_PTR CALLBACK dialog_main( - HWND wnd, - UINT msg, - WPARAM wParam, - LPARAM lParam) -{ - switch (msg) + INT_PTR CALLBACK dialog_main( + HWND wnd, + UINT msg, + WPARAM wParam, + LPARAM lParam) { - HANDLE_MSG(wnd, WM_INITDIALOG, on_init_dialog); - HANDLE_MSG(wnd, WM_COMMAND, on_command); - HANDLE_MSG(wnd, WM_CLOSE, on_close); + switch (msg) + { + HANDLE_MSG(wnd, WM_INITDIALOG, on_init_dialog); + HANDLE_MSG(wnd, WM_COMMAND, on_command); + HANDLE_MSG(wnd, WM_CLOSE, on_close); - default: - return FALSE; + default: + return FALSE; + } } } -} - int APIENTRY wWinMain( HINSTANCE instance, HINSTANCE, |