c++ - CreateProcess 成功,但 GetLastError() 返回拒绝访问

标签 c++ access-denied createprocess getlasterror

由于 CreateProcess()GetLastError() 的返回值冲突,我有点困惑。当我以类似于下面的方式使用 CreateProcess() 时,它会成功并似乎完成了所需的任务。然而,GetLastError() 仍然返回 Access is Denied。

如果访问被拒绝,为什么看起来完成了任务。相反,如果 CreateProcess() 成功,为什么 GetLastError() 返回拒绝访问?

或者我对 GetLastError() 的使用不正确?我是否应该只在 CreateProcess() 返回失败值时使用它? (我对以下行为的理由是我认为安全总比后悔好)

    SetLastError(0);
    hello = CreateProcess(_T("C:\\Windows\\System32\\cmd.exe"), 
        _T("C:\\Windows\\System32\\cmd.exe /C ant debug"),
        NULL,NULL,false,0,NULL,
        _T("C:\\My\\Directory"),&siStartupInfo, &piProcessInfo);
    FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_ALLOCATE_BUFFER 
        |FORMAT_MESSAGE_IGNORE_INSERTS,NULL,GetLastError(), 
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 
        (LPTSTR)&errorText, 0, NULL); 
    AfxMessageBox(errorText);

这种行为正常吗?在 CreateProcess() 文档中,它提到在 CreateProcess() 失败时使用 GetLastError(),但没有提到相反的情况。不是批评文档,只是想得到一些澄清。

无论 CreateProcess() 中的第二个参数是否为 NULL,都会发生这种情况。也许它与 cmd.exe 权限有关?如果是这样,CreateProcess() 不应该失败吗?谢谢。

最佳答案

来自GetLastError的文档

The Return Value section of the documentation for each function that sets the last-error code notes the conditions under which the function sets the last-error code. Most functions that set the thread's last-error code set it when they fail. However, some functions also set the last-error code when they succeed. [emphasis mine]

我认为您得到了经验丰富的结果,因为一旦成功,CreateProcess 不会设置 GetLastError 返回的错误值。相反,您对 GetLastError 的调用会返回由之前调用的另一个函数设置的错误

关于c++ - CreateProcess 成功,但 GetLastError() 返回拒绝访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11089825/

相关文章:

c++ - Cocos2dx 2.2.3 架构 arm64::WebPInitDecoderConfigInternal 的 undefined symbol

c++ - 音频采样频率音高

c++ - std::async 有什么问题?

asp.net-mvc - 调试 ASP.NET MVC 时,IIS Express 出现访问被拒绝错误

javascript - XDomainRequest open ("get", url) 在 IE 中给出拒绝访问错误

c++ - 编辑:填写一列而不覆盖C++中已经存在的内容

c++ - CreateProcess的第二个参数应该是什么?

CreateProcess 杀死 PC

c++ - CreateProcess() 函数无法运行。错误无法写入内存

.net - 如何在 WebBrowser Control 中设置当前 document.domain 以避免 "Access is denied"?