aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/process.cpp
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2016-10-14 00:00:22 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2016-10-14 00:00:22 +0300
commit69905917cf2c46fa0093df7323e5326450d1246b (patch)
tree2ec12ee6f588f7bcefab713c976ed64aa1e729ec /src/process.cpp
parentrename the project (diff)
downloadprivilege-check-69905917cf2c46fa0093df7323e5326450d1246b.tar.gz
privilege-check-69905917cf2c46fa0093df7323e5326450d1246b.zip
move code from headers to .cpp files
Diffstat (limited to 'src/process.cpp')
-rw-r--r--src/process.cpp64
1 files changed, 64 insertions, 0 deletions
diff --git a/src/process.cpp b/src/process.cpp
new file mode 100644
index 0000000..94765bc
--- /dev/null
+++ b/src/process.cpp
@@ -0,0 +1,64 @@
+// Copyright (c) 2016 Egor Tensin <Egor.Tensin@gmail.com>
+// 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 "cmd_line.hpp"
+#include "error.hpp"
+#include "process.hpp"
+
+#include <Windows.h>
+#include <shellapi.h>
+
+#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 (GetLastError() != ERROR_SUCCESS)
+ error::raise("GetModuleFileNameW");
+
+ return buf.data();
+ }
+
+ void runas(
+ const CommandLine& cmd_line,
+ HWND hwnd,
+ int nShow)
+ {
+ static constexpr auto sep = L' ';
+
+ 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);
+ }
+}