[squeak-dev] FFI to call Mac Carbon functions

Levente Uzonyi leves at elte.hu
Sun May 8 22:11:45 UTC 2011


On Sun, 8 May 2011, Sean P. DeNigris wrote:

> I want to call DCSGetTermRangeInString[1]:
>
> CFRange DCSGetTermRangeInString (
>   DCSDictionaryRef dictionary,
>   CFStringRef textString,
>   CFIndex offset
> );
>
> The problem is I have no idea what to do about the return value and
> arguments. That is:
> <apicall: ??? 'DCSGetTermRangeInString' ???>
>
> How do I call this function using FFI? Where can I see an example?

You have to implement them (most of them). For example CFRange is a 
struct, so you have to create a subclass of ExternalStructure for it. You 
implement the class side #fields method which defines its fields, then 
perform the class side #defineFields method to create the accessors for 
them. For example CFRange is defined as[1]:

struct CFRange {
    CFIndex location;
    CFIndex length;
};
typedef struct CFRange CFRange;

So it has two fields, length and location, both have CFIndex as type.
CFIndex is signed long [2], so you'll implement #fields as:

CFRange class >> #fields

 	^#(
 		(length 'long')
 		(location 'long'))

Then evaluate [CFRange defineFields]. If this is done, then you can use 
the class CFRange in the FFI call like:

<cdecl: CFRange 'DCSGetTermRangeInString' (... ... long) module: ...>

You don't have to implement C structs which you won't use in Squeak, just 
pass around various C functions. In this case you can declare them as 
void* and their class will be ExternalData. Don't forget that you have to 
deallocate stuff allocated by C functions, to avoid memory leaks.


Levente

[1] http://developer.apple.com/library/mac/#documentation/CoreFoundation/Reference/CFBaseUtils/Reference/reference.html#//apple_ref/c/tdef/CFRange
[2] http://developer.apple.com/library/mac/#documentation/CoreFoundation/Reference/CFBaseUtils/Reference/reference.html#//apple_ref/c/tdef/CFIndex

>
> Thanks.
> Sean
>
> [1] Full doc at
> http://developer.apple.com/library/mac/#documentation/UserExperience/Reference/DictionaryServicesRef/Reference/reference.html
>
> --
> View this message in context: http://forum.world.st/FFI-to-call-Mac-Carbon-functions-tp3507842p3507842.html
> Sent from the Squeak - Dev mailing list archive at Nabble.com.
>
>



More information about the Squeak-dev mailing list