Discussion:
GetCommandLine() and parsing input from a file (<)
(too old to reply)
Honni
2006-03-23 10:05:28 UTC
Permalink
I have the following code that takes the Console command line and
passes it to another program. Calling GetCommandLine() function does
not read the "<" for directing the contents of a text file to the
command line.

This does work for redirecting the output to the file.

CString CommandLine = (LPCTSTR) GetCommandLine();

//We need to strip the program name which may be enclosed by ""
if(CommandLine.GetLength() > 0)
{
int nProg;
if(CommandLine[0] == '"')
nProg = CommandLine.Find(_T("\""), 1);
else
nProg = CommandLine.Find(_T(" "), 0);
if(nProg >= 0)
CommandLine = CommandLine.Mid(nProg+1);
}

SHELLEXECUTEINFO ExeInfo;

memset(&ExeInfo,0,sizeof(SHELLEXECUTEINFO));

ExeInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ExeInfo.fMask = SEE_MASK_NOCLOSEPROCESS |
SEE_MASK_NO_CONSOLE;
ExeInfo.nShow = SW_SHOW;
ExeInfo.lpVerb = "open";
ExeInfo.lpFile = "t:\\ss\\win32\\sse.exe";
ExeInfo.lpParameters = CommandLine;

if(ShellExecuteEx(&ExeInfo))
{
(void)WaitForSingleObject (ExeInfo.hProcess, INFINITE);
(void)GetExitCodeProcess(ExeInfo.hProcess,&ExitCode);

(void)CloseHandle (ExeInfo.hProcess);
}
else
AfxMessageBox(_T("Error : t:\\ss\\win32\\sse.exe failed to
execute"));
Igor Tandetnik
2006-03-23 13:18:18 UTC
Permalink
Post by Honni
I have the following code that takes the Console command line and
passes it to another program. Calling GetCommandLine() function does
not read the "<" for directing the contents of a text file to the
command line.
Redirection is handled by the command interpreter. Your standard input
(stdin, GetStdHandle(STD_INPUT_HANDLE) ) is set up to read from the
file. Normally, when you create a child process it inherits your
standard handles. I'm not sure how SEE_MASK_NO_CONSOLE flag affects this
feature.

Does the program your are starting read from standard input?
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
Honni
2006-03-23 14:25:41 UTC
Permalink
The application I am starting is a command line application that takes
one or many arguements from the command promt. Such arguements can be
populated with a input file with symbol "< filename"
Honni
2006-03-23 14:46:01 UTC
Permalink
Update, anyway of seeing the "<" symbol on the command line?
Ulrich Eckhardt
2006-03-23 14:58:20 UTC
Permalink
Post by Honni
The application I am starting is a command line application that takes
one or many arguements from the command promt. Such arguements can be
populated with a input file with symbol "< filename"
It would have been beneficial if you hat quoted what exactly you were
referring to, as it seems there is a misunderstanding.

Anyhow, your above assumption is wrong. If you start a program from the
Windows command interpreter (command.com or cmd.exe), this interpreter (aka
shell) removes the '<' and connects the stdin stream of the new program to
that file as input. It has nothing to do with commandline arguments. Note
that std::system() invokes an intermediate shell.

If you start a program directly (CreateProcess..) this does not apply and
you simply get '<' and the filename as arguments in argc/argv.

Uli
Honni
2006-03-27 08:34:36 UTC
Permalink
Consider my program is called ss.exe and the program I am running is
called sse.exe.

I type -

ss label $/path <%TEMP%\build.txt >NULL

GetCommandLine() returns "label $/path" and ignores the bit after <
which stops the completion of the command and stalls the program
sse.exe. The same happens with argv.

sse label $/path <%TEMP%\build.txt >NULL works nicely.

Any ideas?

Loading...