On Tue, 22 Mar 2005 13:11:59 -0800, "Mark T." <[EMAIL PROTECTED]> wrote:
> >"Chris Maloof" <[EMAIL PROTECTED]> wrote in message >news:[EMAIL PROTECTED] >> Hello, >> >> Does anyone know how I can read the ASCII text from a console window >> (from another application) in WinXP? It doesn't sound like a major >> operation, but although I can find the window via pywin32, I haven't >> been able to do anything with it. I'd really just like to get the >> window text into a string. >> >> By "console window", I mean the sort of thing that comes up when you >> run "command" (although this particular one is for the game NetHack). >> >> Thanks, >> Chris > >Check out the Win32 Console functions on MSDN, such as AlocConsole(), >ReadConsoleOutput(), GetStdHandle(). I don't see an easy way to get the >screen buffer handle unless you are the process that owns the console, >however. > >Mark > Yup, this is pretty much closest to what ended up working, after some more research. There might have been an easier way, but for the archives, here's the meat of what I did (as seen also in the python-win32 mailing list): import win32process from ctypes import * import time # Constants from winbase.h in .NET SDK STD_INPUT_HANDLE = c_uint(-10) # (not used here) STD_OUTPUT_HANDLE = c_uint(-11) STD_ERROR_HANDLE = c_uint(-12) # (not used here) startup = win32process.STARTUPINFO() (hProcess, hThread, dwProcessId, dwThreadId) = \ win32process.CreateProcess("C:\myConsoleApp.exe", "-X", None, None, 0, 0, None, None, startup) time.sleep(1) #wait for console to initialize # Use ctypes to simulate a struct from the Win32 API class COORD(Structure): _fields_ = [("X", c_short), ("Y", c_short)] topleft = COORD(0,0) CHARS_TO_READ = 200 result = create_string_buffer(CHARS_TO_READ) count = c_int() windll.kernel32.AttachConsole(c_uint(dwProcessId)) inHandle = windll.kernel32.GetStdHandle(STD_INPUT_HANDLE) outHandle = windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE) errHandle = windll.kernel32.GetStdHandle(STD_ERROR_HANDLE) # See Win32 API for definition windll.kernel32.ReadConsoleOutputCharacterA(outHandle, result, CHARS_TO_READ, topleft, byref(count)) print result.value -- http://mail.python.org/mailman/listinfo/python-list