How can I identify the runtime environment, Carbon or Cocoa, of the current application?

Q: How can I identify the runtime environment, Carbon or Cocoa, of the current application?

A: You can identify the runtime environment of the current application by calling the ProcessInformationCopyDictionary API (in Processes.h) and examining the value of the "Flavor" key. This API is supported in Mac OS X v10.2 and later. Its description in the header incorrectly states "Cocoa applications have the value 4"; Cocoa applications have a flavor of 3 (Radar #3823210).

This is helpful if you're developing plugins, frameworks, input methods, static libraries, etc, when you will not always know in which runtime environment, Carbon, Cocoa, or even Mac OS Classic, your code is going to run. Most of the time, you would not care about this runtime environment but if you handle User Interface elements, some situations may arise when knowing the runtime environment would simplify the code writing.

For convenience, you can use the following function which returns the flavor of the running application:

Listing 1: GetApplicationFlavor.

SInt32 GetApplicationFlavor(void)
{
  // GetApplicationFlavor returns:
  //     -1 if the application flavor could not be identified
  //      0 if the application is a Mac OS Classic application
  //      2 if the application is a Carbon application
  //      3 if the application is a Cocoa application

  static SInt32 flavor = -1;
  OSStatus status;
  CFDictionaryRef processInfoDict = NULL;
  CFNumberRef processInfoFlavor = NULL;

  if (flavor == -1)
  {
    ProcessSerialNumber psn;
    status = GetCurrentProcess(&psn);
    require_noerr(status, GetCurrentProcess);

    processInfoDict = ProcessInformationCopyDictionary(&psn, kProcessDictionaryIncludeAllInformationMask);
    require(processInfoDict != NULL, ProcessInformationCopyDictionary);

    processInfoFlavor = CFDictionaryGetValue(processInfoDict, CFSTR("Flavor"));
    require(processInfoFlavor != NULL, CFDictionaryGetValue);

    CFNumberGetValue(processInfoFlavor, kCFNumberSInt32Type, &flavor);
  }

CFDictionaryGetValue:
ProcessInformationCopyDictionary:
GetCurrentProcess:

  if (processInfoFlavor != NULL)
    CFRelease(processInfoFlavor);
  if (processInfoDict != NULL)
    CFRelease(processInfoDict);

  return flavor;
}

Document Revision History

DateNotes
2006-11-07First Version

Posted: 2006-11-07


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.