execl command to run another *.exe program inside C program

I am trying write a C program to using execl to run another *.exe program.

Here's an example in C.

==== foo.c =====
#include <stdio.h>
#include <process.h>
int main(void) {
  execl("c:\tool", "rmtool.exe",NULL);
  return 0;
}

in dos:
ren foo.exe foobar.exe

copy /b foobar.exe+rmtool.exe foo.exe

running foo.exe will execute rmtool.exe

Does anyone know if it's I can do something like what I describe?

Thanks.

Jim
jimboxAsked:
Who is Participating?
 
ExceterConnect With a Mentor Commented:
>> Does anyone know if it's I can do something like what I describe?

Sure.

>> execl("c:\tool", "rmtool.exe",NULL);

First of all you need to use two backslashes here as the first one is removed when the compiler evaluates the string checking for escape sequences.

execl("c:\\tool", "rmtool.exe",NULL);

If that fails you can try using the system function.

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    system("c:\\tool\\rmtool.exe");
    return 0;
}

Exceter
0
 
jimboxAuthor Commented:
thanks Exceter.

I will give that a try.

Jim
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Exceter's answers (in this and other threads) suggest that he's not too far removed from his C coding days.  :)


jimbox,

The ONLY thing to add to Exceter's reply is that the exec...() family of function calls overlay the new program over your existing program.  If the function returns a value to the calling program it was unable to initiate the command.

Kent
0
Choose an Exciting Career in Cybersecurity

Help prevent cyber-threats and provide solutions to safeguard our global digital economy. Earn your MS in Cybersecurity. WGU’s MSCSIA degree program was designed in collaboration with national intelligence organizations and IT industry leaders.

 
ExceterCommented:
>> Exceter's answers (in this and other threads) suggest that he's not too far removed from his C coding days.  :)

What can I say, I love C. :-)

Exceter
0
 
Kent OlsenData Warehouse Architect / DBACommented:

Funny,

I've been unixing and Cing sine '82 and still hate them both.  (But I have become a pretty fluent C coder.)

kdo

0
 
ExceterCommented:
I geuss it has to do with the fact that I started learning C when I was 13 and it is what I am comfortable with. You are right, however, I do need to become more fluent with the more advanced side of C++. :-)

Exceter
0
 
Kent OlsenData Warehouse Architect / DBACommented:
C was the 3rd or 4th "structured" (recursive) language that I latched onto.  I was always partial to PASCAL, though there really isn't that much of it in use.  C++ pretty much made sure of that.

Though my C/C++ coding out paces all other languages about 50 to 1, I still despise C for its "forgiving" nature.  My worst experience was developing a parser on a TRU64 Alpha system.  Integers and pointers are the same size, and the compiler can be quite forgiving of the casting of similar sized objects.  The application kept blowing up at random addresses for apparently random reasons so I brought the whole thing home and put in on my linux box.  Its compiler quickly noted that I was interchanging ints and *ints.  :(  Something that the Alpha didn't reveal.

I've since learned more about the Alpha, :), but what a way to learn.  
0
 
jimboxAuthor Commented:
I tried to use the overlay with exec() functions, I still wasn't able to call it properly.  I tried your suggestion using the system call and it works.  I was hoping that I could use the overlay exec() function so I can hide my code.

Thanks.
Jim
0
 
Kent OlsenData Warehouse Architect / DBACommented:

Hmmm....  A hacker in the making????   ;)


Check errno right after the execl() call.  If you see the error message the call didn't work.  You can look up the error in the errno.h file.


execl ("c:\\tool", "rmtool.exe", NULL);
fprintf (stderr, " errno=%d\n", errno);



Kdo
0
All Courses

From novice to tech pro — start learning today.