[fpc-pascal] Code example needed, please!
ppkk at mail.ru
Thu Jun 29 12:07:01 CEST 2006
> A friend of me wants to write a program which uses a GUI frontend and a
> "backend", which actually does the work.
> The reason is that the backend shall also be usable from the command line,
> without any GUI, to make it working on more platforms without having to make
> changes on it. So it will be the best to compile it in FPC for the related
> OS, with a minimum of OS dependices.
> My idea is to write the backend in FPC. It shall only use StdIn/StdOut and
> also parse its command line options. The Frontend shall be - in a first step -
> be written in Delphi or Lazarus, but because I ask here for a friend who
> currently uses Delphi, this would be preferred.
> My question is:
> Can somebody show me a little sample program for that?
Well, I have written such a program (Delphi-FPC) not very long ago. I can't provide it as an example (and it is not very little), but I can describe necessary steps and provide code bits.
> It will be enough (for the Delphi-part) to open a window with a textbox and a
> button. When the button is clicked, the backend shall be loaded, some text be
> piped into it, and that what comes out from the backend's stdout shall be
> displayed in the textbox. Nothing more.
> The backend may be a plain FPC program (no delphi), reading from StdIn, making
> i.e. everything to UpperCase, then write this out to its StdOut.
> If somebody, please, could show me how this is done in Windows, please?
> Ing. Rainer Hantsch
Shame on me, but I have given up using pipes. They are not reliable in my opinion.
I used (of course, you can use command line parameters and files: frontend creates "param.txt", runs "backend.exe param.txt" and waits for output.txt to be created; no need to keep in mind something system specific) windows messages (and you have to create invisible dummy window for backend) and shared memory.
For your example it's possible (but it is windows specific)
for frontend: to create shared memory in frontend, zero the first byte of it and start backend (with parameters, if needed). When the button is pressed, the needed parameters and data are written to shared memory and after that frontend flips the switch (changes the first byte from zero to something else). After that the frontend periodically inspects that first byte value, and when the value is zero again, reads the output from shared memory.
for backend: to open shared memory (file), periodically inspect the first byte, read the parameters and data when the first byte becomes nonzero, process them, write output, change the first byte, close everything and die.
If it suits your needs, I can point you to the sources in the net and/or provide my code bits.
PS. I wanted to rewrite ipc unit for windows, so that this task will become less system specific, but didn't succeed: not enough time and confidence*experience; also I don't really know, how it works under linux.
More information about the fpc-pascal