The process working directory is a single value for the process. You are expecting each task to have their own private copy but that is simply not how the working directory operates. In more technical terms there is a data race on the shared process wide working directory. Because of this race when the child processes are created they inherit the working directory of the parent but because of the race some children are inheriting the working directory intended for different children. This is one of the classic mistakes made with parallel programming.
Deal with this by avoiding use of the parent process working directory. Do not modify it at all. Instead pass the working directory to each child process as you create them. This can be done quite simply using CreateProcess
or ShellExecuteEx
. Your WinExec32AndWait
function may need to be modified to accept a working directory argument.
This solves the problem by ensuring that you create a separate copy of the working directory for each distinct child process.