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.

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;
}

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.