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.
If access is denied, why is does it appear to complete the task. In contrast, if CreateProcess()
succeeds, why is GetLastError()
returning access denied?
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);
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.
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.
From the documentation of 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]
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
GetLastError
is only meaningful when the documentation states it is meaningful.
GetLastError
returns the Win32 thread last error set with SetLastError
. Since functions are only supposed to set or clear the last error when it is documented that they do so, if that hasn't happened, the last error may well contain an earlier error which is not relevant.
It is documented that CreateProcess
will set the last error if it fails, not if it succeeds. So you should ignore it.
来源:https://stackoverflow.com/questions/11089825/createprocess-succeeds-but-getlasterror-returns-access-denied