Solved

Get Character From DOS Window

Posted on 2006-06-30
15
223 Views
Last Modified: 2010-04-07
Experts,

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.


Thanks:)
0
Comment
Question by:marvelsoft
  • 7
  • 5
  • 3
15 Comments
 
LVL 9

Assisted Solution

by:justchat_1
justchat_1 earned 100 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
0
 
LVL 35

Accepted Solution

by:
[ fanpages ] earned 400 total points
ID: 17017527
Hi,

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

      Private Type SECURITY_ATTRIBUTES
          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

      Private Type PROCESS_INFORMATION
         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)
       Else
           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.



BFN,

fp.
0
 

Author Comment

by:marvelsoft
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?
0
 
LVL 9

Expert Comment

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

Author Comment

by:marvelsoft
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.
0
 

Author Comment

by:marvelsoft
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?
0
 
LVL 35

Assisted Solution

by:[ fanpages ]
[ fanpages ] earned 400 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.

BFN,

fp.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 9

Expert Comment

by:justchat_1
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
0
 

Author Comment

by:marvelsoft
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.
0
 
LVL 9

Expert Comment

by:justchat_1
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??
0
 

Author Comment

by:marvelsoft
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.

Thanks:)
0
 
LVL 9

Expert Comment

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

Author Comment

by:marvelsoft
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 :)
0
 
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 ]

BFN,

fp.

0
 

Author Comment

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


:)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now