-
April 19th, 2004, 12:46 PM
#1
Can I read the contents of a Console Window that I created?
I'm using a console window to run a command line program. The command line program outputs some text that I want to then read back in whats on the console screen. Sending the output from the console program to a text file is not an option as the console program may need input, or may not, or might pop up a windows style message box, there are too many variables. What I want to do is pretty much select everything within the console (which will always be plain text) and copy it into a text box. Is this possible? I'm creating the console window and have control over it and can interact with it. Is there a buffer I can read form to get what on the screen, like can I read from the screen buffer before its put onto the screen?
Allan.
-
April 20th, 2004, 03:47 AM
#2
If you are using AllocConsole to create the console window you can read from it with ReadConsoleOutput.
The problem is I don't know how to retrieve the handle of a console belonging to another process.
++++++++[>++++++++<-]>+.<+++[>++++<-]>+.<++[>-----<-]>.<+++[>++++<-]>++.<+++[>----<-]>-.----.
God does not play dice with the universe.(A.Einstein)
-
April 20th, 2004, 07:13 AM
#3
Since I created the console window I have a handle to it. What I actually did was created the console, then I set the consoles name to a specific name followed by the hWnd of my main form. Then I do a FindWindow call on the name to get the hWnd of the console window. It works perfectly at getting the handle. I'll have to do some research on ReadConsoleOutput but from what I fould so far it's looking promising (although everything I've found is for C). Hopefully I can find some decent VB examples and try them out. Thanks,
Allan.
-
April 20th, 2004, 11:30 AM
#4
hi,
are you using CreateProcess to start your console program?
if so, then to be able to catch the console (resp. your program's) output you must make the following (unfortunately i've made it only with C):
with few words: you should inherit the standart handles (there is a flag in CreateProcess) and use your own output handle.
=>
1. Set in the STARTUPINFO structure the dwFlags flag to
si.dwFlags = STARTF_USESTDHANDLES;
2. Save the standart output handle
HANDLE hSaveStdout = GetStdHandle(STD_OUTPUT_HANDLE);
3. Create an anonymous pipe that uses your handles (especially the output one):
BOOL ret = CreatePipe(&hYourStdIn, &hYourStdOut, &sa, 0);
4. Call your programm - CreateProcess
5. Read from your output handle with ReadFile
i hope, this helps.
kind of regards,
typecast
-
April 20th, 2004, 11:45 AM
#5
Andrea/Typecast,
I've gotten it to work exactly how I wanted. I'm using the API call ReadConsoleOutputCharacter. It allows you to read line by line the console as long as you can get a handle to it, and since I made it (see above) I can. Using this I'm pulling a big string out of it and then searching for what I need out of the string with a InStr function, then once I find what I want I'm using a Mid function to get the next line. Since the console is 80 characters per line I know that what I want is always 80 + my instr function. The code is below as I've seen lots of other people looking for this answer. If anyone sees any obvious faults let me know. Thanks for the replies!
Allan.
-- in a module --
The declare:
Private Declare Function ReadConsoleOutputCharacter Lib "kernel32" Alias "ReadConsoleOutputCharacterA" _
(ByVal hConsoleOutput As Long, ByVal lpCharacter As String, ByVal nLength As Long, ByVal dwReadCoord _
As Long, lpNumberOfCharsRead As Long) As Long
My Function:
Public Function GetConsoleBuffer(ByVal StartPosition As Long) As String
Dim sc_buffer As String * 30000 ' number of charaters to pull in?
Dim sc_numberread As Long
Call ReadConsoleOutputCharacter(hConsoleOut, sc_buffer, 30000, StartPosition, sc_numberread)
' get rid of begining and ending spaces
GetConsoleBuffer = LTrim(RTrim(sc_buffer))
End Function
-- on a form --
My call when I want the info:
' Dimension my variable for the console text
Dim tempstring As String
' Get console text
tempstring = GetConsoleBuffer(0) ' start at 0 or begining
' Do whatever you need with the string.
blah blah blah
Last edited by CrystalAnnoysMe; April 20th, 2004 at 11:48 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|