This documentation is archived and is not being maintained.

_searchenv, _wsearchenv

Search for a file using environment paths.

void _searchenv(
   const char *filename,
   const char *varname,
   char *pathname 
void _wsearchenv(
   const wchar_t *filename,
   const wchar_t *varname,
   wchar_t *pathname 


Name of file to search for.
Environment to search.
Buffer to store complete path.


The _searchenv routine searches for the target file in the specified domain. The varname variable can be any environment or user-defined variable that specifies a list of directory paths, such as PATH, LIB, and INCLUDE. _searchenv is case sensitive, so varname should match the case of the environment variable.

The routine searches first for the file in the current working directory. If it does not find the file, it looks next through the directories specified by the environment variable. If the target file is in one of those directories, the newly created path is copied into pathname. If the filename file is not found, pathname contains an empty, null-terminated string.

The pathname buffer should be at least _MAX_PATH characters long in order to accommodate the full length of the constructed path name. Otherwise, _searchenv might overrun the pathname buffer resulting in unexpected behavior.

_wsearchenv is a wide-character version of _searchenv; the arguments to _wsearchenv are wide-character strings. _wsearchenv and _searchenv behave identically otherwise.

Generic-Text Routine Mappings

TCHAR.H routine _UNICODE & _MBCS not defined _MBCS defined _UNICODE defined
_tsearchenv _searchenv _searchenv _wsearchenv


Routine Required header Compatibility
_searchenv <stdlib.h> Win 98, Win Me, Win NT, Win 2000, Win XP
_wsearchenv <stdlib.h> or <wchar.h> Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the Introduction.


All versions of the C run-time libraries.


// crt_searchenv.c
/* This program searches for a file in
 * a directory specified by an environment variable.

#include <stdlib.h>
#include <stdio.h>

int main( void )
   char pathbuffer[_MAX_PATH];
   char searchfile[] = "CL.EXE";
   char envvar[] = "PATH";

   /* Search for file in PATH environment variable: */
   _searchenv( searchfile, envvar, pathbuffer );
   if( *pathbuffer != '\0' )
      printf( "Path for %s:\n%s\n", searchfile, pathbuffer );
      printf( "%s not found\n", searchfile );

Sample Output

Path for CL.EXE:
C:\Program Files\Microsoft Visual Studio .NET 2003\VC7\BIN\CL.EXE

See Also

Directory Control Routines | getenv | _putenv | Run-Time Routines and .NET Framework Equivalents