[fpc-pascal] Code example needed, please!

Пётр Косаревский ppkk at mail.ru
Thu Jun 29 19:28:33 CEST 2006

From: Пётр Косаревский<ppkk at mail.ru>
To: FPC-Pascal users discussions <fpc-pascal at lists.freepascal.org>
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
                 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;

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(
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;

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.

More information about the fpc-pascal mailing list