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

Lukasz Sokol el.es.cr at gmail.com
Thu Aug 8 13:15:44 CEST 2013


On 08/08/13 11:58, Henry Vermaak wrote:
> On Thu, Aug 08, 2013 at 11:34:53AM +0100, Lukasz Sokol wrote:
>> 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:
>>
>> (Setup) (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 has QueueAsyncCall, which is a thread safe, cross platform way
> of queueing method calls asynchronously.
> 
> E.g. for USB hotplug notification, I register this function with my
> device library:
> 
> 	procedure NotifyPlug(status: cint); cdecl;
> 	begin
> 	  Application.QueueAsyncCall(@MainForm.NotifyAsync, status);
> 	end;
> 
> Henry

Hello Henry,

but I would then need to make the wrapper object aware of the Application 
or its forms then?

I wanted the wrapper unit to be as self-contained as much as possible,
(so the callback function is defined in a unit that only has Classes, SysUtils and dynlibs as uses,
i.e. no Forms. Controls or Graphics)
but still be thread-safe w/r/t to where the wrapper object is created
(within a GUI form or /not/)

Unless I can do:

procedure Callback (...); stdcall;
begin
  {Application}.QueueAsyncCall(MyObject.CallbackNotify, ...);
end;

no?

Thanks,
Lukasz





More information about the fpc-pascal mailing list