Andreas,
I *think* I already took your second option. I created functions that do nothing but return the structure pointer, call them and pass the result onward - it seems to work.
Bill
-----Original Message----- From: pharo-project-bounces@lists.gforge.inria.fr [mailto:pharo-project-bounces@lists.gforge.inria.fr] On Behalf Of Andreas Raab Sent: Saturday, April 03, 2010 1:16 PM To: The general-purpose Squeak developers list Cc: Pharo-project@lists.gforge.inria.fr Subject: Re: [Pharo-project] FFI - structure pointer
On 4/3/2010 10:10 AM, Schwab,Wilhelm K wrote:
Andreas,
#getProcAddress: is a Smalltalk message, so it does not have a return type. It does return an ExternalData because it it ultimately calls
<cdecl: void* 'FindSymbol' ( long char* )>
I had to create FindSymbol as a simply wrapper around dlsym() because the vm dies a quick death over (what I believe to be) linking hassles similar to recent problems in skype. Of course, I am referring the Linux vm or I would be calling GetProcAddress().
One thing that I considered is that the aspects of the ExternalData might be incorrect. The handle is an external address and type is an ExternalType that prints itself as void*. With many variations, if the call was made, the vm crashed.
Can you verify the correctness of both pointer as well as function signature? Depending on platform and/or entity in question you just *might* get an extra indirection.
If you can't get the code to work as-is I would recommend compiling your own library that exports a symbol in the form you're using and then verify correctness of the bridge that way.
Cheers, - Andreas
-----Original Message----- From: pharo-project-bounces@lists.gforge.inria.fr [mailto:pharo-project-bounces@lists.gforge.inria.fr] On Behalf Of Andreas Raab Sent: Saturday, April 03, 2010 11:54 AM To: The general-purpose Squeak developers list Cc: Pharo-project@lists.gforge.inria.fr Subject: Re: [Pharo-project] FFI - structure pointer
On 4/3/2010 6:36 AM, Schwab,Wilhelm K wrote:
Andreas, John, anybody,
I need to find the address of a "function" (it is in fact a global structure pointer somewhere in memory) and pass it to another function.
Suppose the structure is called Type and the function allocate_one(). I call the function something like
allocateOne:type size:anInt <cdecl: SomeOtherStruct* 'allocate_one' ( Type* long )>
but I am having an awful time getting the pointer to Type. Given that dlsym() will return a pointer to it, how can I represent that in Smalltalk? I have tried variations on
"Indirectly call dlsym()" address := Library default getProcAddress:'name_of_global_struct'. "try to create Type* from the resulting address" type := Type fromHandle:address. type := Type fromHandle:address getHandle. type := ExternalData fromHandle:address type:Type externalType asPointerType. Something := Library default allocateOne:type size:2.
and it either complains about bad arguments during the call or segment faults. Any ideas or similar examples?
The return type of getProcAddress: is likely wrong. It should be declared to return void* which will cause it to return an ExternalData. With an ExternalData your second variant "Type fromHandle: data getHandle" will work.
Cheers, - Andreas
_______________________________________________ Pharo-project mailing list Pharo-project@lists.gforge.inria.fr http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
_______________________________________________ Pharo-project mailing list Pharo-project@lists.gforge.inria.fr http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
squeak-dev@lists.squeakfoundation.org