2

While learning C# and Delphi Prism for use with monotouch is rewarding, I am looking for a way to re-use code written in ordinary Object Pascal. To simply re-write everything I have done in Delphi over 15 years is simply unacceptable.

So my question is twofold:

  1. If I for sake of argument have a library written in ordinary Object Pascal with no external dependencies and compiled with the ARM freepascal compiler for iPhone - could I use the resulting dylib from monotouch?
  2. If the above is correct, how do I declare and call these external functions from C#/Prism? How do i deal with passing record types etc. in mono?

I have noticed that other iPhone applications ship with their own dylib files (the SDL game library is a typical example). I find no reason why this should not work.

Jon Lennart Aasenden
  • 3,920
  • 2
  • 29
  • 44
  • note there is a `dylib` tag, maybe it attracts more readers – mjn Jan 05 '11 at 06:21
  • Why don't you try a web search? I suggest the following search terms: mono freepascal dylib – David Heffernan Jan 05 '11 at 11:07
  • @david: if you are ref. to "http://wiki.lazarus.freepascal.org/Using_Pascal_Libraries_with_.NET_and_Mono" then first of all it does not cover the ARM compiler and secondly it does not take weak linking into account. Thats why. – Jon Lennart Aasenden Jan 05 '11 at 12:42
  • @Jon OK, so what exactly is your problem? When you try this how does it fail? – David Heffernan Jan 05 '11 at 12:44
  • I am wondering if it can be done. Conceptualy there is no problem with this, but in real-life there will be practical issues. For instance passing unmanaged structures as pointer parameters. Then there is the freepascal compiler, which may have different req. on the ARM than on x86 (which is why i asked for feedback here). On the whole i think it is perfectly valid, aside from weak linking - the fpc wiki is straight forward. But i have heard that special care has to be taken when compiling for iOS. Hence i figured that this place would be a good place to ask. – Jon Lennart Aasenden Jan 05 '11 at 12:57

2 Answers2

3

I have not worked with monotouch, but Mono/.NET have a feature called P/Invoke (platform invoke).

Basically you declare your functions/procedures as extern in the managed code and add a DLLImport attribute.

In contrast to its name DLLImport imports functions/procedures from .so files on Linux and .dylib files as well.

http://www.mono-project.com/Interop_with_Native_Libraries

P/Invoke in Mono

Community
  • 1
  • 1
Jens Mühlenhoff
  • 14,565
  • 6
  • 56
  • 113
1

I know nothing of monotouch, and less about Mac relating to Arm devices, but:

If your lib uses mostly the cdecl calling convention, and monotouch could maybe call cdecl (C) functions over some native operation mechanism (P/Invoke or JNI like stuff), that would be a base principle that could work.

The same for structures, declare with {$packrecords C}.

You might also need to call two functions (IIRC FPC_INITIIALIZE, and another one to finalize) to initialize/finalize the FPC RTL before calling functions.

Anything above the plain C level, such as Delphi Objects and such probably not usable, and must be wrapped. Maybe objective Pascal objects are reusable (at least if monotouch understands objective C ones), but like most other Apple specific stuff, that is not my forte.

Marco van de Voort
  • 25,628
  • 5
  • 56
  • 89