"Joe Nardone" <[EMAIL PROTECTED]> wrote in message
news:11857@palm-dev-forum...
>
>
> > How can I write code in CW to call a shared library that I don't know
> > about
> > at compile time?
> >
> > Say I want to call the 'n'th function (in the dispatch table) in a
shared
> > lib
> > from my program, but I'm going to get the name/creator of the library
> > and the specific entry from external input (i.e. the user tells me,
etc.)?

If you have to wait until runtime to know which library to use, you should
be ok.  You can just get the name from the user, SysLibLoad() the library,
then get the lib's SysLibTblEntry using SysLibTblEntry( refNum ).  This
structure has a pointer to the lib's dispatch table.

> > All examples I've seen with shared libs have involved compiling in the
> > specific
> > header for the shared lib,i.e.:
> >
> > Extern MSLErr MSLOpen( UInt uRefNum ) SYS_TRAP ( sysLibTrapOpen );
> >
> > The big pain of this is the SYS_TRAP macro.  I don't see how to call an
> > arbitrary trap # (i.e. sysLibTrapOpen) when I don't know the trap number
> > until runtime.

The trap in this case is always trap #15.  That MSLOpen definition looks
like it came out of my article of long ago.  If you look a few sentences
past the definition of MSLOpen(), there is some disassembled code:

3F2E FFFE move.w -2(a6),-(a7)
4E4F trap #15
A801 <unknown>

So this will call trap #15, the shared library dispatch trap.  It will call
into a shared library function A801, which is going to be that lib's open
function.  You can throw in your own assembly and change this address to
anything you desire:
A801 = SomeLibOpen()
A802 = SomeLibClose()
A803 = SomeLibSleep()
A804 = SomeLibWake()
A805 = First custom lib function ( a.k.a sysLibTrapCustom )

Of course you need to specify a particular reference number.  You can just
move.w your reference number into -(a7) before the 4E4F trap #15 occurs, and
you should be ok.  This will allow you to call an arbitrary function into a
shared library, given its reference number.

Furthermore, realize that at A800 exists a string that is the shared
library's name.  If you have a dispatch table pointer, you can get its name
like this (see SysLibLoad() for more, this is exactly what it does):
  offsetP = (WordPtr)dispatchTblP;
  libNameP = (CharPtr)(dispatchTblP + offsetP[0]);

If you use this technique, you can iterate through all the loaded libraries
using SysLibTblEntry( refNum ).  If you get a non-NULL dispatchTblPtr, you
can get the library name, and populate a drop-down selection list with the
name.  Then you can have a dropdown of which function number you want to
call.  Compute that number against sysLibTrapCustom, generate the 3 m68k
opcodes to perform the dispatch, and you're golden.  The only tough thing is
calling conventions for the particular functions, which is unknown w/o the
header file.  That's a little too much brainpower for 2 AM, sorry!

-Jeff Ishaq



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

Reply via email to