Get Character From DOS Window


How can I get or capture characters from DOS Window? Basically DOS Command Prompt Window is 80 x 24 chars in size. The program must be run in Windows 98. To run the program I will create a visual basic program then when I press button there it will start capturing the characters from DOS Window.

Who is Participating?
[ fanpages ]Connect With a Mentor IT Services ConsultantCommented:

You will need to redirect the STDOUT pipe as demonstrated here:

"Capturing the Output of a shelled program"

And detailed on the Microsoft Support (Knowledge Base) site:
"How To Create a Process for Reading and Writing to a Pipe"
[ ]

1.      Start a new project in Visual Basic. Form1 is created by default.
2.      Add the following code to the General Declarations section of Form1:

      Option Explicit

      Private Declare Function CreatePipe Lib "kernel32" ( _
          phReadPipe As Long, _
          phWritePipe As Long, _
          lpPipeAttributes As Any, _
          ByVal nSize As Long) As Long

      Private Declare Function ReadFile Lib "kernel32" ( _
          ByVal hFile As Long, _
          ByVal lpBuffer As String, _
          ByVal nNumberOfBytesToRead As Long, _
          lpNumberOfBytesRead As Long, _
          byval lpOverlapped As Any) As Long

          nLength As Long
          lpSecurityDescriptor As Long
          bInheritHandle As Long
      End Type

      Private Type STARTUPINFO
         cb As Long
         lpReserved As Long
         lpDesktop As Long
         lpTitle As Long
         dwX As Long
         dwY As Long
         dwXSize As Long
         dwYSize As Long
         dwXCountChars As Long
         dwYCountChars As Long
         dwFillAttribute As Long
         dwFlags As Long
         wShowWindow As Integer
         cbReserved2 As Integer
         lpReserved2 As Long
         hStdInput As Long
         hStdOutput As Long
         hStdError As Long
      End Type

         hProcess As Long
         hThread As Long
         dwProcessID As Long
         dwThreadID As Long
      End Type

      Private Declare Function CreateProcessA Lib "kernel32" (ByVal _
         lpApplicationName As Long, ByVal lpCommandLine As String, _
         lpProcessAttributes As Any, lpThreadAttributes As Any, _
         ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
         ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, _
         lpStartupInfo As Any, lpProcessInformation As Any) As Long

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

      Private Const NORMAL_PRIORITY_CLASS = &H20&
      Private Const STARTF_USESTDHANDLES = &H100&

      Private Sub ExecCmd(cmdline$)
          Dim proc As PROCESS_INFORMATION, ret As Long, bSuccess As Long
          Dim start As STARTUPINFO
          Dim sa As SECURITY_ATTRIBUTES, hReadPipe As Long, hWritePipe _
          As Long
          Dim bytesread As Long, mybuff As String
          Dim i As Integer

          mybuff = String(256, Chr$(65))

          sa.nLength = Len(sa)
          sa.bInheritHandle = 1&
          sa.lpSecurityDescriptor = 0&

          ret = CreatePipe(hReadPipe, hWritePipe, sa, 0)
          If ret = 0 Then
              MsgBox "CreatePipe failed. Error: " & Err.LastDllError
              Exit Sub
          End If

       start.cb = Len(start)
       start.dwFlags = STARTF_USESTDHANDLES
       start.hStdOutput = hWritePipe

       ' Start the shelled application:
       ret& = CreateProcessA(0&, cmdline$, sa, sa, 1&, _
       NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
       If ret <> 1 Then
           MsgBox "CreateProcess failed. Error: " & Err.LastDllError
       End If

       bSuccess = ReadFile(hReadPipe, mybuff, 100, bytesread, 0&)
       If bSuccess = 1 Then
           List1.AddItem Left(mybuff, bytesread)
           MsgBox "ReadFile failed. Error: " & Err.LastDllError
       End If

          ret& = CloseHandle(proc.hProcess)
          ret& = CloseHandle(proc.hThread)
          ret& = CloseHandle(hReadPipe)
          ret& = CloseHandle(hWritePipe)
      End Sub

3.      Add a CommandButton (Command1) to Form1.


justchat_1Connect With a Mentor Commented:
When you run the dos command use > and then a path to save the contents of the DOS window to a file which you can open with VB
marvelsoftAuthor Commented:
Solutions are both correct. Thank you very much for that. But the scenario is like this I've running program in DOS Window and then for my purpose is get those characters displayed and everytime I click a VB6 Button1 it save those characters in a particular file or variable. Is that possible?
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Im not sure I understand the problem... I thought both solutions did that...
marvelsoftAuthor Commented:
The DOS program is a database program. In order to automate the transfer of data/records to a new system I've to do this without any further actions of my encoders.
marvelsoftAuthor Commented:
I already tried your VB6 solution but it only capture the character right after the DOS program finished executing.Right?  But about capturing from it while the DOS program is running?
[ fanpages ]Connect With a Mentor IT Services ConsultantCommented:
To capture output from a program (in MS-DOS, or otherwise) whilst it is running, you can...

a) Pipe/Output (append) to a pre-defined file (using shared write access) [either via STDOUT or normal disk operating system commands] that another application can read from, or
b) Write records to a database available to all environments, and/or
c) Send inter-process communication messages (either via Windows Messaging, or network protocols, e.g. TCP/IP) from one application to another.

A simple method for this...

Invoke your MS-DOS application using this VB(A)/VB6 command:

Call Shell(Environ$("COMSPEC") & " /c c:\folder\subfolder\name_of_application.exe > c:\name_of_output_file.txt")

Then, whenever you wish to read the output, open the "c:\name_of_output_file.txt" file in your VB application.


I think I understand what your talking about... because the output file is buffered there is a delay between when the output is generated and when it is written to the file

Try option 3 that fanpages suggested if instant communication is needed
marvelsoftAuthor Commented:
Right let me try it... I'm trying to have a simple code just like a print sreen in DOS, I think this can be done to have a instant capture anytime I want to get characters from DOS Windows. A trigger from a remote/lan terminal can be an option to capture too while the source pc is in DOS mode.
Can you clarify "in DOS mode":
Do you mean running in dos mode or do you mean running windows with a dos shell open??
marvelsoftAuthor Commented:
Running Windows with a DOS Shell Open or Alt+Enter mode.

That DOS Shell can be maximize but sending Alt+Enter from there Press Print Screen and you will capture all characters from DOS Shell and pasted it in a notepad. Right?

By that method or process do you think it is possible to do that in a Visual Basic 6 program?

My program is just to get characters from DOS Shell. If we can do that in Alt+Enter mode DOS Shell much better or running windows with a DOS Shell Open that can be also.

How about doing just that:
send the keys to Shell(Environ("ComSpec") ) 'the name of the DOS Shell
marvelsoftAuthor Commented:
How can I send keys(print scrn key) to Shell(Environ("ComSpec") )?

How about sending "PRINT SCREEN" from VB while DOS shell is in full screen mode(Alt-Enter). By this process the characters is stored in clipboard and from there you can now manipulate the characters.

Thanks :)
[ fanpages ]IT Services ConsultantCommented:
PS. I use the "SnagIt" utility by TechSmith to capture text from a Window.

There is a trial version available, if you wish to use it:
[ ]



marvelsoftAuthor Commented:
fanpages and justchat_1 thanks you very much for wonderful answers and Patience.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.