.netPlus / Samples / VB | ||
Click here
to view screenshot of sample 2
Filename: | cmdoutput.zip |
Last update: | 01/11/01 |
Size: | 47 kb |
Overview
This ZIP file contains two samples demonstrating how you can capture the output
of a console application into a VB String. They are both based on a generic
module, MGetCmdOutput.bas. This module returns the output written to STDOUT
and/or STDERR, and optionally converts "DOS" to "Windows"
characters.
Sample 1 is a very basic sample, demonstrating only what the module is capable
of by launching a demo application (ConsoleApp.exe, C++ source
included) and displays its output in a message box. Sample 2 is a command prompt
program that lets you enter any command
you want and view its output.
Details
To setup the redirection of output, the first step is to create pipes (in this
case anonymous) with the CreatePipe
API. The returned pipe handles are set as output handles for the application
being launced, in the STARTUPINFO structure in a call to CreateProcess.
Data written to the pipe is read, using ReadFile,
until the launced application terminates.
It is not possible to directly redirect the output of a 16-bit process to a
GUI application running under Windows 9x. If you try to do so,
ReadFile will never return and your program will be blocked. This is documented
in MS Knowledge Base article Q150956, see the links below.
INFO:
Redirection Issues on Windows 95 MS-DOS Applications
The article also suggests a possible solution to the problem. You need an intermediate
Win32 Console application that will launch the 16-bit
program in the same console. RedirStub.exe in the Sample 2 directory does exactly
this. It's needed when Sample 2 is run on Windows 9x, since Command.com, which
is used to process the entered commands, is a 16-bit application. Most console
utilities shipped with Windows 9x are 16-bit, like FC, FIND and SORT.
©2000-2024, .netPlus