Re: C functions
Re: C functions
- Subject: Re: C functions
- From: Uli Kusterer <email@hidden>
- Date: Fri, 18 Oct 2013 19:39:39 +0200
On Oct 18, 2013, at 6:15 PM, Uli Kusterer <email@hidden> wrote:
> On Oct 18, 2013, at 4:48 AM, Shane Stanley <email@hidden> wrote:
>> is there any way to build a call to a C function on the fly? I mean something like pass a string to a method, and have it call the function of that name?
>
>
> Short: No. Long: Maybe.
>
> 1) You can put a function in a dynamic library and export it, and load that library. If you know its signature, you can then call it.
>
> 2) You can build a look-up table of name -> function pointer mappings inside your application, then look up the function pointer by name and call it. You'd need to know its signature then, too.
>
> 3) You can build a dynamic library that has this function as its main entry point and call that. Essentially a variant of #1. You'll still need to know its signature beforehand then
>
> 4) You can use a library like libffi to call a function pointer (using the methods in #1 through #3 to get one from a name) even without knowing its calling conventions beforehand. As long as you know the signature the moment you're calling it and have the proper values, that'll work (this is what you'd do if you wanted to make e.g. CoreFoundation APIs accessible to a scripting language)
>
> Cheers,
> -- Uli Kusterer
> "The Witnesses of TeachText are everywhere..."
> http://www.zathras.de
Oh, one more thing: Instead of dlsym(), you can also use CFBundleGetFunctionPointerForName() and its cohorts might also be useful for this if you want to go a bit more high-level. But even then, it needs to be an exported symbol in a dylib, can't be just any internal function.
Cheers,
-- Uli Kusterer
"The Witnesses of TeachText are everywhere..."
http://www.zathras.de
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden