HOWTO: Redirect Standard I/O of a Console App Using Batch File
ID: Q150767
|
The information in this article applies to:
-
Microsoft Visual Basic Professional and Enterprise Editions, 32-bit only, for Windows, version 4.0
SUMMARY
A 32-bit Visual Basic application launches another Win32 process by using
either the Visual Basic Shell command or the CreateProcess Win32 API. If
the new process is a console application that reads its input from the
standard input (STDIN) or writes its results to the standard output
(STDOUT), you can redirect its input and output from the parent Visual
Basic application. This article describes how to use a batch file (.BAT) to
redirect the standard input and output of the spawned console process. To
build the sample code in this article, you need the 32-bit edition of
Visual Basic and any development tools, such as Visual C++ version 2.0 and
above, that build Win32 console applications.
MORE INFORMATION
After the parent Visual Basic application spawns the child console process,
the parent Visual Basic application provides input to the child's STDIN and
receives the output from the child's STDOUT. By using a batch file, the
parent Visual Basic application provides the child's STDIN through a disk
file and collects the child's STDOUT through another disk file.
Step-by-Step Example
- Create a console application, CONSOL.EXE, that expects an integer as its
STDIN and sends a text string out as its STDOUT, using the following C
code:
#include <stdio.h>
void main(void)
{
int i;
scanf("%d", &i);
printf("\nSTDIn is %d!\n", i);
}
- Create a batch file, named REDIRECT.BAT, that contains only the
following command line:
type stdin.txt | consol.exe > stdout.txt
- Create a new text file using Notepad or any text editor. Enter an
integer and press the ENTER key. Save the file as "stdin.txt."
- Start a new project in Visual Basic. Form1 is created by default.
- Add the following code to the General Declarations section of Form1:
Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal _
dwAccess As Long, ByVal fInherit As Integer, ByVal hObject _
As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal _
hObject As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
hHandle As Long, ByVal dwMilliseconds As Long) As Long
Const SYNCHRONIZE = &H100000
Const NORMAL_PRIORITY_CLASS = &H20&
Const INFINITE = -1&
- Add the following code to the Form1_Click event:
ProcessID& = Shell("test.bat", vbNormalFocus)
ProcessHandle& = OpenProcess(SYNCHRONIZE, True, ProcessID&)
WaitForSingleObject ProcessHandle&, -1&
CloseHandle ProcessHandle&
- Save Form1 and Project1 to the same directory as REDIRECT.BAT and
CONSOL.EXE. Press the F5 key to run the program. Click Form1. A console
window is displayed briefly and closes itself. The STDOUT.TXT file is
then created in the same directory.
Additional query words:
Keywords : kbVBp400 PrgOther VB4WIN kb32bitOnly
Version : WINDOWS:4.0
Platform : WINDOWS
Issue type :
|