Solved

Access Screen Buffer in Terminal Emulator

Posted on 2004-09-27
7
1,028 Views
Last Modified: 2010-08-05
I am going to try this here, one last time before giving up.  As an example, I want to (via Win API) access the session text displayed on the screen of a terminal emulator program.  I do not want to use SendKeys and the ClipBoard.  I know how to do that.  I am looking specifically how to accomplish this if it is possible, to capture the current screen at any given time.

I have tried to AttachConsole.... but get Access Denied error.
I have tried fooling with Pipes... but I probably don't know what I am doing, because I get locked up.

Here is the link to download the emulator.
http://www.mochasoft.dk
and the emulator version is...    W32 TN5250

Please provide means of doing this which I can accomplish from VB6 application.  

Please provide complete code with descriptions.  I may want to adapt the solution to other types of emulators, so I need to know what is happening.

If this is not possible, then I will be forced to revert to the clipboard.

Successful answer is worth more than 500 points, so if there is a way to award more, I will do it.  This is also asked in the MFC area, so successful solution should claim both.  (1000 points total)


0
Comment
Question by:vbPhil
7 Comments
 
LVL 8

Expert Comment

by:mladenovicz
ID: 12161959
maybe this can help

'Redirects output from console program to textbox.
'Requires two textboxes and one command button.
'Set MultiLine property of Text2 to true.
'
'Original bcx version of this program was made by
' dl <dl@tks.cjb.net>
'VB port was made by Jernej Simoncic <jernej@isg.si>
'Visit Jernejs site at http://www2.arnes.si/~sopjsimo/
'
'Note: don't run plain DOS programs with this example
'under Windows 95,98 and ME, as the program freezes when
'execution of program is finnished.

Option Explicit
Private Declare Function CreatePipe Lib "kernel32" (phReadPipe As Long, phWritePipe As Long, lpPipeAttributes As SECURITY_ATTRIBUTES, ByVal nSize As Long) As Long
Private Declare Sub GetStartupInfo Lib "kernel32" Alias "GetStartupInfoA" (lpStartupInfo As STARTUPINFO)
Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As Any, lpThreadAttributes As Any, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long
Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, lpOverlapped As Any) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Private Type SECURITY_ATTRIBUTES
  nLength As Long
  lpSecurityDescriptor As Long
  bInheritHandle As Long
End Type

Private Type PROCESS_INFORMATION
  hProcess As Long
  hThread As Long
  dwProcessId As Long
  dwThreadId 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 Byte
  hStdInput As Long
  hStdOutput As Long
  hStdError As Long
End Type

Private Type OVERLAPPED
    ternal As Long
    ternalHigh As Long
    offset As Long
    OffsetHigh As Long
    hEvent As Long
End Type

Private Const STARTF_USESHOWWINDOW = &H1
Private Const STARTF_USESTDHANDLES = &H100
Private Const SW_HIDE = 0
Private Const EM_SETSEL = &HB1
Private Const EM_REPLACESEL = &HC2

Private Sub Command1_Click()
  Command1.Enabled = False
  Redirect Text1.Text, Text2
  Command1.Enabled = True
End Sub
Private Sub Form_Load()
    Text1.Text = "ping"
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  If Command1.Enabled = False Then Cancel = True
End Sub

Sub Redirect(cmdLine As String, objTarget As Object)
  Dim i%, t$
  Dim pa As SECURITY_ATTRIBUTES
  Dim pra As SECURITY_ATTRIBUTES
  Dim tra As SECURITY_ATTRIBUTES
  Dim pi As PROCESS_INFORMATION
  Dim sui As STARTUPINFO
  Dim hRead As Long
  Dim hWrite As Long
  Dim bRead As Long
  Dim lpBuffer(1024) As Byte
  pa.nLength = Len(pa)
  pa.lpSecurityDescriptor = 0
  pa.bInheritHandle = True
 
  pra.nLength = Len(pra)
  tra.nLength = Len(tra)

  If CreatePipe(hRead, hWrite, pa, 0) <> 0 Then
    sui.cb = Len(sui)
    GetStartupInfo sui
    sui.hStdOutput = hWrite
    sui.hStdError = hWrite
    sui.dwFlags = STARTF_USESHOWWINDOW Or STARTF_USESTDHANDLES
    sui.wShowWindow = SW_HIDE
    If CreateProcess(vbNullString, cmdLine, pra, tra, True, 0, Null, vbNullString, sui, pi) <> 0 Then
      SetWindowText objTarget.hwnd, ""
      Do
        Erase lpBuffer()
        If ReadFile(hRead, lpBuffer(0), 1023, bRead, ByVal 0&) Then
          SendMessage objTarget.hwnd, EM_SETSEL, -1, 0
          SendMessage objTarget.hwnd, EM_REPLACESEL, False, lpBuffer(0)
          DoEvents
        Else
          CloseHandle pi.hThread
          CloseHandle pi.hProcess
          Exit Do
        End If
        CloseHandle hWrite
      Loop
      CloseHandle hRead
    End If
  End If
End Sub
0
 
LVL 2

Author Comment

by:vbPhil
ID: 12162368
That sample works with the example of "ping", but locks me up
when trying to use with the program that I specified...

Text1.Text = "C:\Mochasoft\mtn5250.exe"

Download the Eval of the Terminal Emulator and try it.  
Perhaps you can see from there what needs to be done.



0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12162409
Hi vbPhil,

The application in question is actually drawing the console screen using graphics calls.  You will not be able to capture the text from it.

I suppose it would be possible to do a screen capture and use OCR algorithms to convert the images to text but I don't think that is what you are looking for.

Regards,

Idle_Mind
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 2

Author Comment

by:vbPhil
ID: 12162465

Idle_Mind... Are you familiar with the application?  It does not appear to be Drawing the screen with Graphic calls, but rather looks as if the session window is the result of stdout, stdin functions in C/C++, such as printf etc.  That is why I assume there is a screen buffer that can be accessed.

0
 
LVL 32

Assisted Solution

by:Erick37
Erick37 earned 50 total points
ID: 12162481
From the program website:

"Mocha W32 TN5250 is a Windows 95/98/2000/NT/XP application."

i.e It is not a console application.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 450 total points
ID: 12162600
Trust me vbPhil, the application is drawing the console screen you see.

A terminal receives commands that tell the display what to do such as "change the color", "place the cursor at this location", or "output this letter".  Every terminal has its own protocol for doing this.

A emulator tracks this command stream and converts it to graphical output for you.

~IM
0
 
LVL 2

Author Comment

by:vbPhil
ID: 12162943
I got the message....

Just confused because MS Visual C++, app wizard allows you to create
a W32 Console Application... which from my flawed memory, seemed
to be running in a non-cmd style window.  I guess on more reflection
it probably was not.

0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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 …
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

743 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