5#define WIN32_LEAN_AND_MEAN
15#pragma comment(lib, "userenv.lib")
25 bool hasDoubleQuote = wcschr(s, L
'"') !=
nullptr;
27 bool addDoubleQuotes = wcspbrk(s, L
" \t") !=
nullptr;
72 bool hasDoubleQuote = wcschr(s, L
'"') !=
nullptr;
74 bool addDoubleQuotes = wcspbrk(s, L
" \t") !=
nullptr;
96 for (
int i = 0;
i <= backslashes; ++
i)
139 for (
i = 0;
i < argc && argv[
i]; ++
i)
146 wchar_t *s =
static_cast<wchar_t*
>(malloc(len *
sizeof(
wchar_t)));
151 for (
i = 0;
i < argc && argv[
i]; ++
i)
183 std::memset(&si, 0,
sizeof si);
184 si.cb =
sizeof(STARTUPINFOW);
185 si.lpDesktop =
const_cast<LPWSTR
>(L
"winsta0\\Default");
186 PROCESS_INFORMATION pi;
187 std::memset(&pi, 0,
sizeof pi);
189 if (userToken ==
nullptr)
191 ok = CreateProcessW(exePath,
206 LPVOID environmentBlock =
nullptr;
207 if (!CreateEnvironmentBlock(&environmentBlock, userToken,
TRUE))
209 environmentBlock =
nullptr;
212 ok = CreateProcessAsUserW(userToken,
224 if (environmentBlock)
226 DestroyEnvironmentBlock(environmentBlock);
234 *hProcess = pi.hProcess;
238 CloseHandle(pi.hProcess);
240 CloseHandle(pi.hThread);
244 LPVOID lpMsgBuf =
nullptr;
245 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
246 FORMAT_MESSAGE_FROM_SYSTEM |
247 FORMAT_MESSAGE_IGNORE_INSERTS,
250 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
251 reinterpret_cast<LPWSTR
>(&lpMsgBuf),
254 wprintf(L
"Error restarting: %s\n", lpMsgBuf ? lpMsgBuf : L
"(null)");
256 HeapFree(GetProcessHeap(), 0, lpMsgBuf);
const wchar_t *typedef BOOL
static int ArgStrLen(const wchar_t *s)
Get the length that the string will take and takes into account the additional length if the string n...
static wchar_t * ArgToString(wchar_t *d, const wchar_t *s)
Copy string "s" to string "d", quoting the argument as appropriate and escaping doublequotes along wi...
BOOL WinLaunchChild(const wchar_t *exePath, int argc, wchar_t **argv, HANDLE userToken, HANDLE *hProcess)
Launch a child process with the specified arguments.
wchar_t * MakeCommandLine(int argc, wchar_t **argv)
Creates a command line from a list of arguments.