Microsoft KB Archive/150767

From BetaArchive Wiki
< Microsoft KB Archive
Revision as of 12:55, 21 July 2020 by X010 (talk | contribs) (Text replacement - "&" to "&")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Base


Article ID: 150767

Article Last Modified on 7/16/2004



APPLIES TO

  • Microsoft Visual Basic 4.0 Professional Edition
  • Microsoft Visual Basic 4.0 32-Bit Enterprise Edition



This article was previously published under Q150767

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

  1. 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);
          }
    
                            
  2. Create a batch file, named REDIRECT.BAT, that contains only the following command line:

    type stdin.txt | consol.exe > stdout.txt
  3. 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."
  4. Start a new project in Visual Basic. Form1 is created by default.
  5. 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&
    
                            
  6. Add the following code to the Form1_Click event:

          ProcessID& = Shell("test.bat", vbNormalFocus)
          ProcessHandle& = OpenProcess(SYNCHRONIZE, True, ProcessID&)
          WaitForSingleObject ProcessHandle&, -1&
          CloseHandle ProcessHandle&
    
                            
  7. 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.


Keywords: kbhowto kb32bitonly KB150767