shellexecute的使用和X64判断

Posted _拥你一生,如沐星辰_

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}

 




以上是关于shellexecute的使用和X64判断的主要内容,如果未能解决你的问题,请参考以下文章

作业对象上的 CloseHandle() 但不终止由 ShellExecute 创建的进程

尝试打开大于 10MB 的 mdb 文件时,ShellExecute() 返回错误代码 5

判断指定进程是否为x64的方法(在ntdll判断某个x64函数是否存在)

Win7 x64 Vad遍历模块

delphi中shellExecute的使用

从 ShellExecute 获取 PID