Code example needed, please!

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Code example needed, please!

Rainer Hantsch
Hello!

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?

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?


mfg

  Ing. Rainer Hantsch

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Code example needed, please!

Пётр Косаревский
> 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.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Code example needed, please!

Alexey Pavluchenko
In reply to this post by Rainer Hantsch
Hello Rainer,

Thursday, June 29, 2006, 11:42:12 AM, you wrote:

[skip]
RH> My idea is to write the backend in FPC. It shall only use StdIn/StdOut and
RH> also parse its command line options. The Frontend shall be - in a first step -
RH> be written in Delphi or Lazarus, but because I ask here for a friend who
RH> currently uses Delphi, this would be preferred.

RH> My question is:
RH> Can somebody show me a little sample program for that?

RH> It will be enough (for the Delphi-part) to open a window with a textbox and a
RH> button. When the button is clicked, the backend shall be loaded, some text be
RH> piped into it, and that what comes out from the backend's stdout shall be
RH> displayed in the textbox. Nothing more.

You can find a toy example (which basically does exactly what you
need) of using unnamed pipe in Delphi here:

http://delphi.about.com/cs/adptips2001/a/bltip0201_2.htm

--
Best regards,
 Alexey


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Code example needed, please!

Rainer Hantsch
In reply to this post by Пётр Косаревский
Hello!

Thank you very much for your answer. It is not that complicated as you
possibly assume. It is primarily an execution of four very little programs
which will run less than 1 second, each. (compiler, optimizer, linker,
assembler).

The primary point is that the textmode-backend is fully hidden and that its
output can be shown embedded in the delphi app, nothing more. Shared memory is
a little bit problematic because I want to completely separate the backend
from the frontend. The backend must also work without a frontend in a command
line mode, so I can only use StdIO and command line args for controlling it.


| 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.

Great! Code bits are very welcome!

  Ing. Rainer Hantsch

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Code example needed, please!

Leonardo M. Ramé
In reply to this post by Rainer Hantsch
Rainer,
If the application is a standalone one you can create the backend in a dll. The front end can be a
windows application who share the dll.

Later, if you want to evolve to a Client/Server architecture, you can create a service/daemond
linked to the dll, the daemond must have a protocol to connect with clients.



Leonardo M. Ramé
http://leonardorame.blogspot.com

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com 
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re[2]: Code example needed, please!

Пётр Косаревский
In reply to this post by Rainer Hantsch
Oh.

I didn't succeed with pipes.

But you can use exchange via files (so that frontend will tell parameter like user in command line).

Or (a worse idea) use special parameter to tell, that the frontend is calling (well, it's not separating, it's rather hiding from user).

I have code only with window messages and shared memory (if you don't want it, tell; otherwise I'll post some about an hour later).
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re[3]: Code example needed, please!

Пётр Косаревский
From: Пётр Косаревский<[hidden email]>
To: FPC-Pascal users discussions <[hidden email]>
Subject: Re[2]: [fpc-pascal] Code example needed, please!
> I have code only with window messages and shared memory (if you don't want it, tell; otherwise I'll post some about an hour later).

I used something like that for shared memory in Delphi

ShMemFile is THandle, ShMemBuf is Pointer.

     ShMemFile := CreateFileMapping(
                 INVALID_HANDLE_VALUE, <-- this creates file somewhere in memory or swapfile
                 nil,
                 PAGE_READWRITE,
                 0,
                 ShMemSize,   <-- set what you need
                 ShMemName);  <-- this name will be used by the backend too!! try to make it unique string, you can set it as command line parameter for backend
     if (ShMemFile=0) or (ShMemFile=INVALID_HANDLE_VALUE) then
       begin Application.MessageBox('Shared memory error 1.','Internal error',MB_OK); Close(); end;

     ShMemBuf := MapViewOfFile(ShMemFile, FILE_MAP_ALL_ACCESS, 0, 0, 0);
     if (ShMemBuf=nil) then
       begin Application.MessageBox('Shared memory error 2.','Internal error',MB_OK); CloseHandle(ShMemFile); Close(); end;
     {}
     ShMemP:=ShMemBuf;

Where ShMemP is pointer to array[0..ShMemSize-1] of byte, so that you can address bytes as ShMemP^[x] (somewhat lame).

And in the backend:
The types of ShMemFile and ShMemBuf are as in Delphi

ShMemFile := OpenFileMapping(
                             FILE_MAP_ALL_ACCESS,
                             false,
                             ShMemName);
if (ShMemFile=0) or (ShMemFile=INVALID_HANDLE_VALUE) then
  begin writeln('File open failed!'); ASS_BACK(MsgFileReady); end;

ShMemBuf := MapViewOfFile(ShMemFile, FILE_MAP_ALL_ACCESS, 0, 0, 0);

if ShMemBuf=nil then
  begin writeln('File open failed!'); ASS_BACK(MsgFileReady); end;
ShMemBytes:=ShMemBuf;

Where ShMemBytes is byte pointer (so you address bytes as ShMemBytes[x]).

This code creates one chunk of shared memory of ShMemSize size. I used about 64K. Probably, anything up to gigabyte will work, but may be slow.

All these functions are described in MSDN, the only thing not nice is that you have to guess some default values and pascal types (it's the same in Delphi help: the examples are in C, if I remember right).

If you plan sending window messages, I can send some working code (ask). There are examples (for Delphi) in internet on sites like http://delphi.about.com/od/windowsshellapi/l/aa093003a.htm.

Of course, all the functions are described in MSDN.

There are simple hints: the window handle is Application.handle (you can send it to backend, so that the dialog will not be broadcast from the beginning, also you have to write message hookers in both programs [and create dummy window in FPC one], if you want them to not freeze for some time, use the functions windows.peekmessage, windows.postmessage, windows.translatemessage, windows.dispatchmessage; if I remember right, one of them could have name conflict with something in another unit).

If you want some little fun, broadcast WM_KILL message.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re[2]: Code example needed, please!

Пётр Косаревский
In reply to this post by Leonardo M. Ramé
> If the application is a standalone one you can create the backend in a dll. The front end can be a
> windows application who share the dll.

Well, I didn't check it today, but two months ago it was extremely buggy (crashes). There were about 5 open bugs regarding this situation and .dll generation was marked "(not supported)".

So, it's not very good advice.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal