_exec、_wexec 函数_exec, _wexec Functions

此系列中的每个函数都会加载并执行新进程:Each function in this family loads and executes a new process:

_execl、_wexecl_execl, _wexecl _execv、_wexecv_execv, _wexecv
_execle、_wexecle_execle, _wexecle _execve、_wexecve_execve, _wexecve
_execlp、_wexeclp_execlp, _wexeclp _execvp、_wexecvp_execvp, _wexecvp
_execlpe、_wexeclpe_execlpe, _wexeclpe _execvpe、_wexecvpe_execvpe, _wexecvpe

函数名称末尾的字母可确定该变量。The letter at the end of the function name determines the variation.

_exec 函数后缀_exec function suffix 描述Description
e 将指向环境设置的指针数组 envp 传递给新进程。envp, array of pointers to environment settings, is passed to the new process.
l 将命令行参数单独传递给 _exec 函数。Command-line arguments are passed individually to _exec function. 通常在提前知道新进程的参数数量时使用。Typically used when the number of parameters to the new process is known in advance.
p PATH 环境变量用于查找要执行的文件。PATH environment variable is used to find the file to execute.
v 将指向命令行参数的指针数组 argv 传递给 _execargv, array of pointers to command-line arguments, is passed to _exec. 通常在新进程的参数数量可变时使用。Typically used when the number of parameters to the new process is variable.

备注Remarks

每个 _exec 函数都会加载并执行新进程。Each _exec function loads and executes a new process. 所有 _exec 函数均使用同一个操作系统函数 (CreateProcess)。All _exec functions use the same operating-system function (CreateProcess). _exec 函数将适当自动处理多字节字符字符串参数,从而根据当前正在使用的多字节代码页识别多字节字符序列。The _exec functions automatically handle multibyte-character string arguments as appropriate, recognizing multibyte-character sequences according to the multibyte code page currently in use. _wexec 函数是 _exec 函数的宽字符版本。The _wexec functions are wide-character versions of the _exec functions. _wexec 函数的行为与其对应系列 _exec 完全相同,差别在于这些函数不处理多字节字符字符串。The _wexec functions behave identically to their _exec family counterparts except that they do not handle multibyte-character strings.

一般文本例程映射Generic-Text Routine Mappings

Tchar.h 例程Tchar.h routine 未定义 _UNICODE 和 _MBCS_UNICODE and _MBCS not defined 已定义 _MBCS_MBCS defined 已定义 _UNICODE_UNICODE defined
_texecl _execl _execl _wexecl
_texecle _execle _execle _wexecle
_texeclp _execlp _execlp _wexeclp
_texeclpe _execlpe _execlpe _wexeclpe
_texecv _execv _execv _wexecv
_texecve _execve _execve _wexecve
_texecvp _execvp _execvp _wexecvp
_texecvpe _execvpe _execvpe _wexecvpe

cmdname 参数指定要作为新进程执行的文件。The cmdname parameter specifies the file to be executed as the new process. 它可指定完整路径(来自根)、部分路径(来自当前工作目录)或文件名。It can specify a full path (from the root), a partial path (from the current working directory), or a file name. 如果 cmdname 不具有文件扩展名或不以句点 (.) 结尾,则 _exec 函数将搜索已命名的文件。If cmdname does not have a file name extension or does not end with a period (.), the _exec function searches for the named file. 如果搜索未成功,则它将尝试使用与 .com 文件扩展名相同的基名称,然后依次使用与 .exe、.bat 和 .cmd 文件扩展名相同的基名称。If the search is unsuccessful, it tries the same base name with the .com file name extension and then with the .exe, .bat, and .cmd file name extensions. 如果 cmdname 具有文件扩展名,则仅在搜索中使用该扩展名。If cmdname has a file name extension, only that extension is used in the search. 如果 cmdname 以句点结尾,则 _exec 函数将搜索不具有文件扩展名的 cmdnameIf cmdname ends with a period, the _exec function searches for cmdname with no file name extension. _execlp_execlpe_execvp_execvpe 将在由 cmdname 环境变量指定的目录中(使用相同过程)搜索 PATH_execlp, _execlpe, _execvp, and _execvpe search for cmdname (using the same procedures) in the directories specified by the PATH environment variable. 如果 cmdname 包含一个驱动器说明符或任何斜杠(即如果它是相对路径),则 _exec 将仅调用对指定文件的搜索;不会搜索该路径。If cmdname contains a drive specifier or any slashes (that is, if it is a relative path), the _exec call searches only for the specified file; the path is not searched.

