GautamGreat Posted September 10, 2016 Posted September 10, 2016 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
kao Posted September 10, 2016 Posted September 10, 2016 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. 1
GautamGreat Posted September 10, 2016 Author Posted September 10, 2016 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?
kao Posted September 10, 2016 Posted September 10, 2016 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?
ElCuentista Posted September 11, 2016 Posted September 11, 2016 (edited) 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 September 11, 2016 by ElCuentista
GautamGreat Posted September 11, 2016 Author Posted September 11, 2016 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.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now