Jump to content
View in the app

A better way to browse. Learn more.

Tuts 4 You

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Capture Console output realtime to Memo in Delphi

Featured Replies

Posted

Hello.

I am trying to devlop a GUI app for a console app. I need some help. I want to capture real time text of console  in memo. How can i do it?

I tried some function that available of internet but there is problem it freez the app so anyone have any other idea 

delphi.wikia.com/wiki/Capture_Console_Output_Realtime_To_Memo

Its no working

Since you didn't provide any details what exactly "is no working", all I can say is - Google for "redirect handles windows", you'll gets lots of results for pretty much every programming language.

  • Author

Actually i used a procedure that's working fine but main problem is the program freeze untill the console app get closed. So is there any way to prevent app freezing from it? 

You didn't show how and from where you are calling that procedure. It should be called from separate thread to avoid blocking the whole ui. See TThread class and simple example at http://stackoverflow.com/a/3451813

Is that what you were looking for?

 

 

Hi you can try this function that capture the text of a console and  return the result  like a string, the code is from clubdelphi forum 

Example:

Spoiler

 


unit uConsole;

interface
uses Winapi.Windows, System.SysUtils,Vcl.Forms;

function CmdExec(Application:TApplication; Cmd: AnsiString): AnsiString;

implementation

function IsWinNT: boolean;
var
  OSV: OSVERSIONINFO;
begin
  OSV.dwOSVersionInfoSize := sizeof(osv);
  GetVersionEx(OSV);
  result := OSV.dwPlatformId = VER_PLATFORM_WIN32_NT;
end;

function CmdExec(Application:TApplication; Cmd: AnsiString): AnsiString;
var
  Buffer: array[0..4096] of AnsiChar;
  si: STARTUPINFOA;
  sa: SECURITY_ATTRIBUTES;
  sd: SECURITY_DESCRIPTOR;
  pi: PROCESS_INFORMATION;
  newstdin, newstdout, read_stdout, write_stdin: THandle;
  exitcod, bread, avail: Cardinal;
begin
  Result:= '';
  if IsWinNT then
  begin
    InitializeSecurityDescriptor(@sd, SECURITY_DESCRIPTOR_REVISION);
    SetSecurityDescriptorDacl(@sd, true, nil, false);
    sa.lpSecurityDescriptor := @sd;
  end
  else sa.lpSecurityDescriptor := nil;
  sa.nLength := sizeof(SECURITY_ATTRIBUTES);
  sa.bInheritHandle := TRUE;
  if CreatePipe(newstdin, write_stdin, @sa, 0) then
  begin
    if CreatePipe(read_stdout, newstdout, @sa, 0) then
    begin
      GetStartupInfoA(si);
      with si do
      begin
        dwFlags := STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW;
        wShowWindow := SW_HIDE;
        hStdOutput := newstdout;
        hStdError := newstdout;
        hStdInput := newstdin;
      end;
      Fillchar(Buffer, SizeOf(Buffer), 0);
      GetEnvironmentVariableA('COMSPEC', @Buffer, SizeOf(Buffer) - 1);
      StrCat(@Buffer,PAnsiChar(' /c ' + Cmd));
      if CreateProcessA(nil, @Buffer, nil, nil, TRUE, CREATE_NEW_CONSOLE, nil, nil, si, pi) then
      begin
        repeat
          PeekNamedPipe(read_stdout, @Buffer, SizeOf(Buffer) - 1, @bread, @avail, nil);
          if bread > 0 then
          begin
            Fillchar(Buffer, SizeOf(Buffer), 0);
            ReadFile(read_stdout, Buffer, bread, bread, nil);
            Result:= Result + String(PAnsiChar(@Buffer));
          end;
          Application.ProcessMessages;
          GetExitCodeProcess(pi.hProcess, exitcod);
        until (exitcod <> STILL_ACTIVE) and (bread = 0);
      end;
      CloseHandle(read_stdout);
      CloseHandle(newstdout);
    end;
    CloseHandle(newstdin);
    CloseHandle(write_stdin);
  end;
end;


end.
procedure TForm1.Button1Click(Sender: TObject);
var
  Data:AnsiString;
begin

  if Edit1.Text<>EmptyStr then
  begin
    Memo1.Clear;
    Data:=CmdExec(Application,Edit1.Text);
    Memo1.Text:=Data;
  end;

end;

The code was tried in Delphi Berlin 10

 

Edited by ElCuentista

  • Author
16 hours ago, kao said:

You didn't show how and from where you are calling that procedure. It should be called from separate thread to avoid blocking the whole ui. See TThread class and simple example at http://stackoverflow.com/a/3451813

Is that what you were looking for?

 

Yes,

Thanks a lot @kao

Now its working perfect my problem is solved.   

Create an account or sign in to comment

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.