通过将一个或多个指向字符字符串的指针给定为 _exec 调用中的参数,将参数传递给新进程。Parameters are passed to the new process by giving one or more pointers to character strings as parameters in the _exec call. 这些字符字符串将构成该新进程的参数列表。These character strings form the parameter list for the new process. 继承的环境设置和构成新进程参数列表的字符串的组合长度不能超过 32 KB。The combined length of the inherited environment settings and the strings forming the parameter list for the new process must not exceed 32 kilobytes. 计数中不包括每个字符串的以 null 结尾的字符 ('\0'),但是会计入空格字符(自动插入以分隔参数)。The terminating null character ('\0') for each string is not included in the count, but space characters (inserted automatically to separate the parameters) are counted.

备注

嵌入字符串中的空格可能导致意外行为;例如,将字符串 _exec 传递给 "hi there" 会导致新进程获得两个参数:"hi""there"Spaces embedded in strings may cause unexpected behavior; for example, passing _exec the string "hi there" will result in the new process getting two arguments, "hi" and "there". 如果想要让新进程打开名为“hi there”的文件,则该进程将失败。If the intent was to have the new process open a file named "hi there", the process would fail. 你可以通过引用字符串 "\"hi there\"" 来避免此问题。You can avoid this by quoting the string: "\"hi there\"".

重要

如果没有显式地检查其内容,请不要将用户输入传递给 _execDo not pass user input to _exec without explicitly checking its content. _exec 将导致调用 CreateProcess,因此请牢记,未限定的路径名称可能会导致潜在的安全漏洞。_exec will result in a call to CreateProcess so keep in mind that unqualified path names could lead to potential security vulnerabilities.

_exec 函数将验证其参数。The _exec functions validate their parameters. 如果预期参数是空指针、空字符串或省略了预期参数,则 _exec 函数调用的参数处理程序无效,如参数验证中所述。If expected parameters are null pointers, empty strings, or omitted, the _exec functions invoke the invalid parameter handler as described in Parameter Validation. 如果允许执行继续,则这些功能将 errno 设置为 EINVAL 并返回 -1。If execution is allowed to continue, these functions set errno to EINVAL and return -1. 不执行任何新进程。No new process is executed.

可将参数指针作为单独参数进行传递(在 _execl_execle_execlp_execlpe 中),或作为指针数组进行传递(在 _execv_execve_execvp_execvpe 中)。The argument pointers can be passed as separate parameters (in _execl, _execle, _execlp, and _execlpe) or as an array of pointers (in _execv, _execve, _execvp, and _execvpe). 必须将至少一个参数 arg0 传递给新进程;此参数是该新进程的 argv[0]。At least one parameter, arg0, must be passed to the new process; this parameter is argv[0] of the new process. 通常,此参数是 cmdname 的副本。Usually, this parameter is a copy of cmdname. (不同的值不会产生错误。)(A different value does not produce an error.)

通常在提前知道参数数量时使用 _execl_execle_execlp_execlpe 调用。The _execl, _execle, _execlp, and _execlpe calls are typically used when the number of parameters is known in advance. 参数 arg0 通常是一个指向 cmdname 的指针。The parameter arg0 is usually a pointer to cmdname. 参数 arg1argn 指向构成新参数列表的字符字符串。The parameters arg1 through argn point to the character strings forming the new parameter list. 空指针必须在 argn 后才能标记参数列表的末尾。A null pointer must follow argn to mark the end of the parameter list.

_execv_execve_execvp_execvpe 调用在新进程的参数数量可变时很有用。The _execv, _execve, _execvp, and _execvpe calls are useful when the number of parameters to the new process is variable. 将指向参数的指针作为数组 argv 进行传递。Pointers to the parameters are passed as an array, argv. 参数 argv[0] 通常是一个指向 cmdname 的指针。The parameter argv[0] is usually a pointer to cmdname. 参数 argv[1] 到 argv[n] 指向构成新参数列表的字符字符串。The parameters argv[1] through argv[n] point to the character strings forming the new parameter list. 参数 argv[n+1] 必须是一个 NULL 指针,用以标记参数列表的末尾。The parameter argv[n+1] must be a NULL pointer to mark the end of the parameter list.

在进行 _exec 调用时打开的文件在新进程中仍然保持打开状态。Files that are open when an _exec call is made remain open in the new process. _execl_execlp_execv_execvp 调用中,新进程将继承调用进程的环境。In _execl, _execlp, _execv, and _execvp calls, the new process inherits the environment of the calling process. 通过 _execle 参数传递环境设置的列表,_execlpe_execve_execvpeenvp 调用可更改新进程的环境。_execle, _execlpe, _execve, and _execvpe calls alter the environment for the new process by passing a list of environment settings through the envp parameter. envp 是一个字符指针数组,其中每个元素(最后一个元素除外)均指向定义环境变量的以 null 结尾的字符串。envp is an array of character pointers, each element of which (except for the final element) points to a null-terminated string defining an environment variable. 此类字符串通常具有 NAME=value 格式,其中 NAME 是环境变量的名称,value 是为该变量设置的字符串值。Such a string usually has the form NAME=value where NAME is the name of an environment variable and value is the string value to which that variable is set. (请注意,value 并未括在双引号中。)envp 数组的最后一个元素应该是 NULL(Note that value is not enclosed in double quotation marks.) The final element of the envp array should be NULL. envp 本身是 NULL 时,新进程将继承调用进程的环境设置。When envp itself is NULL, the new process inherits the environment settings of the calling process.

