由于 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/