Microsoft KB Archive/51742

{|
 * width="100%"|

PRB: Spawned Process Runs Out of Environment Space

 * }

Q51742

-

The information in this article applies to:


 * The C Run-Time (CRT), included with:
 * Microsoft C for MS-DOS, versions 5.1, 6.0, 6.0a, and 6.0ax
 * Microsoft C/C++ for MS-DOS, version 7.0
 * Microsoft Visual C++, versions 1.0, 1.5

-

SYMPTOMS
With a Microsoft C created MS-DOS application, using spawn to create a child process which then attempts to increase the environment space of the child (by adding a new environment variable or expanding an existing environment variable) results in the following error:

Out of Environment Space

This problem does NOT happen if the parent and child are built as native OS/2 or Win32-based applications.

RESOLUTION
To work around this problem, set up a dummy environment variable that is large enough to hold the new environment variable you plan to use in the spawned process. When the child process is called, you can then set the dummy variable to null (with a &quot;set dummy=&quot;), then you can set your processes environment variable. Please note that this will change the dummy environment variable for the child only, not the parent process.

MORE INFORMATION
The following program(s) illustrates this behavior:

Sample Code
/* parent.c

Compile options needed: none

This will call the child process (child) with the spawnlp function.


 * 1) include 
 * 2) include 

void main (void);

void main (void) {   printf (&quot;In the parent process\n&quot;);

spawnlp (P_WAIT, &quot;child.exe&quot;, &quot;child&quot;, NULL);

printf (&quot;\nAnd back to the parent process.\n&quot;); }

/* child.c

Compile options needed: none

Called by parent.c, uses the system function to call a batch file (BATCH.BAT) to attempt to set a new environment variable.


 * 1) include 
 * 2) include 
 * 3) include 
 * 4) include 

void main (void);

void main (void) {   printf (&quot;At child process...\n&quot;); system (&quot;batch.bat&quot;); getch ; }

/*-*/ BATCH.BAT The batch file, which is called by the child process (CHILD.EXE). It just shows the environment variables, attempts to set another environment variable, then shows the environment variables one more time. /*-*/

set set blah=thisisatestonlyatestsoitdoesnotreallymatter set

/*-*/  Parent will spawn child, which in turn spawns (through system) BATCH.BAT. The idea is to show that when BATCH.BAT is called, an &quot;Out of Environment Space&quot; error will be given. Yet, if BATCH.BAT is run from MS-DOS, no such error is issued.

Under MS-DOS, the child receives only enough space to hold the current environment variables.

Additional query words: 5.10 6.00 6.00a 6.00ax 7.00 1.00 1.50

Keywords : kb16bitonly kbprb

Issue type : kbprb

Technology : kbVCsearch kbAudDeveloper kbCRT