blob: aa57879572ddfb7755dc0e85a935da9382b07fcd (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
// Copyright (c) 2016 Egor Tensin <egor@tensin.name>
// This file is part of the "Privilege check" project.
// For details, see https://github.com/egor-tensin/privilege-check.
// Distributed under the MIT License.
#include "process.hpp"
#include "cmd_line.hpp"
#include "error.hpp"
/* clang-format off */
#include <windows.h>
#include <shellapi.h>
/* clang-format on */
#include <array>
#include <string>
namespace process {
std::wstring get_executable_path() {
static constexpr DWORD max_path = MAX_PATH;
std::array<wchar_t, max_path> buf;
const auto ret = GetModuleFileNameW(NULL, buf.data(), max_path);
if (ret != ERROR_SUCCESS)
error::raise("GetModuleFileNameW");
return buf.data();
}
void runas(const CommandLine& cmd_line, HWND hwnd, int nShow) {
const auto exe_path = cmd_line.has_argv0() ? cmd_line.get_argv0() : get_executable_path();
SHELLEXECUTEINFOW info;
ZeroMemory(&info, sizeof(info));
info.cbSize = sizeof(info);
info.lpVerb = L"runas";
info.lpFile = exe_path.c_str();
const auto args = cmd_line.join_args();
if (!args.empty())
info.lpParameters = args.c_str();
info.hwnd = hwnd;
info.nShow = nShow;
if (!ShellExecuteExW(&info))
error::raise("ShellExecuteExW");
}
void runas_self(HWND hwnd, int nShow) {
runas(CommandLine::query(), hwnd, nShow);
}
} // namespace process
|