Need help with Alien, unexpected function return value

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Need help with Alien, unexpected function return value

Joachim Geidel
Hello everybody,

I need some help with Alien on Mac OS X 10.6.3 in Pharo 1.0. I am calling a function from a vtable struct in the Java Native Interface of the Java VM which comes with Mac OS X and which is defined as

    jint ( __stdcall * GetVersion)(JNIEnv * env);

The result type jint is just an alias for int. The Smalltalk method is

----
primGetVersion
        | functionPointer result vtablePointer |
        vtablePointer := Alien
                atAddress: self externalData asUnsignedLong
                dataSize: Alien sizeofPointer * self vtableSize.
        functionPointer := Alien forPointer:
                (vtablePointer unsignedLongAt: (4 * Alien sizeofPointer + 1)).
        result := (Alien newGC: 4) pointer.
        functionPointer
                primFFICallResult: result
                with: self asJNIParameter.
        ^result signedIntAt: 1
----

functionPointer is the pointer to the GetVersion function.
The result should be 65542 (16r00010006), which is also what I get when calling the function from VisualWorks, but the method answers  -394042288 (-16r177C9BB0). The "result" Alien has the printString '#[0 0 0 0 6 0 1 0]' which suspiciously contains the 1 and the 6 I am looking for, but in the wrong places. ;-)

I looked at examples for callouts, which seem to indicate that result should be the Alien and not a pointer to it. So I changed the code to

----
        result := (Alien newC: 4).
        functionPointer
                primFFICallResult: result pointer
                with: self asJNIParameter.
        ^result signedIntAt: 1
----
This didn't really help. The result is now 0 (zero), although the Alien prints as #[252 255 255 255 80 15 38 0] which doesn't look like a 0 at first sight.

Am I using those Aliens in a correct way? What can I try to debug this?
If this is too obscure and someone who knows about Aliens would like to have a look at the rest of the code, I am willing to send a bunch of Monticello packages and instructions for reproducing the problem, but I won't make it publicly available yet.

Any help is welcome.

Best regards,
Joachim Geidel