Re: CreateProcess in a loop
On Mon, 20 Aug 2007 15:11:24 -0700, one-trick-pony
<worldofpain.aamir@gmail.com> wrote:
Howdy,
I have CreateProcess call placed inside a loop that iterates about 5
or less times. Is that legal?
Sure. You can call CreateProcess as many times as you want. It may
eventually fail, but there's no restriction on calling it.
STARTUPINFO si;
PROCESS_INFORMATION pi;
int i = 0;
while ( i < 6 )
{
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
CreateProcess(function arguments);
WaitForSingleObject(some arguments);
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
i++;
}
I am particularly suspicious of first 3 lines of code inside while
loop. Do they need to be run multiple times or can they be placed
before entering loop? Thanks for help.
I'd initialize the STARTUPINFO every time. That way, I don't have to worry
about the 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 like this:
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 member to sizeof(si), and
the remaining members are initialized to zero. This is obscure or
idiomatic, depending on your point of view. :) If you think it's the
former, there's certainly nothing wrong with the ZeroMemory approach.
--
Doug Harrison
Visual C++ MVP