Sign in to follow this  
choffstein

CFBundle

Recommended Posts

This is such a huge stretch, considering it is a Mac thing, but I am having trouble loading bundles. Here is my code:
#include <iostream>
#include <CoreFoundation/CoreFoundation.h>

using namespace std;

typedef void (*voidFuncPtr)();

void MyLoadPrivateFrameworkBundle (CFStringRef framework, 
                                CFBundleRef *bundlePtr)               
// 1
{
    CFURLRef    baseURL     = NULL;
    CFURLRef    bundleURL   = NULL;

    CFBundleRef myAppsBundle= NULL;
	
    if ( bundlePtr == NULL ) goto Bail;

    *bundlePtr = NULL;
    
	cout << "Getting main Bundle." << endl;
	myAppsBundle= CFBundleGetMainBundle();                              
	if ( myAppsBundle == NULL ) goto Bail;
    
	cout << "CopyPrivateFrameworksURL" << endl;
	baseURL = CFBundleCopyPrivateFrameworksURL (myAppsBundle);          
    if ( baseURL == NULL ) goto Bail;
	
	cout << "CreateCopyAppendingPathComponent." << endl;
    bundleURL = CFURLCreateCopyAppendingPathComponent (
                                kCFAllocatorSystemDefault, 
								baseURL,
                                framework,
                                false );   
	if ( bundleURL == NULL ) goto Bail;
	
	/*
	cout << "Get URL" << endl;
	bundleURL = CFURLCreateWithFileSystemPath(
                kCFAllocatorDefault, 
                CFSTR("BundleTest.bundle"),
                kCFURLPOSIXPathStyle,
                true );
	*/
	
	cout << "BundleCreate" << endl;
    *bundlePtr = CFBundleCreate (NULL, bundleURL);                     
	if ( *bundlePtr == NULL ) goto Bail;

	cout <<"Load executable." << endl;
	if ( ! CFBundleLoadExecutable (*bundlePtr) )                        
	{
		cout << "Failed to load executable." << endl;
		CFRelease (*bundlePtr);
		*bundlePtr = NULL;
	}

	Bail:   
		if ( bundleURL != NULL ) CFRelease (bundleURL);                     
		if ( baseURL != NULL ) CFRelease (baseURL);                         
}

int main()
{
    CFBundleRef myCFBundle;
	voidFuncPtr print = NULL; // pointer to the print() function in the shared lib
	
	MyLoadPrivateFrameworkBundle(CFSTR("BundleTest.bundle"), &myCFBundle);
	
	if(myCFBundle)
	{
		print = (voidFuncPtr) CFBundleGetFunctionPointerForName(myCFBundle, CFSTR("print"));
	
		if(print != NULL)
			print();
	}
	else
	{
		cout << "myCFBundle failed to load." << endl;
	}
	
	if(myCFBundle) CFRelease(myCFBundle);
		
	return 1;
}


I built it in XCode as a standard C++ tool that loads the Core Foundation framework so I can use CFBundle -- except it always fails at CreateBundle(). I have my bundle, "BundleTest.bundle" sitting in the application directory, so I know its there -- maybe it has something to do with how I created the bundle? So that is my question right now: how do I go about correctly creating a bundle? I have three options in XCode: CFPlugIn, Cocoa Bundle, and Carbon Bundle. The XCode info says that CFPlugIn is a Core Foundation Bundle, and it creates a .bundle -- but I would assume it is actually a CFPlugIn based on its name, which means I have to load it like a CFPlugIn, right? Man, this makes my head hurt. There is 0 good documentation on just creating a CFBundle. If I figure this out, I am so writing a tutorial.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this