码迷,mamicode.com
首页 > 系统相关 > 详细

shellexecute的使用和X64判断

时间:2019-11-24 17:24:00      阅读:67      评论:0      收藏:0      [点我收藏+]

标签:错误   param   概率   turn   div   cti   nbsp   bsp   arch   

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;
}

 




shellexecute的使用和X64判断

标签:错误   param   概率   turn   div   cti   nbsp   bsp   arch   

原文地址:https://www.cnblogs.com/gd-luojialin/p/11922964.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!