PRB: Spawned Process Runs Out of Environment Space

Last reviewed: July 17, 1997
Article ID: Q51742
5.10 6.00 6.00ax 7.00 | 1.00 1.50
MS-DOS                | WINDOWS
kbprg kbprb

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++ for Windows, versions 1.0 and 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 "set dummy="), 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.

*/

#include <stdio.h>
#include <process.h>

void main (void);

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

    spawnlp (P_WAIT, "child.exe", "child", NULL);

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

/*
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.

*/

#include <stdio.h>
#include <process.h>
#include <conio.h>
#include <errno.h>

void main (void);

void main (void)
{
    printf ("At child process...\n");
    system ("batch.bat");
    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 "Out of Environment Space" 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 reference words: 5.10 6.00 6.00a 6.00ax 7.00 1.00 1.50
KBCategory: kbprg kbprb
KBSubcategory: CRTIss
Keywords : kb16bitonly


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 17, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.