How does Windows hook up cmd.exe?

来源:互联网 发布:电脑盲打软件 编辑:程序博客网 时间:2024/06/06 02:30
原文: http://discuss.joelonsoftware.com/default.asp?design.4.332503.5
When Windows invokes an application that uses the "console" subsystem, it launches cmd.exe which is used to display the application's output.  Text typed into cmd.exe is also transmitted to the application, as expected.

Two questions:

a) How does Windows decide which program is supposed to handle console applications?  Is this just a registry key?

b) How does Windows do this standard input/standard output piping?  It can't be using popen() because cmd.exe is exhibiting more functionality than popen() allows (read/write to a co-process).
Michael B
Sunday, April 16, 2006
 
 
Windows executables have a flag in their PE header indicating whether they are console or GUI applications: http://www.windowsitlibrary.com/Content/356/11/1.html .
GinG
Sunday, April 16, 2006
 
 
"When Windows invokes an application that uses the "console" subsystem, it launches cmd.exe which is used to display the application's output.  Text typed into cmd.exe is also transmitted to the application, as expected."

This not correct.  Are you thinking of a batch file that was invoked by the shell? Batch files -( .bat/.cmd files ) are associated with cmd.exe through COMSPEC registry key.
(HKLM/System/CurrentControlSet/Control/Session Manager/Environment".

When you execute a program directly ( createprocess ) you dont have a shell. The clearest evidence of this is that the ordinary dir and pipe operations dont work.

a - the explorer decides which program based on the filename extension.

b - if you arent running in cmd.exe, you dont get ordinary piping like ( |, >, < ). But stdin, stdout, stderr are part of the win32 process specification.  The CreateProcess call accepts (via startup info ) handles for stdin, stdout and stderr. So its up to the program that launched this program.  They are just ordinary windows handles.  Could be a file, named pipe, whatever.
B
Sunday, April 16, 2006
 
 
"How does Windows do this standard input/standard output piping?"

CreateProcess() can accept three file handles (stdin, stdout, stderr). The father process creates anonymous pipes, initializes a STARTUPINFO with the pipes' handles, and calls CreateProcess() to launch the child:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/createprocess.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/startupinfo_str.asp

Have a look on http://support.microsoft.com/default.aspx?scid=kb;en-us;190351 for an example.
GinG
Sunday, April 16, 2006
 
 
Ohhhhh.

All is clear, now.
Michael B
Sunday, April 16, 2006
 
 
There is also the issue of the Windows Console Device and related API calls.  This doesn't necessarily have a thing to do with standard I/O streams, and thus often can't be redirected.

This is why some people get frustrated with custom shells or even trying to control a spawned "Console EXE" via anonymous pipes.  Examples include the FTP.EXE that comes with Windows (at least the 2000/XP versions), EDIT.EXE, and many others such as several networking-related commands.
Lutz
Monday, April 17, 2006
 
 

This topic is archived. No further replies will be accepted.

 
原创粉丝点击