Discussion:
CreateProcess not executing dir
(too old to reply)
muta...@gmail.com
2021-04-24 07:05:25 UTC
Permalink
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.
R.Wieser
2021-04-24 07:52:58 UTC
Permalink
Paul,
Post by ***@gmail.com
rc = CreateProcessA("cmd.exe",
...
Post by ***@gmail.com
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
muta...@gmail.com
2021-04-24 10:22:47 UTC
Permalink
Post by R.Wieser
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.
muta...@gmail.com
2021-05-25 00:19:09 UTC
Permalink
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.
R.Wieser
2021-05-25 08:29:58 UTC
Permalink
Muta,
Post by ***@gmail.com
I found that I needed to
add "c:" before the "/c".
No (not exactly).
Post by ***@gmail.com
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 ? :-)
Post by ***@gmail.com
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
muta...@gmail.com
2021-05-25 17:55:28 UTC
Permalink
Post by R.Wieser
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.
R.Wieser
2021-05-25 19:39:43 UTC
Permalink
Paul,
Post by ***@gmail.com
Thanks Rudy!
You're welcome. And yes, I've run into the same problem myself. :-)
...
Post by ***@gmail.com
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

Continue reading on narkive:
Loading...