本文介绍了CreateProcess成功,但是GetLastError()返回拒绝访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于CreateProcess()GetLastError()的返回值冲突,我有点困惑.当我以类似于下面的方式使用CreateProcess()时,它会成功执行并似乎已完成其所需的任务.但是,GetLastError()仍然返回访问被拒绝".

I'm having a little confusion due to conflicting return values from CreateProcess() and GetLastError(). When I use CreateProcess() in a manner similar to below, it succeeds and appears to accomplish its required tasks. Yet, GetLastError() still returns Access is Denied.

如果访问被拒绝,为什么它似乎可以完成任务.相反,如果CreateProcess()成功,为什么GetLastError()返回访问被拒绝?

If access is denied, why is does it appear to complete the task. In contrast, if CreateProcess() succeeds, why is GetLastError() returning access denied?

还是我对GetLastError()的使用不正确?我只能在CreateProcess()返回失败值时使用它吗? (我对以下行为的辩解是,我认为安全胜于后悔)

Or is my use of GetLastError() incorrect? Am I only supposed to use it when CreateProcess() returns a failed value? (My justification for the below behavior was that I figured it was better to be safe than sorry)

    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()的情况,但没有提及相反的情况.不批评文档,只是想澄清一下.

Is this type of behavior normal? In the CreateProcess() documentation, it mentions using GetLastError() when CreateProcess() fails, but it does not mention the inverse. Not criticizing the documentation, would just like some clarification.

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

This occurs whether the second parameter in CreateProcess() is NULL or not. Maybe it has to do with cmd.exe permissions? If that's the case, shouldn't CreateProcess() fail? Thank you.

推荐答案

来自GetLastError

我认为您获得了有经验的结果,因为成功完成CreateProcess并没有设置错误值GetLastError返回.相反,您对GetLastError的调用会返回由另一个先前调用的函数设置的错误

I think you get the experienced outcome because, uppon success, CreateProcess does not set the error value GetLastError returns. Instead your call to GetLastError returns an error set by another function called earlier

这篇关于CreateProcess成功,但是GetLastError()返回拒绝访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 01:14