在vc.net中可以用system("pause");在控制台中暂停,system函数是怎么实现的,在VC6中要用什么头文件才可以使用这个函数?

解决方案 »

  1.   

    能解释一下system函数及其原理吗?
      

  2.   

    #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));
    }
      

  3.   

    天哪,看了半天看不大懂system()到底是怎么执行的。谢谢 kevin_wang。
    我知道用管道可以获得Shell的输出,既然有system可以直接输入命令执行,那肯定也有办法直接获得控制台的输出了,有这们的API吗?没有的话怎么实现呢?
      

  4.   

    system函数和你在开始菜单“运行”里输入命令的执行效果是一样的   因为你执行system函数时会看到有个DOS窗口闪一下
      

  5.   

    To palmax(南宫煌) 
    如果是这样的话,那就是新创建了一个进程或是线程了,真是这样的话,system("pause")还能让当前的控制台窗口暂停吗?!
      

  6.   

    当然可以,我就是因为老是ping网关很烦不过,所以自己写了就这么一行的代码system("ping 202.114.215.254 -t");自己就这样停住了啊
      

  7.   

    system函数的缺点就是打开一个DOS窗口   只有该线程结束  才能执行后续代码
    楼主用WinExec() 试试另 pause命令就是  让DOS窗口暂停    并出现"press any key to continue"