Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

CreateProcess for batchfile with command-line args fails

812 views
Skip to first unread message

Vishwanath Nukal

unread,
Apr 9, 2001, 11:49:55 PM4/9/01
to
Hi

Using NT 4.0 SP5

From a Windows Service program I want to launch a batch file in a separate
process. I used CreateProcess in the following manner

CreateProcess(lpszCommand,
lpszCommandLine,
NULL,
NULL, FALSE,
CREATE_NO_WINDOW|CREATE_NEW_PROCESS_GROUP,
NULL,/* Environment of calling process */
NULL,
&si,
&pi);

This succeeds as long as lpszCommandLine is an empty string. But when
non-empty it fails with GetLastError returning 2. This does not happen when
lpszCommand is an .exe file.

From the newsgroups I found a workaround to route the batch file thro
CMD.EXE
i.e. Making the batch file and its command-line arguments(CLA) as
the CLA to CMD.exe. This worked but this has the overhead of an additional
CMD.EXE process which is undesirable this being a long-running program.

Does anybody have any ideas, apart from the above workaround, to launch a
batch-file having CLA using CreateProcess ?

Thanks,
V

------------------------------------------------------------
Get your FREE web-based e-mail and newsgroup access at:
http://MailAndNews.com

Create a new mailbox, or access your existing IMAP4 or
POP3 mailbox from anywhere with just a web browser.
------------------------------------------------------------

Luc Kumps

unread,
Apr 10, 2001, 1:39:44 AM4/10/01
to
A .BAT or .CMD file need to be interpreted by a Command Interpreter. CMD.EXE
is one example of a command interpreter. You could write another one, but
you would always need a .EXE to interpret the commands in the batch file.
Batch = "batch of commands". Some executable is needed to parse and execute
the commands...

Luc

"Vishwanath Nukal" <lak...@MailAndNews.com> wrote in message
news:3AD7...@MailAndNews.com...

Alex Blekhman

unread,
Apr 10, 2001, 2:36:25 AM4/10/01
to
"Vishwanath Nukal" <lak...@MailAndNews.com> wrote in message
news:3AD7...@MailAndNews.com...

Is this also happening if you specify NULL instead of lpszCommand and
in lpszCommandLine give filename and CLA (for instance: "myfile.bat
arg1 arg2")?

Alex Blekhman

unread,
Apr 10, 2001, 2:40:10 AM4/10/01
to
"Luc Kumps" <NOkum...@pandora.be> wrote in message
news:A0xA6.28104$ii5.2...@afrodite.telenet-ops.be...

> A .BAT or .CMD file need to be interpreted by a Command Interpreter.
CMD.EXE
> is one example of a command interpreter. You could write another
one, but
> you would always need a .EXE to interpret the commands in the batch
file.
> Batch = "batch of commands". Some executable is needed to parse and
execute
> the commands...

Is it shell functionality to recognise BAT and CMD files and to run
interpreter? Or this is more low level feature implemented by
OS(loader?) itself?

Luc Kumps

unread,
Apr 10, 2001, 2:29:32 AM4/10/01
to

"Alex Blekhman" <tkfx....@yahoo.com> wrote in message
news:9au6ir$6sgdf$1...@ID-54051.news.dfncis.de...

I *thought* I knew the answer... until I looked up the "cmdfile" and
"batfile" entries under HKEY_CLASSES_ROOT!
The Shell\open\command entries specify "%1" "%*", which is also present for
"comfile", "exefile", "piffile"

I expected to find %SYSTEMROOT%\CMD.EXE %* there...

I guess .bat .cmd .com .exe and .pif *are* handled in a "special" way.
Anyway, the result will always be that launching a .bat or cmd needs a
CMD.EXE or COMMAND.COM to handle it...
Or did I still miss something?

Luc

Vishwanath Nukal

unread,
Apr 10, 2001, 2:41:46 PM4/10/01
to

This makes sense, except for the fact that I am able to successfully launch a
batch file without a CMD.EXE when I don't pass any command-line arguments to the
batch file.
Why does the command-line arguments change the behaviour ?

Anyway, I continued the trial-and-error work and found that the following method
works.

CreateProcess(NULL,
"batchfile.bat cla1 cla2 cla3",


NULL,
NULL, FALSE,
CREATE_NO_WINDOW|CREATE_NEW_PROCESS_GROUP,
NULL,/* Environment of calling process */
NULL,
&si,
&pi);

Making the command as NULL and putting the batch-file alongwith its CLA into the
CommandLine does the trick. This does not even require the extra do-nothing
CMD.EXE process to be hovering around.

Thanks to all who responded
V

Luc Kumps wrote:

--
Vishwana...@oracle.com
Ph: (650) 633 6349


Michael D. Ober

unread,
Apr 10, 2001, 2:46:17 PM4/10/01
to
MSDN recommends that only 32 bit GUI programs be launched with the first
parameter in CreateProcess. You can launch ALL applications using the
second parameter.
--
Mike Ober.

"Vishwanath Nukal" <Vishwana...@Oracle.com> wrote in message
news:3AD353EA...@MailAndNews.com...

Luc Kumps

unread,
Apr 10, 2001, 3:17:44 PM4/10/01
to
An extra do-nothing CMD.EXE?
Doesn't your CreateProcess() create a CMD.EXE?

Luc

"Vishwanath Nukal" <Vishwana...@Oracle.com> wrote in message
news:3AD353EA...@MailAndNews.com...
>

0 new messages