C++/VB - OpenProcess() question

Asked By Jeff McKay
19-Nov-09 08:13 PM
I have an application that uses CreateProcess to start several child
processes.  I want my main program to
periodically check to see if they are still running.  I tried to do this by
calling OpenProcess() using the process id
of the child process; I assumed I would get a null return from the call if
it was no longer running.   What I get
instead is that OpenProcess() always works, even after I have used Task
Manager to kill the child process.  I have
used this technique in another program, that monitors processes created
elsewhere, but not when I spawn the process
myself.  Any suggestions?
GetExitCodeProcess
(1)
HProcessHandle
(1)
CreateProcess
(1)
OpenProcess
(1)
TeamB
(1)
Andcheck
(1)
Reuses
(1)
  Alexander Grigoriev replied to Jeff McKay
19-Nov-09 09:50 PM
You need to check the process handle state instead.

WaitForSingleObject(hProcessHandle, 0) will do.

OpenProcess may work for a dead process if there are still open handles to
it.
  David Lowndes replied to Jeff McKay
20-Nov-09 04:40 AM
Keep the process handles that you start and use GetExitCodeProcess and
check for STILL_ACTIVE.

Dave
  Remy Lebeau replied to Jeff McKay
20-Nov-09 02:36 PM
You do not need to use OpenProcess() for that.  CreateProcess() returns =
both the Process ID and Process Handle of the launched process.  Use =
those values as-is.  You can pass the Process Handle to =
GetExitCodeProcess() or any of the WaitFor...() functions to detect if =
the process is running.  Just be sure to close the Process Handle when =
you are done using it.


Not necessarily.  If you do not close the handle that CreateProcess() =
returns, the child process remains alive in memory, even if it is not =
running anymore.  That way, you can call GetExitCodeProcess() and other =
process-related functions on the child process.  Worse, you are =
re-opening the Process ID, which has no clue if the original child =
process is still attached to that ID anymore.  OpenProcess() opens =
whatever current process is using that ID.  The OS can reuses a =
process's ID after the process is fully terminated.  All the more reason =
to use the Process Handle that CreateProcess() gives you instead.


The killed Process ID was likely reused by the OS before you had a =
chance to call OpenProcess().

--=20
Remy Lebeau (TeamB)
Create New Account
help
CreateProcess in a loop C++ / VB Howdy, I have CreateProcess call placed inside a loop that iterates about 5 or less times. Is that legal while ( i < 6 ) { ZeroMemory( &si, sizeof(si) ); si.cb = sizeof(si); ZeroMemory( π sizeof(pi) ); CreateProcess(function arguments); WaitForSingleObject(some arguments); CloseHandle( pi.hProcess ); CloseHandle( pi.hThread ); i++; } I am particularly times or can they be placed before entering loop? Thanks for help. VC MFC Discussions GetExitCodeProcess (1) IsIPValid (1) SzResource.GetBuffer (1) AfxMessageBox (1) GetLastError (1) SetLastError (1) CNetworkDlg (1) Report Mon, 20 Aug 2007 15:11:24 -0700, one-trick-pony Sure. You can call CreateProcess as many times as you want. It may eventually fail, but there's no restriction first call being OK and the subsequent calls having problems due to potential modification by CreateProcess. It doesn't matter for PROCESS_INFORMATION, which is a purely "out" parameter. You do need to check the return code for CreateProcess (and probably WFSO as well) and proceed appropriately. I would have written the loop something for (int i = 0; i < 6; ++i) { STARTUPINFO si = { DWORD(sizeof(si)) }; PROCESS_INFORMATION pi; if (CreateProcess(. . .)) { WFSO; CloseHandle; CloseHandle; } else deal with error; } The initialization of si sets the first struct
Strange CreateProcess() API failure. C++ / VB I'm seeing strange behavior with CreateProcess() API with VC++ 2005. - - bCP = CreateProcess(NULL, TEXT("cmd.exe / C dir") . . .); Failes with Exception with VC++ 8.0. Works fine with VC++ 6.0. bCP = CreateProcess(TEXT(""), TEXT("cmd.exe / C dir") . . .); bCP CreateProcess(TEXT(""), TEXT("C: / WINDOWS / system32 / cmd.exe / C dir") . . .); Fails with VC++ 8.0; Error code 3 from GetLastError(). CreateProcess(TEXT("C: / WINDOWS / system32 / cmd.exe"), TEXT(" / C dir"), Works fine. No Error. - - Wondering if config. on my system. VC Language Discussions CreateProcessW (1) GetLastError (1) LpApplicationName (1) LpCommandLine (1) CreateProcess (1) Specifies (1) Enforces (1) Literals (1) The second parameter of CreateProcess should be writable (note that it's declared LPTSTR as opposed to LPCTSTR). String literals
Starting an MFC application with CreateProcess Api C++ / VB Hi all, I am trying to start a dialog based MFC application using CreateProcess Api. Also my MFC application takes a single command line argument from CreateProcess Api. MFC Code within the InitInstance BOOL CDispatchApp::InitInstance() { CString cmd; cmd = m_lpCmdLine; CDispatchDlg dlg if i start my application from command prompt. But when i start my application using CreateProcess Api, argument doesnt pass onto the application . I am using the following code for CreateProcess if(!CreateProcess(temp, port, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &startupinfo, &procinfo)) port is defined as char Can any one tell me what is worng in the code kunal VC MFC Discussions CreateProcess (1) MFC (1) CREATE_NEW_CONSOLE (1) CDispatchDlg (1) InitInstance (1) SPort (1) CsCommand (1) CsCmdFmt (1) Drop the first argument of CreateProcess (set to NULL). Pass the second argument as " \ "AppFileName.exe \ " 60000" Thanks for the reply
WinExec() and CreateProcess() C++ / VB Hi all, I have tested WinExec() as follows: WinExec(cmd, SW_HIDE); The behaviour of the program during running is satisfactory. But a test with CreateProcess() failed. Since WinExec() is a macro which calls CreateProcess(), what parameter list should be used with CreateProcess() to get the same effect as WinExec(cmd, SW_HIDE)? Many thanks in advance! kwwang VC Am 12.08.2010 23:06, schrieb wang: WinExec is not really a Macro around CreateProcess() http: / / msdn.microsoft.com / en-us / library / ms682425(v = VS.85).aspx for SW_HIDE usage there is no way to answer this question. You did not say how you call CreateProcess. There is nothing here that could possible enable anyone to answer this question! I have chapter summary): The WinExec function is an obsolete Windows function. You really should use the CreateProcess function instead. However, the CreateProcess function has a number of arguments that are difficult to understand this early in programming