Technical Q&A QA1372

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.

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
2010-03-19

Removed the reference to bug (Radar #3823210) which has been closed, and original sample code description is correct.

2006-11-07

New document that identifies the runtime environment of the application. Useful only for plugins, frameworks, input methods, etc.