On Jan 31, 2012, at 1:31 PM, Howard Moon wrote:

>       How does one call a Cocoa function from C++ code?
> 
>       I've got an existing .cpp file that leverages the vstgui framework, 
> which includes support for Cocoa in the underlying layers.  However, vstgui 
> does not support dialogs or alerts. My code already supports native Windows 
> dialogs/alerts, and Carbon windows/alerts, as needed, and I'd like to add 
> support for Cocoa when my audio plug-in is compiled as 64-bit on the Mac.
> 
>       One of my current functions is something like this:
> 
> void MyObj::showAlert( char* text, char* caption )
> {
> #ifdef WIN32
>       // calls ShowMessage
> #else
>       // calls CreateStandardAlert and RunStandardAlert
> #endif
> }
> 
>       I'd like to add the ability to support Cocoa, by adding a .mm/.h file 
> with the Cocoa code to display an NSAlert, and call it from here when the 
> preprocessor symbol MAC_COCOA is defined.  I've created the Cocoa files, but 
> how can I include and call into that code from my .cpp file?  Simply adding 
> either #import or #include of my new .h file causes many many errors, even 
> though the .mm file itself compiles fine.
> 
>       Anyone know of a very simple example showing what I need to do here?  
> All the examples I find so far show making the call from a .mm file in the 
> first place, but I can't do that because my code is also compiled for 32-bit 
> Carbon (and Windows). How do I call from a .cpp file into a .mm file?

Put this at the top of your .h file:

#ifdef __cplusplus
extern "C" {
#endif

and this at the bottom:

#ifdef __cplusplus
}
#endif

Then, just wrap all the Cocoa method calls you want to make in plain C 
functions, and restrict all usage of Objective-C to the .m, leaving the .h as 
plain C.

Charles
_______________________________________________

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

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

Reply via email to