i=WaitForMultipleObjects(hLength,hList,false,INFINITE) //Wait for any process to finish
if( !CreateProcessW( NULL, // No module name (use command line)
tempArg, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si[refList[i]], // Pointer to STARTUPINFO structure
&pi[refList[i]]) // Pointer to PROCESS_INFORMATION structure
){
printf( "CreateProcess failed (%d).\n", GetLastError() );
return 1;
}
etc.
But CreateProcess always fails with code 87 for "Invalid Parameters".
What gets me is if I copy/paste the CreateProcess *in front* of WaitForMultipleObjects (and carefully make sure all the inputs are exactly the same) it works fine.
I've tested this replacing WaitForMultipleObjects with Sleep().
If I sleep for anything less than about 3 seconds, CreateProcess runs fine. If sleep for more, it errors out. I've tried everything I can think of, and it seems to me CreateProcess just doesn't work if your thread has been waiting for too long. Does anyone have an idea what's going on here?
(I'm spawning and monitoring so many processes because this is meant as a monitoring process to run perl scripts written by someone else, and it'd cumbersome to rewrite the perl into C++ and run threads. That would make a lot more sense, but there's various reasons (besides time and laziness) why we'd prefer to leave it in perl.)