0

_execl() is returning -1 and error message as "No such file or directory" even though the given file is there. When I run gzip command directly on command prompt it works. I am not able to understand what is it that I am missing here.

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

void main(){
int ret = _execl("cmd.exe", "gzip.exe", "C:\\Users\\user_name\\work\\Db618\\test.txt");
printf("ret: %d \t strerror: %s\n", ret, strerror(errno));
}

Can someone give an example of how to use this function, I found one more API system() while looking for a solution, but before using that I wanted to know what is the difference in both of these on Windows platform?

confucius_007
  • 186
  • 1
  • 15
  • 1
    Use [`_wspawnl`](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/spawnl-wspawnl?view=vs-2019) instead of `_execl`. Windows does not implement replacing the image of the current process, so `_execl` just spawns a new process and calls `exit`, which is a mess for console apps. `cmdname` is used as the `lpApplicationName` argument of `CreateProcessW`, so it needs to be a qualified path. `arg0` can be the same as `cmdname`, or an unqualified name such as just "cmd". It and the remaining arguments are joined as the `lpCommandLine` argument of `CreateProcessW`. – Eryk Sun Jul 29 '20 at 23:13
  • 1
    As far as I know, `_execl` is modelled after the `execl` of POSIX and POSIX requires that the path to the executable is specified in full. If you want to find commands in PATH, then you need to use `_execlp`. – Antti Haapala -- Слава Україні Jul 30 '20 at 05:40
  • @AnttiHaapala, but it is nothing like POSIX `execl` in practice because it doesn't replace the process image, but instead creates a new process and terminates the current process. So if you're running this from the command line, the shell only waits for the process that it creates and resumes its interactive prompt after the `_execl` call, and now two processes are competing to read and write from the console. It's worthless in Windows. – Eryk Sun Jul 30 '20 at 10:45
  • @eryksun yea, all these functions exist in windows nowadays solely so that back in time they could deceive people that windows is posix compatible... – Antti Haapala -- Слава Україні Jul 30 '20 at 11:22

1 Answers1

1

According to the _execl:Your first parameter does not need to be cmd.exe, but should be the first command of the command line, like gzip.exe.

You can refer to the MSDN sample.

Finally, your program only needs to delete the initial "cmd.exe", but it should be noted that the last parameter must be NULL to indicate termination.

Here is the code:

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

int main(int argc, const char* argv[])
{
    int ret = _execl("D:\\gzip-1.3.12-1-bin\\bin\\gzip.exe" ,"-f","D:\\gzip-1.3.12-1-bin\\bin\\test.txt" ,NULL);
    printf("ret: %d \t strerror: %s\n", ret, strerror(errno));
    return 0;
}

If you want to use system, you can pass the command as a parameter to the system function just like using CMD to achieve the same effect.

You can use it like:

system("gzip.exe test.txt");
Zeus
  • 3,703
  • 3
  • 7
  • 20
  • Can you please explain how to check the status of the execution of these functions, like if the execution is completed of not? Is there a way to safely stop the execution if it is going beyond some time limit? – confucius_007 Aug 05 '20 at 11:46
  • According to the [_execl](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/execl-wexecl?view=vs-2019):If successful, these functions do not return to the calling process. A return value of -1 indicates an error, in which case the errno global variable is set.Also you can refer to [system](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/system-wsystem?view=vs-2019) to find its return value. – Zeus Aug 06 '20 at 01:03