[fpc-pascal] Library callback : how do I do that?

Lukasz Sokol el.es.cr at gmail.com
Thu Aug 8 12:34:53 CEST 2013


I am making a Pascal wrapper for a library, which states in its API description:

Note that the Callback function will be called from an independent child
thread, not from the ApplicationÂ’s Window thread. It should not therefore be
used to draw data directly to a Windows screen. The safest use of the callback
is to use a threadsafe interface (such as Windows messaging) to pass the
message to Windows for handling by the Windows thread.

and c(++) example

void Callback( int SerialNumber, int Event, int Data )
// Send the message to Windows thread for processing
PostMessage( NULL, WM_USER_MULTIPORTEVENT, Event,SerialNumber );
// Note: Data ignored. We can poll for an up-to-date
// value when this message is handled.

This would be fine, IFF i was adding this to the main form of my application...
but I'm not. I'm trying to wrap this .dll in a separate unit, only communicating
to the form through a wrapper object, defined in a separate unit.
The object itself, would be created /by/ the form window, but won't be a GUI object,
so won't be in the message queue;

Doing a chain like:

(form passes its handle to the object) -> (object installs callback function)
and then
(normal operation)
(form receives callback via Windows Message) -> (form calls e.g OnCallback function of the object)

seems a bit backwards.

I can imagine, that I need to have the Callback procedure as 

procedure Callback(Port, Event, Value : Integer); stdcall;

but how do I call functions/procedures of MyObject in a threadsafe manner from there?

(Lazarus 1.0.10 2013-06-09, FPC 2.6.2 rev. 41613 i386-win32-win32/win64)

Sorry if it's lame,
Kind Regards,

More information about the fpc-pascal mailing list