使用一个 _exec 函数执行的程序将始终加载到内存中,如同已将该程序的 .exe 文件头中的最大分配字段设置为默认值 0xFFFFH。A program executed with one of the _exec functions is always loaded into memory as if the maximum allocation field in the program's .exe file header were set to the default value of 0xFFFFH.

_exec 调用不会保留打开的文件的转换模式。The _exec calls do not preserve the translation modes of open files. 如果新进程必须使用继承自调用进程的文件,请使用 _setmode 例程将这些文件的转换模式设置为所需模式。If the new process must use files inherited from the calling process, use the _setmode routine to set the translation mode of these files to the desired mode. fflush 函数调用之前,你必须(使用 _flushall_exec)显式刷新或关闭任何流。You must explicitly flush (using fflush or _flushall) or close any stream before the _exec function call. 信号设置不会保留在通过对 _exec 例程的调用而创建的新进程中。Signal settings are not preserved in new processes that are created by calls to _exec routines. 会在新进程中将信号设置重置为默认设置。The signal settings are reset to the default in the new process.

示例Example

// crt_args.c  
// Illustrates the following variables used for accessing  
// command-line arguments and environment variables:  
// argc  argv  envp  
// This program will be executed by crt_exec which follows.  

#include <stdio.h>  

int main( int argc,  // Number of strings in array argv  
 char *argv[],       // Array of command-line argument strings  
 char **envp )       // Array of environment variable strings  
{  
    int count;  

    // Display each command-line argument.  
    printf( "\nCommand-line arguments:\n" );  
    for( count = 0; count < argc; count++ )  
        printf( "  argv[%d]   %s\n", count, argv[count] );  

    // Display each environment variable.   
    printf( "\nEnvironment variables:\n" );  
    while( *envp != NULL )  
        printf( "  %s\n", *(envp++) );  

    return;  
}  

运行以下程序以执行 Crt_args.exe:Run the following program to execute Crt_args.exe:

// crt_exec.c  
// Illustrates the different versions of exec, including  
//      _execl          _execle          _execlp          _execlpe  
//      _execv          _execve          _execvp          _execvpe  
//  
// Although CRT_EXEC.C can exec any program, you can verify how   
// different versions handle arguments and environment by   
// compiling and specifying the sample program CRT_ARGS.C. See   
// "_spawn, _wspawn Functions" for examples of the similar spawn   
// functions.  

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

char *my_env[] =     // Environment for exec?e  
{  
   "THIS=environment will be",  
   "PASSED=to new process by",  
   "the EXEC=functions",  
   NULL  
};  

int main( int ac, char* av[] )  
{  
   char *args[4];  
   int ch;  
   if( ac != 3 ){  
      fprintf( stderr, "Usage: %s <program> <number (1-8)>\n", av[0] );  
      return;  
   }  

   // Arguments for _execv?   
   args[0] = av[1];  
   args[1] = "exec??";  
   args[2] = "two";  
   args[3] = NULL;  

   switch( atoi( av[2] ) )  
   {  
   case 1:  
      _execl( av[1], av[1], "_execl", "two", NULL );  
      break;  
   case 2:  
      _execle( av[1], av[1], "_execle", "two", NULL, my_env );  
      break;  
   case 3:  
      _execlp( av[1], av[1], "_execlp", "two", NULL );  
      break;  
   case 4:  
      _execlpe( av[1], av[1], "_execlpe", "two", NULL, my_env );  
      break;  
   case 5:  
      _execv( av[1], args );  
      break;  
   case 6:  
      _execve( av[1], args, my_env );  
      break;  
   case 7:  
      _execvp( av[1], args );  
      break;  
   case 8:  
      _execvpe( av[1], args, my_env );  
      break;  
   default:  
      break;  
   }  

   // This point is reached only if exec fails.   
   printf( "\nProcess was not execed." );  
   exit( 0 );  
}  

惠?Requirements

标头:process.hHeader: process.h

请参阅See Also

进程和环境控制 Process and Environment Control
abort abort
atexit atexit
exit、_Exit、_exit exit, _Exit, _exit
_onexit、_onexit_m _onexit, _onexit_m
_spawn、_wspawn 函数 _spawn, _wspawn Functions
system、_wsystemsystem, _wsystem