ID Number: Q45904
5.10 6.00 6.00a
OS/2
Summary:
In Microsoft C versions 5.1, 6.0, and 6.0a, if the C run-time library
function putenv() is used to change the PATH environment variable and
the OS/2 DosExecPgm() API is then called to start an executable that
lies on that path, DosExecPgm() fails to execute the process and gives
a return code indicating that the file could not be found.
To correct this problem, use the C run-time library function spawnlp()
instead of the OS/2 DosExecPgm() API.
Response:
The C run-time startup code copies the environment table into DGROUP.
The putenv() run-time function modifies this copy of the environment.
Other C run-time functions, including spawnlp(), will use this copy of
the environment. So, when you alter your path variable
putenv( "PATH=d:\\stuff" );
and look at it
Path = getenv( "PATH" );
you see that Path does indeed point to the new path. However, then
using an OS/2 API call to check the path variable
DosScanEnv( "PATH", &Path );
reveals that the path is unchanged. This is because the API functions
deal with the original environment table, not the copy in DGROUP.
DosExecPgm() is no exception, meaning that the original PATH
environment variable will be used rather than the altered copy.
To perform a DosExecPgm() of a process that is not on the original
search path but is known to be on another search path, you may want to
use the DosSearchPath() function in conjunction with DosExecPgm(), in
the following manner:
Sample Code
-----------
/* Compile options needed: none
*/
#define INCL_DOS
#include <os2.h>
#include <stdio.h>
void main( void );
void main()
{
RESULTCODES ResCodes;
char Fail[256];
USHORT ret;
char Pgm[256];
DosSearchPath( SEARCH_CUR_DIRECTORY, // Start with cur dir
"d:\\;c:\\;b:\\stuff", // Search this path
"off.exe", // for OFF.EXE.
Pgm, // Put result path here
256 ); // It's 256 bytes long
printf( "Pgm path: %s\n", Pgm );
ret = DosExecPgm( Fail,
256,
0,
NULL,
0,
&ResCodes,
Pgm );
printf( "DosExecPgm says: %u\n", ret );
printf( "Fail: %s\n", Fail );
}
This program uses DosSearchPath() to generate a full path to OFF.EXE.
Each directory in the path passed to DosSearchPath() will be searched.
In this case, the current directory will be searched first because of
the SEARCH_CUR_DIRECTORY flag.
Additional reference words: 5.10 6.00 6.00a 5.1 6.0 6.0a