You can get a more detailed view by diving into the source code of the
process unit but very basically, what it does is build the parameters needed for the system functions calls it uses to run a command according to the set of properties of TProcess.
To some of those system functions one can pass a pre-built environment, which is basicaly a chunk of memory whose bytes just happen to spell, some way or other, those strings we are used to. If
Environment is not empty then that chunk of memory is built anew and set from the strings in it; otherwise the system usually just makes a copy of the parent's one by itself.
Now, your questions:
so the aprocess.environment.text starts out empty then?
Yes. It's only needed to
change the default, which is to use the parent's (your program's) environment.
what is all the stuff in the tstrings variable?
is it happen to be just 'allocated memory' that still has my prior env stuff in it?
Yes, it's just garbage. And note that it's *not* in the TStrings variable: you're seeing it because the typecast of TStrings to String is causing you to access random zones of memory.
the size (length in the code) stays the same cause it is the size of the allocated memory? (when the .text is empty or just has one line in it)
No. When you ask for the length of a string, it's taken from a field of the internal "header" of the AnsiString (there's where the codepage and other info is stored). Since your "string" points to random memory, that "length" just represents whatever was in that memory chunk, with no relation whatever to any real string length.
then where is the actually environment of the new process stored?
in the sysutils.getenvironmentstring or dos.envstr and the tprocess.environment is in addition or in substitution of these?
The actual environment is stored in a chunk of memory controlled by the OS. What those functions in sysutils, dos, etc. do is read it from there and "massage" it to a form more suitable to be used in a Pascal program.
The environment for a new process is actually created by the system after the fork/exec/whatever call that prepares it. Some of those calls allow you to tell the system: "Hey, I have a pre-built environment you can use", but it's usually not needed
unless you want the child process to use a specific one.
so what is the purpose of the aprocess.envirment
To make the child process use a environment with something other than what the parent's has. In this case, for example, a different "LD_LIBRARY_PATH".
Ultra long post, isn't it? Hope all this helps!