runas 为是否以管理员身份运行。
filePath 为运行的EXE(可执行文件)路径。
arguments 为运行使用的CLI命令行接口参数。
returnCode 为运行进程退出返回的错误代码,通常进程返回:0。
bool Win32Native::Execute(bool runas, const char* filePath, const char* argumentText, int* returnCode) noexcept
{
if (NULL == filePath || *filePath == '\x0')
{
return false;
}
if (NULL == argumentText)
{
argumentText = "";
}
if (NULL != returnCode)
{
*returnCode = INFINITE;
}
SHELLEXECUTEINFOA sei;
memset(&sei, 0, sizeof(sei));
sei.cbSize = sizeof(sei);
sei.fMask = SEE_MASK_NOCLOSEPROCESS;
sei.nShow = SW_HIDE;
sei.lpVerb = runas ? "runas" : "open";
sei.lpFile = filePath;
sei.lpParameters = argumentText;
if (!ShellExecuteExA(&sei))
{
return false;
}
if (NULL != returnCode)
{
WaitForSingleObject(sei.hProcess, INFINITE);
if (!GetExitCodeProcess(sei.hProcess, reinterpret_cast<DWORD*>(returnCode)))
{
*returnCode = INFINITE;
}
}
CloseHandle(sei.hProcess);
return true;
}