-2

How to run any exe file from turbo c++? I know that I should stop using turbo c++ and move one to Dev or Code::Blocks, but my school doesn't agree so I gotta wing it.

I just want to know how to run a file with or without the system() function. Any kind of advice is welcome

Here's what I have tried so far:

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:

    start "c:\TurboC3\BIN\" tnfsv13.exe

NOTE: Just a doubt, you guys: system() is not working in windows XP. I tried it using dosbox in windows 7 and it works well, but in XP it does absolutely nothing. Not even the system("dir") command seems to work but system(NULL) returns 1. Any guesses why?

Thanks.

Kshitij
  • 15
  • 7
  • 2
    `system()` should work in Turbo C++ too. What have you tried, and what was the result? [If you are using the 16-bit DOS version of TC++, then you may have the problem that it can't start a 32-bit executable?] – Mats Petersson Jun 29 '15 at 19:43
  • _@Kshitij_ Also `fork()`/`exec()` should work, if you're required not to use `system()`. – πάντα ῥεῖ Jun 29 '15 at 19:45
  • 5
    Gotta love schools that prepare students for life in the fast-paced, ever changing world of software by teaching them with 20-year old tech. – user4581301 Jun 29 '15 at 19:48
  • @MatsPetersson I have uploaded what I tried and both ways have failed. I fixed the 16-bit and 32-bit problem(used a 16-bit application instead) but if there can be a work- around for that please let me know. – Kshitij Jun 29 '15 at 20:18
  • And exactly what happens, and how is that different from what you expect? – Mats Petersson Jun 29 '15 at 20:22
  • @MatsPetersson nothing exactly happens, there's only a blank screen. The system(NULL) shows 1, the system("dir") works fine but cannot run anything. – Kshitij Jun 29 '15 at 20:24
  • What is the .exe that you are running actually supposed to do? Does it work if you run it on its own? – Mats Petersson Jun 29 '15 at 20:30
  • Are you trying to slow down your PC in order to work around the long known bug in the startup code which takes a measurement for the delay routine? I guess this doesn't work for several reasons: The program you're trying to run may be a TSR. Can't start that like this, probably. And it's already too late, because the deleay initialization has already happened at that point in time. – Sebastian Jun 29 '15 at 22:01
  • @MatsPetersson it is a DOS based game. And yes, it works. Sebastian I'm a mere amateur, dude. I didn't know even know that Turbo C++ is so old until very recently. – Kshitij Jun 30 '15 at 17:30
  • Sorry, if tnfsv is a game, my guess was completely wrong. Maybe that bug I remembered only happens with Turbo Pascal, not Turbo C. – Sebastian Jul 01 '15 at 19:55
  • @Sebastian hmm can not remember for sure too but I think C++ did not have CRT unit. And there are also fixes for CRT runtime error not involving slowing down of computer (there are fixed crt libs out there and parches too) – Spektre May 08 '18 at 08:07

2 Answers2

1

system() works fine, though it may not work exactly the way you expect: it does the same thing as typing a command at a MSDOS (or Win32) command prompt including input and output being connected to the console.

If you just want to run a program, pass parameters, and not return from it, use a convenient form from the exec() family of functions. See this for one example.

Community
  • 1
  • 1
wallyk
  • 56,922
  • 16
  • 83
  • 148
  • I shall try exec(), but system() seemed straightforward, so I tried it first. I also read about spawnl and spawnv, can these be used too? – Kshitij Jun 29 '15 at 20:20
  • @Kshitij: Yes, `spawn()` is particularly good in the Microsoft environment because of MS's awkwardness managing processes. – wallyk Jun 29 '15 at 21:07
  • @Kshitij: Note that adding the executable file extension to a command is *ignored*. If there are multiple files differing only the extension, you cannot specify which one to run with the extension: DOS follows its own rules for which one to execute. As I recall, it first tries .COM then .EXE then .BAT. In modern versions of Windows, see the `PATHEXT` environment variable. On a nearby Win 7 system, the value is `PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC` – wallyk Jun 29 '15 at 21:10
  • Thank you for that info! Since I'm a mere beginner I couldn't understand the definitions given in c++, so understanding exec and spawn are kinda hard. I shall try and post results – Kshitij Jun 30 '15 at 16:49
1

You can also use Turbo C++'s execl() function. execl() loads and runs C:\\TC\\BIN\\tnfsv13.exe. NULL means there are no arguments to send to tnfsv13.exe. If an error occurs, execl() returns -1 into int c .

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

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


    return 0;
}

Explanation:

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.

-------------------------------- Example ---------------------------------

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);
       }
Software_Designer
  • 8,490
  • 3
  • 24
  • 28
  • So, what do I do? Could you please explain what does assigning that value to an integer does? Also, the value returned by c is -1. – Kshitij Jun 30 '15 at 17:02
  • I understand that and that means execl() has an error. Is there no way of telling what this error might be? and I still don't get how assigning it to to an integer can run the file. Elaborate please? Thanks! – Kshitij Jun 30 '15 at 17:27