[futurebasic] Re: [FB] DyLib

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : June 2014 : Group Archive : Group : All Groups

From: Brian S <fblistserve@...>
Date: Sat, 31 May 2014 18:47:55 -0700
Robert Covington wrote:

> Possible to access a Cocoa framework DyLib type dynamic library built in Xcode, in an FB project?

Recently, I wrote some code to access a compiled bundle of Bernie’s FBLog functions ( compiled in Xcode so when you call the functions you just have the headers and the actual compiled code is in a bundle ). A bundle is not a framework but both have a compiled unix executable that can be loaded. A framework might be similar enough to use the same approach. Some notes:

(1) The bundle was built as a 32-bit bundle in Xcode because 64-bit wouldn’t work ( which is predictable given FB’s roots )
(2) Since a bundle is not linked into the binary, it must be loaded and pointers created to point
to the various functions in it. The simple POC shows it’s possible and isn’t optimized.
(3) This source and the bundle must be all in the same folder.
(4) If Bernie gives permission I’ll upload the bundle but all this code works. It also needs the c header, NSLog.h. Same thing there as the bundle.

HTH - Brian

include "FBLog.incl"
toolbox fn NSApplicationLoad = long

local fn GetFuncPointerFromBundle( bundleName as CFBundleRef, funcName as CFStringRef ) as pointer
dim as pointer       fp

fp = fn CFBundleGetFunctionPointerForName( bundleName, funcName )
if ( fp == 0 ) then shutdown "CFBundleGetFunctionPointerForName failed" 

end fn = fp

local fn GetBundlePtr( bundleName as CFStringRef ) as CFBundleRef
dim as CFBundleRef   mainBundle, bundle
dim as CFURLRef      bundleURL
dim as pointer       fp

fp = 0
mainBundle = fn CFBundleGetMainBundle
if ( mainBundle == 0 ) then shutdown "CFBundleGetMainBundle failed"
bundleURL = fn CFBundleCopyResourceURL( mainBundle,  bundleName, 0, 0 )
if ( bundleURL == 0 ) then shutdown "CFBundleCopyResourceURL failed"
bundle = fn CFBundleCreate( 0, bundleURL )
CFRelease( bundleURL )
if ( bundle == 0 ) then shutdown "CFBundleCreate failed"
if ( fn CFBundleLoadExecutable( bundle ) == _false ) then shutdown "CFBundleLoadExecutable failed"

end fn = bundle

dim as pointer fbLogPtr, fbWinTitlePtr
dim as CFBundleRef   bundleRef

fn NSApplicationLoad

bundleRef = fn GetBundlePtr( fn CFSTR( "NSLogBundle.bundle" ) ) // load the bundle with Bernie’s code

fbLogPtr  = fn GetFuncPointerFromBundle( bundleRef, @"FBLog" )  // get a pointer to Bernie’s FBLog call
def fn MyFBLog( string as CFStringRef ) using fbLogPtr

fbWinTitlePtr = fn GetFuncPointerFromBundle( bundleRef, @"FBLogSetWindowTitle")  // get a pointer to Bernie’s FBLogSetWindowTitle

def fn MySetWindowTitle( string as CFStringRef ) using fbWinTitlePtr

fn MyFBLog(@"Hello")
fn MySetWindowTitle(@"A Different Win Title")


N.B. in this case FBLog.incl looks like:
compile as "Objective-C"

include "NSLog.h"
include resources "NSLogBundle.bundle"

toolbox FBLog( CFStringRef format, ... )
toolbox FBLogShow
toolbox FBLogClear
toolbox FBLogClose
toolbox FBLogSetFrame( CGRect r )
toolbox FBLogSetWindowTitle( CFStringRef title )
toolbox FBLogSetShowsDate( Boolean flag )
toolbox FBLogSetShowsTime( Boolean flag )
toolbox FBLogSetShowsAppName( Boolean flag )
toolbox FBLogSetForwardToConsole( Boolean flag )
toolbox FBLogSetFloatingWindow( Boolean flag )
toolbox FBLogSetFont( CFStringRef name, CGFloat size )