Question

I have a file hello.exe file path is D:\test\hello.exewhich is a simple hello world program (tested ok).

I have another program proc.c file path is D:\test\proc.c with code as follows:

#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<process.h>
#include<errno.h>
#include<string.h>

main(int argc,char *argv[])
{
int ret;
ret=execl("D:\\test\\hello.exe","D:\\test\\hello.exe");
if(ret==-1)
    printf("%d:\t%s",ret,errno);
}

The program hangs (windows dialog says The program has stopped working)!!! I even tried D:/test/hello.exe as param to execl(), but the same...

Where am I going wrong??? Please someone provide me the right syntax? Please prvide me example codes using various functions of process.h with MinGW under windows. Web tutorial links are acceptable as well.

Thanks a lt !!!

Was it helpful?

Solution

You have to include a library,

#include<unistd.h>

Because it is defined in this library So if you don't include this you will get AN error. and correct your code because you have written "exec" not execl.

int main()
{

execl("D:/test/hello","D:/test/hello",0);
return 0;
}

OTHER TIPS

If you compiled with warnings, you'd notice that errno is an int, but you're trying to use it with printf as a string, which means it is being used as a pointer. Memory address 0xFFFFFFF2 (or whatever the hexadecimal value of errno is) probably isn't valid.

Aside from that, you need to end your execl invocation with a NULL pointer. Otherwise the function will keep thinking it has more command line arguments, all of which are strings, until the first NULL it finds in memory or until the first invalid memory access, whichever comes first. Because copying the strings involves dereferencing a pointer, it will read the address made up of unknown bytes and attempt to dereference the pointer at that address repeatedly. If the pointer cannot be dereferenced, meaning accessing the address for reading data from it cannot be done, it will crash your program.

In the future, please compile with -Wall.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top