?
Solved

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

Posted on 2003-02-26
9
Medium Priority
?
1,202 Views
Last Modified: 2007-11-27
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
0
Comment
Question by:jimbox
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 8

Accepted Solution

by:
Exceter earned 140 total points
ID: 8030173
>> 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
 

Author Comment

by:jimbox
ID: 8032632
thanks Exceter.

I will give that a try.

Jim
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 8034589
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:Exceter
ID: 8034708
>> 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
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 8034752

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
 
LVL 8

Expert Comment

by:Exceter
ID: 8034878
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
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 8035609
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
 

Author Comment

by:jimbox
ID: 8038083
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
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 8038640

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses
Course of the Month9 days, 7 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question