On Feb 3, 2011, at 9:38 PM, Greg Parker wrote:

> On Feb 3, 2011, at 6:18 PM, Jeff Kelley wrote:
>> I’m trying to use objc_msgSend_stret to call -bounds on a UIScreen. I have 
>> the following code:
>> 
>>> SEL bounds = NSSelectorFromString(@"bounds");
>>> CGRect screenBounds;
>>> objc_msgSend_stret((void *)&screenBounds, screen, bounds);
>> 
>> The declaration of objc_msgSend_stret in the Xcode code sense pop-up is as 
>> follows:
>> 
>>> void objc_msgSend_stret(void * stretAddr, id self, SEL op, ...);
>> 
>> But that’s not what message.h would leave me to believe (with some unrelated 
>> lines removed):
>> 
>>> #if defined(__OBJC2__)
>>> OBJC_EXPORT void objc_msgSend_stret(id self, SEL op, ...);
>>> #else
>>> /* For compatibility with old objc-runtime.h header */
>>> OBJC_EXPORT void objc_msgSend_stret(void * stretAddr, id self, SEL op, ...);
>>> #endif
>> 
>> So, if the declaration is actually the first (I’m compiling for iOS 4.2, so 
>> I’m pretty sure __OBJC2__ is defined), how do you use it and get the return 
>> value of the function?
> 
> Both declarations are wrong: you cannot and must not call them directly. The 
> declaration of objc_msgSend_stret() cannot be expressed in C.
> 
> The correct way to call objc_msgSend_stret() or any other objc_msgSend() 
> variant is to cast it to an appropriate function pointer type. Like this:
> 
>     CGRect (*msgSendFn)(id self, SEL _cmd);
>     msgSendFn = (CGRect(*)(id, SEL))objc_msgSend_stret;
>     CGRect screenBounds = msgSendFn(screen, @selector(bounds));
> 
> If the method had parameters, you would need to add them to the declaration 
> of the function pointer variable.
> 
> 
> -- 
> Greg Parker     gpar...@apple.com     Runtime Wrangler


That worked like a charm. Thanks, and don’t worry—this isn’t going in shipping 
code. :)

Jeff Kelley
slauncha...@gmail.com

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to