簡體   English   中英

運行可執行文件的 Turbo C++ 系統函數

[英]Turbo C++ system function running an executable

如何從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()工作正常,盡管它可能不完全按照您的預期工作:它與在 MSDOS(或 Win32)命令提示符下鍵入命令的作用相同,包括連接到控制台的輸入和輸出。

如果您只想運行一個程序,傳遞參數,而不是從中返回,請使用exec()系列函數中的一種方便的形式。 請參閱示例。

您還可以使用 Turbo C++ 的execl()函數。 execl()加載並運行C:\\\\TC\\\\BIN\\\\tnfsv13.exe NULL表示沒有要發送到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);
       }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM