Recent

Author Topic: showing console program output in GUI app  (Read 6941 times)

Martin V

  • Full Member
  • ***
  • Posts: 139
showing console program output in GUI app
« on: August 06, 2016, 06:16:59 pm »
I have managed to redirect the output of a console program via named pipes to my gui app and to print it to a memo field. On Windows, I have used the Windows API functions:
- CreateNamedPipe
- ConnectNamedPipe
- I have run the console program synchronous via cmd.exe in an own thread, by using >pipename behind the command line parameters
- after running the external program, a 300 ms sleep in the main thread to get the time which is necessary to receive the rest of the output
- DisConnectNamedPipe in the own thread
- in the main thead a loop with ReadFile which gets exited if ReadFile results an error or 0 bytes read.

Meanwhile, the code works fine. But the named pipes concept seems to be very different in Linux, I have not found any platform-independent code in Freepascal/Lazarus for named pipes. There are only few API calls. Which are the correct Unix API calls? (I also plan to create my program on Mac OS) ?

Thank you for any hint.

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: showing console program output in GUI app
« Reply #1 on: August 06, 2016, 06:22:36 pm »
Maybe TProcess can do the same? I think that sort of general purpose class would be made cross-platform.

http://www.freepascal.org/docs-html/fcl/process/tprocess.input.html
or
http://www.freepascal.org/docs-html/fcl/process/tprocess.output.html

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: showing console program output in GUI app
« Reply #2 on: August 06, 2016, 06:27:05 pm »
Maybe TProcess can do the same?

Martin,

What he said.

I would scrap that entire approach and just use TProcess to run the console app and capture its output.

The "300 ms sleep" sounds like a failure waiting to happen. And doesn't that kind of defeat the purpose of running the console app in a separate thread if you're waiting for it to complete?

The Win API calls will likely tie your code to Windows. Start over would be my suggestion.

-Phil

Martin V

  • Full Member
  • ***
  • Posts: 139
Re: showing console program output in GUI app
« Reply #3 on: August 06, 2016, 09:11:31 pm »
Thank you for the tProcess.Output hint.

This page holds an explanation and a sample code:

http://wiki.freepascal.org/Executing_External_Programs#Reading_large_output

 

TinyPortal © 2005-2018