1

I have my own application for building a huge set of C++ projects. On Windows I need to set the environment variables via vcvarsall.bat, but how can I execute the batch from a C++ console app in the context of the current process, so that the environment the batch creates is kept for my app? Both _spawnvp and ShellExecuteEx create a dedicated process.

Edit: I of course know how to execute a process or batch, but I want to know to preserve the environment the batch creates.

Vojtěch Melda Meluzín
  • 1,117
  • 3
  • 11
  • 22
  • execute *bat* in context of current process - only if you will be parse and execute *bat* yourself. you faster need use `CreateProcess` for create *cmd*, which execute *bat*. - this api let you set any Environment variables – RbMm Mar 21 '18 at 19:13
  • Possible duplicate of [how can we use a batch file in c++?](https://stackoverflow.com/questions/1478171/how-can-we-use-a-batch-file-in-c) – Smit Ycyken Mar 21 '18 at 19:15
  • 2
    Use another batch file that first runs `call vcvarsall.bat` and then runs your application. – Jonathan Potter Mar 21 '18 at 20:08
  • @SmitYcyken - No, that is not a duplicate. The OP already knows how to spawn a process that runs a batch script from C++ code. But the OP wants to know how to execute the batch script from C++ and preserve environment variables that the script creates so the C++ program can use them. (I don't think it is possible in the way the OP envisions it) – dbenham Mar 21 '18 at 20:10
  • Exactly! @JonathanPotter of course, but that's not exactly easy to do in my case. I'd like to do that from the app itself and continue working. That's the question. – Vojtěch Melda Meluzín Mar 21 '18 at 21:41
  • You can't. You have a misconception here. You should realize that a "Batch file" is _never_ executed by itself. The program that "execute" the Batch file is `cmd.exe`, so your question really is: How execute `cmd.exe` program and preserve its environment after it ends? The answer should be obvious... – Aacini Mar 22 '18 at 01:41
  • Well, could you help me with the "obvious" part? – Vojtěch Melda Meluzín Mar 22 '18 at 08:40
  • When an .exe program ends, _all_ the resources allocated for it are closed and released, like any opened file or any allocated memory block. This, of course, include the memory block allocated for the environment variables that belongs to such a program. This is the way that standard .exe programs (like `cmd.exe` or anyone of your C++ projects) works in Windows OS and there is no way to modify it... – Aacini Mar 22 '18 at 13:51
  • I suggest you to review the description of [ExitProcess Win32 API function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms682658(v=vs.85).aspx) that indicate: _Exiting a process causes the following: 6. All of the object handles opened by the process are closed_. – Aacini Mar 22 '18 at 13:52

1 Answers1

1

There is no way to automatically apply a child process's environment to a parent process. You could read the child environment manually by jumping through some hoops. Check for environment variable in another process?

There are arguably simpler approaches though. Here are a couple.

  • Programmatically create a batch file that calls vcvarsall.bat and then invokes the compilation.
  • Programmatically create a batch file that calls vcvarsall.bat and then writes the environment to a file (set > env.txt). Then read and parse that text file.
Michael Gunter
  • 12,528
  • 1
  • 24
  • 58
  • Thank you! The idea with the text file is actually a good one! Didn't think about that. It's sad that such a basic thing isn't natively possible, but hey, if it works, then as a workaround it is fine by me! – Vojtěch Melda Meluzín Mar 22 '18 at 17:56