【问题标题】:Turbo C++ system function running an executable运行可执行文件的 Turbo C++ 系统函数
【发布时间】:2015-09-16 10:03:48
【问题描述】:

如何从 turbo c++ 运行任何 exe 文件?我知道我应该停止使用 turbo c++ 并将其移至 Dev 或 Code::Blocks,但我的学校不同意,所以我得动起来。

我只想知道如何使用或不使用 system() 函数来运行文件。 欢迎任何建议

这是我迄今为止尝试过的:

1

#include<process.h>
int main()
{
    system("tnfsv13.exe");     //tnfsv being a 16-bit application(The need for slowness v 13)
    return 0;
} 

2

  #include<process.h>
    int main()
    {
        system("tnfsv13.bat");     
         return 0;
    } 
  1. tnfsv13.bat:

    启动 "c:\TurboC3\BIN\" tnfsv13.exe

注意:只是一个疑问,你们:system() 在 Windows XP 中不起作用。我在 Windows 7 中使用 dosbox 进行了尝试,它运行良好,但在 XP 中它完全没有任何作用。甚至 system("dir") 命令似乎都不起作用,但 system(NULL) 返回 1。任何猜测为什么?

谢谢。

【问题讨论】:

  • system() 也应该在 Turbo C++ 中工作。你尝试过什么,结果如何? [如果你使用的是16位DOS版本的TC++,那么你可能会遇到无法启动32位可执行文件的问题?]
  • @Kshitij 如果要求您不要使用 system()fork()/exec() 也应该可以使用。
  • 爱上那些通过 20 年的老技术教授学生,为他们在快节奏、不断变化的软件世界中的生活做好准备的学校。
  • @MatsPetersson 我已经上传了我尝试过的内容,但两种方式都失败了。我修复了 16 位和 32 位问题(改用 16 位应用程序),但如果有解决方法,请告诉我。
  • 究竟会发生什么,这与您的预期有何不同?

标签: c++ function codeblocks dev-c++ turbo-c++


【解决方案1】:

您还可以使用 Turbo C++ 的execl() 函数。 execl() 加载并运行 C:\\TC\\BIN\\tnfsv13.exeNULL 表示没有要发送到 tnfsv13.exe 的参数。如果发生错误,execl() 会将 -1 返回到 int c 中。

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

int main()
{
    int c = execl("C:\\TC\\BIN\\tnfsv13.exe", NULL);


    return 0;
}

解释:

execl() loads and executes a new child process.  Because the child
process is placed in the memory currently occupied by the calling
process, there must be sufficient memory to load and execute it.

'pathname' specifies the file name of the child process.  If
'pathname' has a file name extension, then only that file is searched
for. If 'pathname' ends with a period (.), then 'pathname' without an
extension is searched for.  If that filename is not found, then
".EXE" is appended and execl() searches again.  If 'pathname' has no
extension and does not end with a period, then execl() searches for
'pathname' and, if it is not found, appends ".COM" and searches
again.  If that is not found, it appends ".EXE" and searches again.

 'arg0', 'arg1',...'argn' are passed to the child process as command-
line parameters.  A NULL pointer must follow 'argn' to terminate the
list of arguments. 'arg0' must not be NULL, and is usually set to
'pathname'.

The combined length of all the strings forming the argument list
passed to the child process must not exceed 128 bytes.  This includes
"n" (for 0-n arguments) space characters (required to separate the
arguments) but does not include the null ('\0') terminating
character.

   Returns:     If execl() is successful, it does not return to the
                calling process. (See the spawn...() routines for a
                similar function that can return to the calling
                process). If an error occurs, execl() returns -1 to
                the calling process. On error, 'errno' (defined in
                <errno.h>) is set to one of the following values
                (defined in <errno.h>):

                E2BIG       Argument list or environment list too big.
                              (List > 128 bytes, or environment > 32k)
                EACCES      Locking or sharing violation on file.
                              (MS-DOS 3.0 and later)
                EMFILE      Too many files open.
                ENOENT      File or path not found.
                ENOEXEC     File not executable.
                ENOMEM      Not enough memory.

     Notes:     Any file open when an exec call is made remains open
                in the child process.  This includes
                'stdin','stdout', 'stderr', 'stdaux', and 'stdprn'.

                The child process acquires the environment of the
                calling process.

                execl() does not preserve the translation modes of
                open files.  Use setmode() in the child process to
                set the desired translation modes.

                See the spawn...() routines for similar though more
                flexible functions that can return to the calling
                program.

   Caution:     The file pointers to open buffered files are not
                always preserved correctly.  The information in the
                buffer may be lost.

                Signal settings are not preserved.  They are reset to
                the default in the child process.

-------------------------------- 示例 -------------- -------------------

The following statements transfer execution to the child process
"child.exe" and pass it the three arguments "child", "arg1",
and"arg2":

       #include <process.h>    /* for 'execl' */
       #include <stdio.h>      /* for 'printf' and 'NULL' */
       #include <errno.h>      /* for 'errno', 'ENOENT' and 'ENOMEM' */

       main()
       {
           execl("child.exe", "child", "arg1", "arg2", NULL);
           /* only get here on an exec error */
           if (errno == ENOENT)
               printf("child.exe not found in current directory\n");
           else if (errno == ENOMEM)
               printf("not enough memory to execute child.exe\n");
           else
               printf("  error #%d trying to exec child.exe\n", errno);
       }

【讨论】:

  • 那么,我该怎么办?您能否解释一下将该值分配给整数的作用?另外,c 返回的值是-1。
  • 我明白这意味着 execl() 有错误。有没有办法告诉这个错误可能是什么?而且我仍然不明白如何将其分配给整数可以运行该文件。请详细说明?谢谢!
【解决方案2】:

system() 工作正常,但它可能无法完全按照您的预期工作:它的作用与在 MSDOS(或 Win32)命令提示符下键入命令相同,包括连接到控制台的输入和输出。

如果您只想运行一个程序、传递参数而不是从中返回,请使用exec() 系列函数中的一种方便的形式。一个例子请参见this

【讨论】:

  • 我会尝试 exec(),但是 system() 看起来很简单,所以我先尝试了。我还阅读了 spawnl 和 spawnv,这些也可以使用吗?
  • @Kshitij:是的,spawn() 在 Microsoft 环境中特别好,因为 MS 管理流程很笨拙。
  • @Kshitij:请注意,将可执行文件扩展名添加到命令是忽略。如果有多个文件仅扩展名不同,则无法指定使用扩展名运行哪个文件:DOS 遵循自己的规则来执行哪个文件。我记得,它首先尝试 .COM,然后是 .EXE,然后是 .BAT。在现代版本的 Windows 中,请参阅 PATHEXT 环境变量。在附近的 Win 7 系统上,值为PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
  • 感谢您提供的信息!由于我只是一个初学者,我无法理解 C++ 中给出的定义,所以理解 exec 和 spawn 有点困难。我会尝试发布结果
猜你喜欢
  • 2023-03-19
  • 1970-01-01
  • 1970-01-01
  • 2011-01-29
  • 1970-01-01
  • 2013-08-13
  • 1970-01-01
  • 1970-01-01
  • 2015-06-14
相关资源
最近更新 更多