Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Initialize Cocoa/ObjC from a dynamically-loaded bundle?



I missed the beginning of this thread, so I apologize up front if my answer is ~not~ to the question raised.

I had to do this recently for a Finder plugin that I am working on. Not really wanting to mess with Carbon,
I had to figure out how to load Cocoa nibs.

It is (somewhat) trivial to do from Carbon applications. You need to keep in mind
that any method you will be calling from Carbon that is defined in the Cocoa bundle WILL need
a C wrapper.

On the carbon side of the application you need to do something like this to load the Cocoa bundle:

static void loadCocoaBundle(CFStringRef framework, CFBundleRef *bundlePtr)
{
CFBundleRef pluginBundle = NULL;
CFURLRef baseURL = NULL;
CFURLRef bundleURL = NULL;

pluginBundle = CFBundleGetBundleWithIdentifier( CFSTR("SVN_CMM") );
require(pluginBundle, CantFindMainBundle);

//Note that is the Bundle is somewhere other than the resource's directory, you
//will need to use the appropriate function to get the correct URL
baseURL = CFBundleCopyResourcesDirectoryURL(pluginBundle);
require(baseURL, CantCopyURL);

bundleURL = CFURLCreateCopyAppendingPathComponent(kCFAllocatorSystemDefault, baseURL, CFSTR("CocoaBundle.bundle"), false);
require(bundleURL, CantCreateBundleURL);

bundleRef = CFBundleCreate(NULL, bundleURL);

CFRelease(bundleURL);

CantCreateBundleURL:
CFRelease(baseURL);

CantCopyURL:
CantFindMainBundle:
return;
}

Once the bundle is loaded, to use it do something like this:

static OSStatus SVNPluginHandleSelection(void* thisInstance, AEDesc* inContext, SInt32 inCommandID )
{
OSStatus errorCode = noErr;

OSStatus (*initializeBundlePtr)(void);
void (*setArgumentsPtr)(CFArrayRef files);
void (*setCommandPtr)(int command);
void (*performOperationPtr)();

loadCocoaBundle(CFSTR("SCPluginCocoaBundle.bundle"), &bundleRef);
initializeBundlePtr = CFBundleGetFunctionPointerForName(bundleRef, CFSTR("initializeBundle"));
errorCode = (*initializeBundlePtr)();

setCommandPtr = CFBundleGetFunctionPointerForName(bundleRef, CFSTR("setCommand"));
(*setCommandPtr)(inCommandID);

setArgumentsPtr = CFBundleGetFunctionPointerForName(bundleRef, CFSTR("setArguments"));
(*setArgumentsPtr)((CFArrayRef)filesForCommand);

performOperationPtr = CFBundleGetFunctionPointerForName(bundleRef, CFSTR("performOperationOnFiles"));
(*performOperationPtr)();

[pool release];
return 0;
} // TerminalHerePluginHandleSelection


For a further example, look at SCplugin. The source is browse-able at http://svn.red-bean.com/scplugin/trunk
In particular look at the SCPlugin.m class.

Cheers,
cmp
email@hidden
http://www.pycs.net/cmp


On Jan 31, 2004, at 17:25, Cheng Wei wrote:

NSApplicationLoad();

Unfortunately, this is in Cocoa.framework. I can't even load a plug-in that links to Cocoa.framework for some reason.

check out Apple cocoa spelling check sample code.

you can manually load cocoa frame work, find NSApplicationLoad() symbol and call it.

http://developer.apple.com/samplecode/Sample_Code/OS_Utilities/ SpellingChecker-CarbonCocoa.htm

http://developer.apple.com/documentation/Cocoa/Conceptual/ CarbonCocoaDoc/index.html?http://developer.apple.com/documentation/ Cocoa/Conceptual/CarbonCocoaDoc/cci_chap1/chapter_1_section_1.html#// apple_ref/doc/uid/20001514
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >Initialize Cocoa/ObjC from a dynamically-loaded bundle? (From: Keith Bauer <email@hidden>)
 >Re: Initialize Cocoa/ObjC from a dynamically-loaded bundle? (From: Sherm Pendley <email@hidden>)
 >Re: Initialize Cocoa/ObjC from a dynamically-loaded bundle? (From: Keith Bauer <email@hidden>)
 >Re: Initialize Cocoa/ObjC from a dynamically-loaded bundle? (From: Cheng Wei <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.