Re: Newbie: Passing a pointer to a function to a plugin
Re: Newbie: Passing a pointer to a function to a plugin
- Subject: Re: Newbie: Passing a pointer to a function to a plugin
- From: Dustin Voss <email@hidden>
- Date: Sat, 24 Jul 2004 12:53:49 -0700
On 24 Jul, 2004, at 10:44 AM, Chris Griffin wrote:
I am very new to cocoa programming. I have a plugin that needs to call
back into a cocoa program. How should I declare the call back
function? Currently it is part of a implementation. Is there such a
thing as static as in C++? How do I pass the address of a function in
cocoa? Thanks.
You should have provided a bit more information, specifically, are you
writing the plug-in or the application? What language is the Cocoa app,
and what language is the plug-in? And how is the plug-in packaged, as a
bundle or library?
But let me try to answer as best I can. Plug-ins always specify what
the functions or methods called by the plug-in should look like.
If the plug-in says to use a C function, then you, as the app writer,
should make a C function. C functions in Objective-C code are declared
and passed just like C functions in C code. Typically, the
implementations of C call-back functions in an Objective-C Cocoa app
will examine the parameters they were given and find an appropriate
Objective-C object to handle the plug-in's request, then they call
methods of the Objective-C object to actually perform the request.
Here is a sample implementation of a C call-back function in an
Objective-C Cocoa app. It would go in a .m file. I usually put them
outside of the @implementation...@end block:
void UpdateProgress (void *pluginHandle, int percentage)
{
// Get the Objective-C object that handles requests from a given
plug-in instance.
id objCObject = [globalPlugInDictionary objectForKey:[NSValue
valueWithPointer:pluginHandle]];
[objCObject updateProgress:percentage];
}
The dictionary has to be global because this is a C function and is not
associated with any Objective-C object. Putting this function inside an
@implementation block won't fix that, either. The run-time has no idea
which instance this function is being called on.
C++ does have static, but I don't see how that applies here. C/C++
functions and global variables can be static (made private by the
linker) or extern (made public by the linker). C and C++ functions are
extern by default, which is what you'd want for a call-back. Or are you
referring to making local variables static, meaning the value doesn't
change between function invocations? Yes, C++ has that, too.
In Objective-C, you pass the address of a C function just like you do
in C. You don't pass the address of a method; Objective-C uses
selectors or NSInvocation for that. (Technically, you can pass the
address of the method, but that involves the Objective-C run-time and
isn't what you want.)
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.