#include "stdlib.h" main() { system("PAUSE"); } 这样其实就是执行了系统的一个命令PAUSE,你还可以找到其他命令,比如DIR什么的,都可以用。在VC里,跟踪system函数,可以看到如下的函数: /*** *int system(command) - send the command line to a shell * *Purpose: * Executes a shell and passes the command line to it. * If command is NULL, determine if a command processor exists. * The command processor is described by the environment variable * COMSPEC. If that environment variable does not exist, try the * name "cmd.exe" for Windows NT and "command.com" for Windows '95. * *Entry: * char *command - command to pass to the shell (if NULL, just determine * if command processor exists) * *Exit: * if command != NULL returns status of the shell * if command == NULL returns non-zero if CP exists, zero if CP doesn't exist * *Exceptions: * *******************************************************************************/int __cdecl _tsystem ( const _TSCHAR *command ) { int catch; _TSCHAR *argv[4]; argv[0] = _tgetenv(_T("COMSPEC")); // 取得环境变量中的字符串,在win2000里comspec = %SystemRoot%\system32\cmd.exe,win95可能有所不同,但肯定是command.exe /* * If command == NULL, return true IFF %COMSPEC% * is set AND the file it points to exists. */ if (command == NULL) { return argv[0] == NULL ? 0 : (!_taccess(argv[0],0)); } _ASSERTE(*command != _T('\0')); argv[1] = _T("/c");// cmd/c PAUSE就是执行字符串指定的命令"PAUSE"然后终断 argv[2] = (_TSCHAR *) command; argv[3] = NULL; /* If there is a COMSPEC defined, try spawning the shell */ if (argv[0]) /* Do not try to spawn the null string */ if ((catch = _tspawnve(_P_WAIT,argv[0],argv,NULL)) != -1 || (errno != ENOENT && errno != EACCES)) return(catch); //_tspawnve执行argv[0]这个命令,argv作为参数其实就是执行cmd/c 参数就是PAUSE /* No COMSPEC so set argv[0] to what COMSPEC should be. */ argv[0] = ( _osver & 0x8000 ) ? _T("command.com") : _T("cmd.exe"); /* Let the _spawnvpe routine do the path search and spawn. */ return(_tspawnvpe(_P_WAIT,argv[0],argv,NULL)); }
main()
{
system("PAUSE");
}
这样其实就是执行了系统的一个命令PAUSE,你还可以找到其他命令,比如DIR什么的,都可以用。在VC里,跟踪system函数,可以看到如下的函数:
/***
*int system(command) - send the command line to a shell
*
*Purpose:
* Executes a shell and passes the command line to it.
* If command is NULL, determine if a command processor exists.
* The command processor is described by the environment variable
* COMSPEC. If that environment variable does not exist, try the
* name "cmd.exe" for Windows NT and "command.com" for Windows '95.
*
*Entry:
* char *command - command to pass to the shell (if NULL, just determine
* if command processor exists)
*
*Exit:
* if command != NULL returns status of the shell
* if command == NULL returns non-zero if CP exists, zero if CP doesn't exist
*
*Exceptions:
*
*******************************************************************************/int __cdecl _tsystem (
const _TSCHAR *command
)
{
int catch;
_TSCHAR *argv[4]; argv[0] = _tgetenv(_T("COMSPEC")); // 取得环境变量中的字符串,在win2000里comspec = %SystemRoot%\system32\cmd.exe,win95可能有所不同,但肯定是command.exe
/*
* If command == NULL, return true IFF %COMSPEC%
* is set AND the file it points to exists.
*/ if (command == NULL) {
return argv[0] == NULL ? 0 : (!_taccess(argv[0],0));
} _ASSERTE(*command != _T('\0')); argv[1] = _T("/c");// cmd/c PAUSE就是执行字符串指定的命令"PAUSE"然后终断
argv[2] = (_TSCHAR *) command;
argv[3] = NULL; /* If there is a COMSPEC defined, try spawning the shell */ if (argv[0]) /* Do not try to spawn the null string */
if ((catch = _tspawnve(_P_WAIT,argv[0],argv,NULL)) != -1
|| (errno != ENOENT && errno != EACCES))
return(catch);
//_tspawnve执行argv[0]这个命令,argv作为参数其实就是执行cmd/c 参数就是PAUSE
/* No COMSPEC so set argv[0] to what COMSPEC should be. */
argv[0] = ( _osver & 0x8000 ) ? _T("command.com") : _T("cmd.exe"); /* Let the _spawnvpe routine do the path search and spawn. */ return(_tspawnvpe(_P_WAIT,argv[0],argv,NULL));
}
我知道用管道可以获得Shell的输出,既然有system可以直接输入命令执行,那肯定也有办法直接获得控制台的输出了,有这们的API吗?没有的话怎么实现呢?
如果是这样的话,那就是新创建了一个进程或是线程了,真是这样的话,system("pause")还能让当前的控制台窗口暂停吗?!
楼主用WinExec() 试试另 pause命令就是 让DOS窗口暂停 并出现"press any key to continue"