Get Character From DOS Window

Posted on 2006-06-30
Medium Priority
Last Modified: 2010-04-07

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.

Question by:marvelsoft
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 3

Assisted Solution

justchat_1 earned 400 total points
ID: 17017328
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
LVL 35

Accepted Solution

[ fanpages ] earned 1600 total points
ID: 17017527

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

"Capturing the Output of a shelled program"
[ http://www.developersdomain.com/vb/articles/redirectpipe.htm]

And detailed on the Microsoft Support (Knowledge Base) site:
"How To Create a Process for Reading and Writing to a Pipe"
[ http://support.microsoft.com/kb/q173085/ ]

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.



Author Comment

ID: 17027371
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?
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 17027378
Im not sure I understand the problem... I thought both solutions did that...

Author Comment

ID: 17027383
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.

Author Comment

ID: 17027402
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?
LVL 35

Assisted Solution

by:[ fanpages ]
[ fanpages ] earned 1600 total points
ID: 17027422
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.



Expert Comment

ID: 17031889
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

Author Comment

ID: 17034468
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.

Expert Comment

ID: 17043859
Can you clarify "in DOS mode":
Do you mean running in dos mode or do you mean running windows with a dos shell open??

Author Comment

ID: 17045192
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.


Expert Comment

ID: 17045993
How about doing just that:
send the keys to Shell(Environ("ComSpec") ) 'the name of the DOS Shell

Author Comment

ID: 17056264
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 :)
LVL 35

Expert Comment

by:[ fanpages ]
ID: 17056716
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:
[ http://www.techsmith.com/snagit.asp ]




Author Comment

ID: 17060636
fanpages and justchat_1 thanks you very much for wonderful answers and Patience.


Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month13 days, 8 hours left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question