I have a console that is running and I need to get the output. I cannot use startprocess
to start the console as it is spawned separately. I do not have access to the source code, I am simply trying to redirect the output from the console while it is already running.
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
you need to read https://support.microsoft.com/en-us/help/318804/how-to-set-a-windows-hook-in-visual-c-net Specifically the bottom portion
It turns out that attaching to already running separate process using managed framework is not possible.
However, It is possible to achieve this using
Console Api Functions
underkernel32.dll
.Edit: The code is Improved for better usability
In order to achieve this we need to use
FreeConsole
,AttachConsole
,ReadConsoleOutputCharacter
,GetConsoleScreenBufferInfo
andAttachConsole
fromWinApi
Declarations of static external libraries:
We first need to free current console handle because we can only Attach to a single Console
Improvements
ref short currentPosition
added toReadALineOfConsoleOutput
function for synchronizing currentPosition of the Standard OutputGetConsoleScreenBufferInfo
is used to getlineSize
of the consoleshort lineSize = outInfo.dwSize.X
is added for lineSizeuint numberofLinesToRead = (uint) (outInfo.dwCursorPosition.Y - currentPosition)
is used to calculate number of lines to be read using difference between actual position of the console and current position of the cursor.lpNumberOfCharsRead
to avoid garbage line endings