< Previous PageNext Page > Hide TOC

Using Carbon and Cocoa in the Same Application

It has always been possible to integrate Cocoa and Carbon functionality in the same application, at least when it comes to functionality that doesn’t handle user interface elements.

Contents:

Using Carbon in a Cocoa Application
Using Cocoa in a Carbon Application


Using Carbon in a Cocoa Application

For a Cocoa application to call Carbon functions, the only requirements are that the compiler has access to the appropriate header files and the application is linked against the appropriate frameworks. To access Carbon functionality, you can simply import Carbon.h and link against the Carbon framework. Since Objective-C is a superset of ANSI C, calling Carbon functions from a Cocoa application is easy (although prior to Mac OS X v10.2 the functions could not be user interface functions).

Using Cocoa in a Carbon Application

A Carbon application, by taking a few extra steps, can use many Cocoa and Objective-C technologies. To access Cocoa functionality, you can simply import Cocoa.h and link against the Cocoa framework. To access other Objective-C technologies, you may need to import additional headers and link against additional frameworks. For example, to use Web Kit, you need to import WebKit.h and link against the WebKit framework.

You also need to take these steps:

Using C-Callable Wrapper Functions

When integrating Objective-C code into a Carbon project, a common approach is to write C-callable wrapper functions (or simply C-wrapper functions) for the Objective-C portion of the code. In the context of Carbon and Cocoa integration, a C-callable wrapper function is a C function whose body contains Objective-C code that allows data to be passed to or obtained from Cocoa. These C-wrapper functions can be placed in a separate Objective-C source file and compiled using the Objective-C compiler.

Let’s look at a typical scenario for a Carbon application that accesses functionality provided in a Cocoa source file. The Cocoa source must contain all the necessary Objective-C code for its classes and methods. It must also contain a C-callable wrapper function for each method whose functionality is needed by the Carbon application. For example, for a changeText: method that takes a string and manipulates it in some way, the C-callable wrapper function would look similar to the following:

OSStatus changeText (CFStringRef message)
{
    NSAutoreleasePool *localPool;
 
    localPool = [[NSAutoreleasePool alloc] init];
    [[Controller sharedController] changeText:(NSString *)message];
    [localPool release];
    return noErr;
}

Note: The C-wrapper function usually allocates and initializes an NSAutoreleasePool object and then releases it when it is no longer needed, as shown in the changeText function. This is a requirement for C-wrapper functions that are called directly by your Carbon application, in order to achieve correct memory management.

In summary, here’s how C-callable wrapper functions are used to allow Carbon applications to access Cocoa functionality:

  1. Access to the Cocoa functionality is provided in an Objective-C source file. The file contains C-callable wrapper functions for any Cocoa method that’s needed by the Carbon application.

  2. The Carbon application invokes the C-wrapper functions as needed.



< Previous PageNext Page > Hide TOC


© 2002, 2007 Apple Inc. All Rights Reserved. (Last updated: 2007-10-31)


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.