Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7921707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:41:11+00:00 2026-06-03T16:41:11+00:00

I want to get access to the buffer of another process console (via AttachConsole),

  • 0

I want to get access to the buffer of another process console (via AttachConsole), for calling ReadConsoleOutput, etc.

Is a DOS 16bit application. I can’t use pipes because it doesn’t writes output secuentially (it emulates “windows”.. like FAR commander if you know what I mean).

So I should:

1) launch the app
2) get the process id
3) call AttachConsole(ProcId)
4) call GetConsoleScreenBufferInfo to get the size
5) call ReadConsoleOutput

The problem is at 3: when I call AttachConsole ir returns 0, and after a call to GetLastError it reports ERROR_INVALID_PARAMETER 87 (0x57).

The only parameter of AttachConsole is the ProcessId and I’ve checked it with ProcessExplorer that is right (it’s actually the PID of ntvdm.exe that emulates the app).

Delphi code:

function AttachConsole(dwProcessId: DWORD): Cardinal; external kernel32 name 'AttachConsole';

var
  Handle: HWND;

function EnumWindowsProc(hwnd: HWND; lParam: LPARAM): BOOL; stdcall;
var
  s: string;
  IsVisible, IsOwned, IsAppWindow: Boolean;
begin
  Result := True;//carry on enumerating

  IsVisible := IsWindowVisible(hwnd);
  if not IsVisible then
    exit;

  IsOwned := GetWindow(hwnd, GW_OWNER)<>0;
  if IsOwned then
    exit;

  IsAppWindow := GetWindowLongPtr(hwnd, GWL_STYLE) and WS_EX_APPWINDOW<>0;
  if not IsAppWindow then
    exit;

  SetLength(s, GetWindowTextLength(hwnd));
  GetWindowText(hwnd, PChar(s), Length(s)+1);
  if AnsiContainsText(s, '????.EXE') then // set windows name to search
    Handle := hwnd;
end;

procedure Test(Strings: TStrings);
var
  ProcessID: Cardinal;
begin
  Handle := 0;
  EnumWindows(@EnumWindowsProc, 0);
  Strings.Add('Handle: ' + IntToStr(Handle));
  if Handle <> 0 then
    SetForegroundWindow(Handle);
  Sleep(100);

  GetWindowThreadProcessId(Handle, @ProcessID);
  Strings.Add('ProcessId: ' + IntToStr(ProcessID));

  if AttachConsole(ProcessId) <> 0 then
    Strings.Add('Ok Attached')
  else
    Strings.Add('Error: ' + IntToStr(GetLastError));
end;

Drop memo and button in form. At OnClick call Test(Memo1.Lines).

===== EDIT complete solution =====

function AttachAndGetConsoleHandle(ProcessId: Cardinal): Cardinal;
begin
  if not AttachConsole(ProcessId) then
    raise Exception.Create('AttachConsole error: ' + IntToStr(GetLastError));

  Result := GetStdHandle(STD_OUTPUT_HANDLE);

  if Result = INVALID_HANDLE_VALUE then
    raise Exception.Create('GetStdHandle(STD_OUTPUT_HANDLE) error: ' + IntToStr(GetLastError));
end;

procedure DettachConsole;
begin
  if not FreeConsole then
    raise Exception.Create('FreeConsole error: ' + IntToStr(GetLastError));
end;

function ReadConsole(ConsoleHandle: Cardinal): TStringList;
var
  BufferInfo: _CONSOLE_SCREEN_BUFFER_INFO;
  BufferSize, BufferCoord: _COORD;
  ReadRegion: _SMALL_RECT;
  Buffer: Array of _CHAR_INFO;
  I, J: Integer;
  Line: AnsiString;
begin
  Result := TStringList.Create;

  ZeroMemory(@BufferInfo, SizeOf(BufferInfo));
  if not GetConsoleScreenBufferInfo(ConsoleHandle, BufferInfo) then
    raise Exception.Create('GetConsoleScreenBufferInfo error: ' + IntToStr(GetLastError));

  SetLength(Buffer, BufferInfo.dwSize.X * BufferInfo.dwSize.Y);

  BufferSize.X := BufferInfo.dwSize.X;
  BufferSize.Y := BufferInfo.dwSize.Y;
  BufferCoord.X := 0;
  BufferCoord.Y := 0;
  ReadRegion.Left := 0;
  ReadRegion.Top := 0;
  ReadRegion.Right := BufferInfo.dwSize.X;
  ReadRegion.Bottom := BufferInfo.dwSize.Y;

  if ReadConsoleOutput(ConsoleHandle, Pointer(Buffer), BufferSize, BufferCoord, ReadRegion) then
  begin
    for I := 0 to BufferInfo.dwSize.Y - 1 do
    begin
      Line := '';
      for J := 0 to BufferInfo.dwSize.X - 1 do
        Line := Line + Buffer[I * BufferInfo.dwSize.X + J].AsciiChar;
      Result.Add(Line)
    end
  end
  else
    raise Exception.Create('ReadConsoleOutput error: ' + IntToStr(GetLastError));
end;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T16:41:17+00:00Added an answer on June 3, 2026 at 4:41 pm

    The definition should be:

    function AttachConsole(dwProcessId: DWORD): BOOL; stdcall; external
    kernel32 name 'AttachConsole';
    

    So the code following it should be:

    if AttachConsole(ProcessId) then
    

    Can’t help you anymore than that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to to get the access token from LinkedIn. First I send the
Using Access 2003 I want to get a table value from the two databases
i want to access or get the class which is present in different project.How
I want to get access to a IP range with this net feature: 68.241.45.0/20
<script> //in one script var someVarName_10 = 20; </script> I want to get access
I wonder how to solve the following problem: I want to get access to
I want to programmatically determine if the current user (or process) has access to
I am trying to get access to Form1’s public method on another form Form2
I want to get access to the page of network-setting from my own app.
I want to get access to all CSS properties (not only for a specific

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.