I find it much easier to store the library reference in feature memory, and
then change every reference to the library to a function that retrieves the
feature value.
Something like this

// UInt16 MathLibRef
#define MathLibRef  getMathLibRef()
#define FeatureNumberMathLibRef 0x6000


static UInt16 getMathLibRef()
{
    UInt32 value = sysInvalidRefNum;
    FtrGet(MyProgramCreatorID, FeatureNumberMathLibRef, &value);
    return (UInt16)value;
}

static void setMathLibRef(UInt16 refNum)
{
    UInt32 value = (UInt32)refNum;
    if (refNum == sysInvalidRefNum)
        FtrUnregister (MyProgramCreatorID, FeatureNumberMathLibRef);
    else
        FtrSet(MyProgramCreatorID, FeatureNumberMathLibRef, value);
}

In the LibraryOpen function call setMathLibRef( refNum), and in the
LibraryClose function call setMathLibRef(sysInvalidRefNum).


LionScribe



"Ben Combee" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> At 12:45 PM 5/7/2004, you wrote:
> >Hi,
> >
> >I'm creating a shared library (syslib-type) that in turn makes use of
> >MathLib. Those of you who've used MathLib know that you include MathLib.c
> >in your own project. MathLib.c defines a global variable to hold the
> >refnum when the library is opened. Global variables are not allowed in
> >shared libraries, so I'm told. Yet, when I placed the global variable in
> >the .text segment of my shared library, like this:
> >
> >UInt16 MathLibRef __attribute__ ((section (".text")));
> >
> >it worked (meaning it compiled without errors or warnings, and I was able
> >to successfully access MathLib functions in a test program)! Was this
just
> >a fluke, or can this be done safely and reliably?
>
> I would guess that the only reason this would work would be that you were
> running on a device or simulator where memory protection was
> disabled.  Putting the variable in the .text section puts it in read-only
> memory on almost all devices.
>
> Why not implement your code as a C++ class where you can pass this MathLib
> ref around as one of the class instance members.  While you can't use a
lot
> of C++ in a shared library, a simple stack-based class should work fine
and
> would simplify passing of "pseudo-globals".  I used this technique in the
> original PACEInterface layer in the CW PNO tools.
>
>
> -- Ben Combee, DTS technical lead, PalmSource, Inc.
>     Read "Combee on Palm OS" at http://palmos.combee.net/
>
>
>



-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/support/forums/

Reply via email to