1 line
3.8 KiB
C++
Executable File
1 line
3.8 KiB
C++
Executable File
/* Copyright (c) 2017, Computer History Museum
|
|
All rights reserved.
|
|
Redistribution and use in source and binary forms, with or without modification, are permitted (subject to
|
|
the limitations in the disclaimer below) provided that the following conditions are met:
|
|
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
|
|
disclaimer in the documentation and/or other materials provided with the distribution.
|
|
* Neither the name of Computer History Museum nor the names of its contributors may be used to endorse or promote products
|
|
derived from this software without specific prior written permission.
|
|
NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE
|
|
COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
|
|
DAMAGE. */
|
|
|
|
#include "MachOWrapper.h"
|
|
|
|
#include <Gestalt.h>
|
|
#include <Folders.h>
|
|
|
|
OSStatus LoadFrameworkBundle ( CFStringRef framework, CFBundleRef *bundlePtr ) {
|
|
// This routine finds a the named framework and creates a CFBundle
|
|
// object for it. It looks for the framework in the frameworks folder,
|
|
// as defined by the Folder Manager. Currently this is
|
|
// "/System/Library/Frameworks", but we recommend that you avoid hard coded
|
|
// paths to ensure future compatibility.
|
|
//
|
|
// You might think that you could use CFBundleGetBundleWithIdentifier but
|
|
// that only finds bundles that are already loaded into your context.
|
|
// That would work in the case of the System framework but it wouldn't
|
|
// work if you're using some other, less-obvious, framework.
|
|
OSStatus err;
|
|
FSRef frameworksFolderRef;
|
|
CFURLRef baseURL;
|
|
CFURLRef bundleURL;
|
|
|
|
*bundlePtr = nil;
|
|
baseURL = nil;
|
|
bundleURL = nil;
|
|
|
|
// Find the frameworks folder and create a URL for it.
|
|
err = FSFindFolder ( kOnAppropriateDisk, kFrameworksFolderType, true, &frameworksFolderRef );
|
|
if ( err == noErr ) {
|
|
baseURL = CFURLCreateFromFSRef ( kCFAllocatorSystemDefault, &frameworksFolderRef );
|
|
if ( baseURL == nil )
|
|
err = coreFoundationUnknownErr;
|
|
}
|
|
|
|
// Append the name of the framework to the URL.
|
|
if ( err == noErr ) {
|
|
bundleURL = CFURLCreateCopyAppendingPathComponent ( kCFAllocatorSystemDefault, baseURL, framework, false );
|
|
if ( bundleURL == nil )
|
|
err = coreFoundationUnknownErr;
|
|
}
|
|
|
|
// Create a bundle based on that URL and load the bundle into memory.
|
|
// We never unload the bundle, which is reasonable in this case because
|
|
// the sample assumes that you'll be calling functions from this
|
|
// framework throughout the life of your application.
|
|
if ( err == noErr ) {
|
|
*bundlePtr = CFBundleCreate ( kCFAllocatorSystemDefault, bundleURL );
|
|
if ( *bundlePtr == nil )
|
|
err = coreFoundationUnknownErr;
|
|
}
|
|
|
|
if ( err == noErr ) {
|
|
if ( !CFBundleLoadExecutable ( *bundlePtr ))
|
|
err = coreFoundationUnknownErr;
|
|
}
|
|
|
|
// Clean up.
|
|
if ( err != noErr && *bundlePtr != nil) {
|
|
CFRelease ( *bundlePtr );
|
|
*bundlePtr = nil;
|
|
}
|
|
|
|
if ( bundleURL != nil )
|
|
CFRelease ( bundleURL );
|
|
if ( baseURL != nil )
|
|
CFRelease ( baseURL );
|
|
|
|
return err;
|
|
}
|