CreateProcess not executing dir

10 views
Skip to first unread message

muta...@gmail.com

unread,
Apr 24, 2021, 3:05:27 AMApr 24
to
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

unread,
Apr 24, 2021, 3:53:22 AMApr 24
to
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


muta...@gmail.com

unread,
Apr 24, 2021, 6:22:49 AMApr 24
to
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.

muta...@gmail.com

unread,
May 24, 2021, 8:19:10 PMMay 24
to
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

unread,
May 25, 2021, 4:30:11 AMMay 25
to
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


muta...@gmail.com

unread,
May 25, 2021, 1:55:29 PMMay 25
to
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.

R.Wieser

unread,
May 25, 2021, 3:39:55 PMMay 25
to
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


Reply all
Reply to author
Forward
0 new messages