• CreateProcess not executing dir

    From mutazilah@gmail.com@21:1/5 to All on Sat Apr 24 00:05:25 2021
    Hi.

    I have my own system() function:

    __PDPCLIB_API__ int system(const char *string)
    {
    BOOL rc;
    PROCESS_INFORMATION pi;
    STARTUPINFO si;
    DWORD ExitCode;
    char cmdbuf[300];

    if (strlen(string) > sizeof cmdbuf - 10)
    {
    printf("command %s too long\n", string);
    return (-1);
    }
    strcpy(cmdbuf, "/c ");
    strcat(cmdbuf, string);
    memset(&si, 0, sizeof si);
    si.cb = sizeof si;
    memset(&pi, 0, sizeof pi);
    /*strcpy(cmdbuf, "/c mybat");*/
    printf("cmdbuf is %s\n", cmdbuf);
    rc = CreateProcessA("cmd.exe",
    cmdbuf,
    NULL,
    NULL,
    FALSE,
    0,
    NULL,
    NULL,
    &si,
    &pi);
    if (!rc)
    {
    printf("last error is %d\n", (int)GetLastError());
    return (GetLastError());
    }
    WaitForSingleObject(pi.hProcess, INFINITE);
    GetExitCodeProcess(pi.hProcess, &ExitCode);
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);
    return (ExitCode);
    }


    and it is printing:

    cmdbuf is /c dir
    last error is 2

    https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes--0-499-

    ERROR_FILE_NOT_FOUND

    2 (0x2)

    The system cannot find the file specified.


    I've tried a variety of things without success, such
    as executing a batch file. I'm always getting a return
    code of 2:

    As far as I can tell, I am executing it according to
    the instructions:

    https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa

    To run a batch file, you must start the command interpreter; set lpApplicationName to cmd.exe and set lpCommandLine to the following arguments: /c plus the name of the batch file.


    system("dir"); works on other people's compilers.

    Any ideas?

    Thanks. Paul.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From R.Wieser@21:1/5 to All on Sat Apr 24 09:52:58 2021
    Paul,

    rc = CreateProcessA("cmd.exe",
    ...
    last error is 2

    ERROR_FILE_NOT_FOUND

    https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa

    [quote]
    The string can specify the full path and file name of the module to execute
    or it can specify a partial name. In the case of a partial name, the
    function uses the current drive and current directory to complete the specification. The function will not use the search path.
    [/quote]

    Take notice of the last sentence.

    In other words, you have to use something like "c:\windows\system32\cmd.exe" (or first call a function which searches the path for the executable :-) )

    Regards,
    Rudy Wieser

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From mutazilah@gmail.com@21:1/5 to R.Wieser on Sat Apr 24 03:22:47 2021
    On Saturday, April 24, 2021 at 5:53:22 PM UTC+10, R.Wieser wrote:

    In other words, you have to use something like "c:\windows\system32\cmd.exe" (or first call a function which searches the path for the executable :-) )

    Thankyou so much!

    I used the "ComSpec" variable and now everything is
    working great.

    BFN. Paul.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From mutazilah@gmail.com@21:1/5 to All on Mon May 24 17:19:09 2021
    Hi.

    As a follow-up to calling CreateProcess().

    I also wish to run under "HX" on Freedos, and to work
    on both Windows 10 and HX I found that I needed to
    add "c:" before the "/c". Any idea what that is about?
    I don't see it mentioned in "cmd /?" nor the CreateProcess()
    documentation.

    cmdproc is XC:\WINDOWS\system32\cmd.exeX
    cmdbuf is Xc: /c dirX

    Thanks. Paul.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From R.Wieser@21:1/5 to All on Tue May 25 10:29:58 2021
    Muta,

    I found that I needed to
    add "c:" before the "/c".

    No (not exactly).

    Any idea what that is about?

    Yes.

    A question though: Have you already tried to replace that "c:" with
    something like "foo", "bar" or just some other random string / character ?
    Why not ? :-)

    I don't see it mentioned in "cmd /?" nor the CreateProcess()
    documentation.

    Look again :

    [quote]
    If both lpApplicationName and lpCommandLine are non-NULL,
    ....
    Because argv[0] is the module name, C programmers generally ***repeat the module name as the first token in the command line***.
    [/quote]

    ("***" added for emphasis)

    Regards,
    Rudy Wieser

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From mutazilah@gmail.com@21:1/5 to R.Wieser on Tue May 25 10:55:28 2021
    On Tuesday, May 25, 2021 at 6:30:11 PM UTC+10, R.Wieser wrote:

    Because argv[0] is the module name, C programmers generally ***repeat the module name as the first token in the command line***.

    Thanks Rudy!

    I'm now on to hopefully the last problem, this time
    seemingly with HX.

    But it seems to me that this is quite misleading:

    To run a batch file, you must start the command interpreter; set lpApplicationName to cmd.exe and set lpCommandLine to the following arguments: /c plus the name of the batch file.


    It doesn't suggest that you're supposed to put something
    before the "/c" as mentioned elsewhere.

    BFN. Paul.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From R.Wieser@21:1/5 to All on Tue May 25 21:39:43 2021
    Paul,

    Thanks Rudy!

    You're welcome. And yes, I've run into the same problem myself. :-)

    But it seems to me that this is quite misleading:
    ...
    It doesn't suggest that you're supposed to put something
    before the "/c" as mentioned elsewhere.

    True, but they are talking about a Microsoft OS, not FreeDos. And its
    rather possible that its not the problem of the CreateProcess function, but that of the executed program : /it/ expects the modulename to come first.

    Than again, I seem to remember a similar problem, which I solved by starting the commandline arguments with a single space.

    Regards,
    Rudy Wieser

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)