Microsoft Knowledge Base Email Alertz

KBAlertz.com: (150767) - 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...

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 2.0 Web Hosting with SQL 2005: Click Here!
Discount ASP.NET Hosting


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




How To Redirect Standard I/O of a Console App Using Batch File

Article ID:150767
Last Review:July 16, 2004
Revision:2.2
This article was previously published under Q150767
On This Page

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.

Back to the top

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.

Back to the top

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&amp; = 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.

Back to the top


APPLIES TO
Microsoft Visual Basic 4.0 Professional Edition
Microsoft Visual Basic 4.0 32-Bit Enterprise Edition

Back to the top

Keywords: 
kbhowto kb32bitonly KB150767

Back to the top

       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Anonymous User Report As Irrelevant  
Written: 12/24/2004 7:57 AM
Nice article but a bit confusing because of the call to test.bat in the form1_click event shell call. Shouldn't this be a call to redirect.bat instead?

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please