Loading Executable Code

The CFBundle Services function CFBundleLoadExecutable is provided so that you can load a bundle's main executable. This function locates, loads, and dynamically links code into the running application. Once the code is loaded, you can look up function pointers using the function CFBundleGetFunctionPointerForName .

The function CFBundleCopyExecutableURL locates and returns the URL for the main executable of the bundle. It returns the version of the executable appropriate for the current platform. Helper executables can be located using CFBundleCopyAuxiliaryExecutableURL , which takes as parameters a bundle reference and the name of the executable. Helper executables are stored in the same place as the main executable and are also platform specific. Helper executables might be useful for applications that require a command line tool. The tool can be packaged in the bundle's various platform executable directories and can be located with this function. This allows applications to ship with versions of the tool for each platform.

Listing 11 An example function for a loadable bundle
// Add one to the incoming value and return it. long addOne(short number) { return ( (long)number + 1 ); }

In order to call this function from your application, you must first load the code from the bundle, and then search for the desired function by name. The following code fragment illustrates this process.

Listing 12 Finding functions in a loadable bundle's code
// Typedef for the function pointer. typedef long (*AddOneFunctionPtr)(short number); // Function pointer. AddOneFunctionPtr addOne = NULL; // Flag that indicates the status returned when // attempting to load a bundle's executable code. Boolean didLoad = false; // Value returned from the loaded function. long result; // Try to load the executable from my bundle. didLoad = CFBundleLoadExecutable( myBundle ); // If the code was successfully loaded, look for our function. if (didLoad) { // Now that the code is loaded, search for // the function we want by name. addOne = (void*)CFBundleGetFunctionPointerForName( myBundle, CFSTR("addOne") ); // If our function was found in the loaded code, // call it with a test value. if (addOne) { // This should add 1 to whatever was passed in result = addOne ( 23 ); } // Unload the bundle's executable code. CFBundleUnloadExecutable( myBundle ); }

© 1999 Apple Computer, Inc. – (Last Updated 07 September 99)