Well, here, in fact, I'm trying to do this. I have a process P1. This process should invoke the Visual Studio command-line compiler cl.exein a separate process P2(obviously). However, since everyone who has ever used the Visual Studio command-line compiler knows, you cannot just invoke cl.exeand expect a good experience. Instead, you should first run the script package %VSXXXCOMNTOOLS%\vsvars32.bat(where XXXis the version number of Visual Studio). This script sets several key environment variables used by the compiler (for example, what to use as an inclusion path). Using the script package, this is insanely easy to do:
call "%VS110COMNTOOLS%\vsvars32.bat"
...
cl Foo.cpp Bar.cpp ...
since just calling the batch file from the script package is executed in the same process (and therefore the added environment variables are constant). This is what I did before I realized that I needed more flexibility and decided to port my script to C ++, which so far has worked wonderfully. That is, until I got to the point that I need to implement the actual compilation.
So this is the problem that I'm ultimately trying to solve. The best idea that I came up with is to call cmd.exe /c "%VS110COMNTOOLS%\vsvars32.bat"into a separate process P3using CreateProcess, wait for the process to complete, and then extract the changed environment variables from this child process. That is, it P1creates P3and waits for its completion. P1then sets the environment variables P3as their own. P1then creates P2with these environment variables set. Thus, the code looks something like this (minus all error checks):
...
CreateProcess(TEXT("cmd"), TEXT("/c \"%VS110COMNTOOLS%\vsvars32.bat\""), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
...
CreateProcess(TEXT("cl"), TEXT("..."), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
. , , , , . NET, ProcessExplorer, , , , . - , . , MSDN. Merge Environment - yes. - , ? , .
, , , (1) script, vsvars32.bat, cl.exe , (2) cmd cl vsvars32.bat ( 1, ... , ) (3) , . - , .
. , 99% , , , , .