11

I'm working on a component that should be shared between Delphi and C++Builder, so I'm using Pascal as the lingua franca. Because I don't have Delphi on my computer at home, I first created the component in the Lazarus IDE. Now I "ported" it to Delphi and found an astonishing syntax problem:

This compiles with FPC (but not Delphi):

FSync.FSyncMethod := @SyncCheckInput;

This compiles with Delphi (but not FPC):

FSync.FSyncMethod := SyncCheckInput;

How can I share a unit between Lazarus and Delphi despite this syntactic divergence?

Wolf
  • 9,679
  • 7
  • 62
  • 108
  • "*a lack of a common (and portable) synchronizing mechanism*" - FreePascal mimics many of Delphi's core classes, include `TThread` and its `Synchronize()` method: [FreePascal Wiki | Multithreaded Application Tutorial | The TThread Class](http://wiki.freepascal.org/Multithreaded_Application_Tutorial#The_TThread_Class) – Remy Lebeau Oct 22 '15 at 22:20
  • @RemyLebeau I needed a non-blocking synchronisation mechanism and I got it with (the thread-safe) `Application.QueueAsyncCall` in Lazarus and `PostMessage` to an invisible Window (via `AllocateHWnd`) in Delphi. Of course, I used the `TThread` class, but `Synchronize` was not helpful in my case because of its rendezvous feature. Thanks anyway :) – Wolf Oct 23 '15 at 07:31
  • `TThread` also has an asynchronous `Queue()` method. – Remy Lebeau Oct 23 '15 at 17:30
  • @RemyLebeau I'm afraid there is no `Queue` method in Delphi 4, that I have to use (I'm not sure, I've no D4 or its docs at hand now). So I better remove the sidenote from the question or be more specific about the versions I'm using? – Wolf Oct 23 '15 at 20:06
  • You did not say which Delphi/FreePascal version you are using. No, `Queue()` does not exist in D4. FreePascal mimics D7. – Remy Lebeau Oct 24 '15 at 00:26
  • I removed the Note about synchronisation mechanisms. BTW: `TThread.Queue` does currently not exist in the LCL. – Wolf Oct 24 '15 at 08:50

1 Answers1

15

Insert this at the beginning of your units:

{$IFDEF FPC}{$MODE DELPHI}{$ENDIF}

This will instruct FreePascal to use the Delphi dialect to compile the unit. Delphi will ignore the {$MODE DELPHI} directive because FPC is not defined.

You can then use this

FSync.FSyncMethod := SyncCheckInput;

for setting events dynamically.

Free Consulting
  • 4,300
  • 1
  • 29
  • 50
Wosi
  • 41,986
  • 17
  • 75
  • 82