21 {
22
23 std::ostringstream oss;
24 oss << "-NoProfile -ExecutionPolicy Bypass -Command \"" << psCommand << "\"";
25 const std::string argStr = oss.str();
26
27
28 auto toWide = [](const std::string& s) -> std::wstring
29 {
30 if (s.empty())
31 return std::wstring();
32 int needed = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), (int)s.size(), nullptr, 0);
33 std::wstring ws(needed, L'\0');
34 MultiByteToWideChar(CP_UTF8, 0, s.c_str(), (int)s.size(), ws.data(), needed);
35 return ws;
36 };
37
38 const std::wstring wExe = L"powershell.exe";
39 const std::wstring wArgs = toWide(argStr);
40
41 SHELLEXECUTEINFOW sei{};
42 sei.cbSize = sizeof(sei);
43 sei.fMask = SEE_MASK_NOCLOSEPROCESS;
44 sei.hwnd = nullptr;
45 sei.lpVerb = L"runas";
46 sei.lpFile = wExe.c_str();
47 sei.lpParameters = wArgs.c_str();
48 sei.nShow = SW_HIDE;
49
50 if (!ShellExecuteExW(&sei))
51 {
52
53 return false;
54 }
55 if (sei.hProcess)
56 {
57 WaitForSingleObject(sei.hProcess, INFINITE);
58 CloseHandle(sei.hProcess);
59 }
60 return true;
61 }