dwExitCode returns exit code 87 (ERROR_INVALID_PARAMETER) from ::GetExitCodeProcess after creating the process (launching the .exe) using ::CreateProc

  • Thread starter Thread starter bhavya internship
  • Start date Start date
B

bhavya internship

Guest
Hello all,


I am using the below code to create the process and to wait the process to exit.

First I am calling _CreateProcessAndWaitEx function and this function internally calling ::CreateProcessW to create the process (launching the .exe).
Here the paths are valid. So it is creating the process.

After that calling MsgWaitForObject and this function internally calling ::GetExitCodeProcess. But here dwExitCode returns exit code 87 (i.e, ERROR_INVALID_PARAMETER)

Could anyone please help me why here I am getting exit code 87 (ERROR_INVALID_PARAMETER)?

Below is the code snippet:

_CreateProcessAndWaitEx(LPCWSTR pszExePath, LPWSTR pszCmdLine, LPCWSTR pszCurrentDirectory, HANDLE hUserToken, BOOL bWait)
{

Below is the values of the above parameters:

pszExePath = C:\Program Files\Common Files\Installer\emrinst.exe
pszCmdLine= C:\Test\Emrinst\src\pkg\2000.inf /install
pszCurrentDirectory = C:\Test\src\TestPath
hUserToken is NULL
AND bWait IS TRUE


LPWSTR pszTemp;
WCHAR szWorkingDir[MAX_PATH + 1];
STARTUPINFO si;
PROCESS_INFORMATION pi;
WCHAR szExe[MAX_PATH * 4];

DWORD dwExitCode = 0;

wcscpy_s(szWorkingDir, MAX_PATH + 1, pszExePath);
pszTemp = wcsrchr(szWorkingDir, L'\\');
if (pszTemp)
*pszTemp = L'\0';
else
szWorkingDir[0] = L'\0';

wsprintf(szExe, L"\"%s\" %s", pszExePath, pszCmdLine);

::ZeroMemory(&si, sizeof(si));
::ZeroMemory(&pi, sizeof(pi));

si.cb = sizeof(si);
si.lpDesktop = L"winsta0\\default";


if (NULL != hUserToken)
{
dwExitCode = ::CreateProcessAsUser(hUserToken, 0, szExe,
0, 0, FALSE, NORMAL_PRIORITY_CLASS, 0,
pszCurrentDirectory, &si, &pi);
}
else
{

// control is comming to this loop and creating the process

dwExitCode = ::CreateProcessW(pszExePath, pszCmdLine,
0, 0, FALSE, NORMAL_PRIORITY_CLASS, 0,
pszCurrentDirectory, &si, &pi);
}
if (0 == dwExitCode)
{
dwExitCode = ::GetLastError();
return dwExitCode;
}
dwExitCode = 0;

if (pi.hThread)
::CloseHandle(pi.hThread);

if (pi.hProcess)
{

// Then it is comming to this loop and calling _MsgWaitForObject function


if (bWait)
dwExitCode = _MsgWaitForObject(&(pi.hProcess));
::CloseHandle(pi.hProcess);
}

return dwExitCode;
}

_MsgWaitForObject(HANDLE *phObjects)
{

MSG Msg;
BOOL bResult = TRUE;

DWORD dwExitCode = ERROR_SUCCESS;

while (bResult)
{
DWORD dwResult = ::MsgWaitForMultipleObjects(1, phObjects, FALSE, INFINITE, QS_ALLINPUT);

if (WAIT_OBJECT_0 == dwResult)
{
// Here dwExitCode returns exit code 87 (i.e, ERROR_INVALID_PARAMETER)

::GetExitCodeProcess(phObjects[0], &dwExitCode);
bResult = FALSE;
break;
}
else
{
if ((WAIT_OBJECT_0 + 1) == dwResult)
{
while (::PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE))
{
::TranslateMessage(&Msg);
::DispatchMessage(&Msg);
}
}
else
{
dwExitCode = GetLastError();
break;
}
}
}

return dwExitCode;
}

Thanks in advance

Continue reading...
 
Back
Top