Microsoft KB Archive/150767: Difference between revisions

From BetaArchive Wiki
m (Text replacement - ">" to ">")
m (Text replacement - "&" to "&")
 
(2 intermediate revisions by the same user not shown)
Line 63: Line 63:
       {
       {
             int i;
             int i;
             scanf("%d", &i);
             scanf("%d", &i);
             printf("\nSTDIn is %d!\n", i);
             printf("\nSTDIn is %d!\n", i);
       }
       }


Line 71: Line 71:
<br />
<br />
'''type stdin.txt | consol.exe > stdout.txt'''</li>
'''type stdin.txt | consol.exe > stdout.txt'''</li>
<li>Create a new text file using Notepad or any text editor. Enter an integer and press the ENTER key. Save the file as &quot;stdin.txt.&quot;</li>
<li>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."</li>
<li>Start a new project in Visual Basic. Form1 is created by default.</li>
<li>Start a new project in Visual Basic. Form1 is created by default.</li>
<li><p>Add the following code to the General Declarations section of Form1:</p>
<li><p>Add the following code to the General Declarations section of Form1:</p>
<pre class="codesample">      Private Declare Function OpenProcess Lib &quot;kernel32.dll&quot; (ByVal _
<pre class="codesample">      Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal _
         dwAccess As Long, ByVal fInherit As Integer, ByVal hObject _
         dwAccess As Long, ByVal fInherit As Integer, ByVal hObject _
         As Long) As Long
         As Long) As Long


       Private Declare Function CloseHandle Lib &quot;kernel32&quot; (ByVal _
       Private Declare Function CloseHandle Lib "kernel32" (ByVal _
         hObject As Long) As Long
         hObject As Long) As Long


       Private Declare Function WaitForSingleObject Lib &quot;kernel32&quot; (ByVal _
       Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
       hHandle As Long, ByVal dwMilliseconds As Long) As Long
       hHandle As Long, ByVal dwMilliseconds As Long) As Long


       Const SYNCHRONIZE = &amp;H100000
       Const SYNCHRONIZE = &H100000
       Const NORMAL_PRIORITY_CLASS = &amp;H20&amp;
       Const NORMAL_PRIORITY_CLASS = &H20&
       Const INFINITE = -1&amp;
       Const INFINITE = -1&


                         </pre></li>
                         </pre></li>
<li><p>Add the following code to the Form1_Click event:</p>
<li><p>Add the following code to the Form1_Click event:</p>
<pre class="codesample">      ProcessID&amp;amp; = Shell(&quot;test.bat&quot;, vbNormalFocus)
<pre class="codesample">      ProcessID& = Shell("test.bat", vbNormalFocus)
       ProcessHandle&amp; = OpenProcess(SYNCHRONIZE, True, ProcessID&amp;)
       ProcessHandle& = OpenProcess(SYNCHRONIZE, True, ProcessID&)
       WaitForSingleObject ProcessHandle&amp;, -1&amp;
       WaitForSingleObject ProcessHandle&, -1&
       CloseHandle ProcessHandle&amp;
       CloseHandle ProcessHandle&


                         </pre></li>
                         </pre></li>

Latest revision as of 12:55, 21 July 2020

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