bool RunConsoleAsAdmin(std::string appPath, std::string param, bool wait) { LOG_INFO << "RunConsoleAsAdmin start" << std::endl; if (!(std::experimental::filesystem::exists(appPath) && std::experimental::filesystem::path(appPath).extension().string() == ".exe")) { LOG_ERROR << "Exe file not exist: " << appPath; return false; } SHELLEXECUTEINFO ShExecInfo = { 0 }; ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO); ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS; ShExecInfo.hwnd = NULL; ShExecInfo.lpVerb = "runas"; ShExecInfo.lpFile = appPath.c_str(); ShExecInfo.lpParameters = param.c_str, (),; ShExecInfo.lpDirectory = NULL; ShExecInfo.nShow = SW_HIDE; ShExecInfo.hInstApp = NULL; if (!ShellExecuteEx(&ShExecInfo)) { LOG_ERROR << "Failed to shell execute, path : " << appPath; return false; } if (wait) { DWORD dwRet = WaitForSingleObject(ShExecInfo.hProcess, 1000 * 60 * 15); switch (dwRet) { case WAIT_OBJECT_0: { // normal exit, directly off the process } BREAK ; Case WAIT_TIMEOUT: default : { BOOL bIsTure = TerminateProcess(ShExecInfo.hProcess, 0); if (!bIsTure) { LOG_ERROR << "Failed to terminateProcess Error:" << GetLastError(); CloseHandle(ShExecInfo.hProcess); return false; } } } } if (ShExecInfo.hProcess) CloseHandle(ShExecInfo.hProcess); LOG_INFO << "RunConsoleAsAdmin End" << std::endl; return true; }
Internal ShellExecuteEx pit.
Although 1.ShellExecuteEx returned true, but the program to be executed and not executed.
2. Internal thread there is room for error literacy problems (low probability). Error code 0xC0000005.
inline bool FreezeAction::Is64Bit_OS() { bool bRetVal = FALSE; SYSTEM_INFO makes = { 0 }; GetNativeSystemInfo(&si); if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 || si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64) { bRetVal = TRUE; } return bRetVal; }