shellexecute的使用和X64判断

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:
        {
            //正常退出,直接关闭进程
        }
        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;
}
ShellExecuteEx内部有坑。
1.ShellExecuteEx虽然返回了true,但是要执行的程序并没有被执行。
2.内部线程空间存在读写错误问题(概率低)。错误码0xC0000005。


inline bool FreezeAction::Is64Bit_OS()
{
    bool bRetVal = FALSE;
    SYSTEM_INFO si = { 0 };
    GetNativeSystemInfo(&si);
    if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ||
        si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64)
        {
          bRetVal = TRUE;
        }
    return bRetVal;
}

 




posted @ 2019-11-24 16:32  gd_沐辰  阅读(633)  评论(0编辑  收藏  举报