CCallbacks

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

CCallbacks

Alexander Lazarevic'
Hi,

do CCallbacks work with shared libraries under linux86? I have something
like this (just a quick example):

libHallo.c
----------
void
hallo(int (* callback)(int,const char *, int ar[3])) {
        int res,
            ar[3] = {88, 77, 99};

        printf("install\n");

        res = callback(5, "Say hallo", ar);

        printf("result : %d\n", res);
        printf("%d %d %d\n", ar[0], ar[1], ar[2]);
}

sayHallo.c
----------
int callback(int number, const char *name, int ar[3]) {
        printf("%d %s %d %d %d\n", number, name, ar[0], ar[1], ar[2]);
        ar[0] = number;
        ar[1] = number;
        ar[2] = number;
        return number-1;
}

int main(int argc, char argv[]) {
        hallo(callback);
}

Running sayHello gives:

install
5 Say hallo 88 77 99
result : 4
5 5 5

Now from VW with the ExternalInterface Hallo and:

callbackType
        <C: typedef int (* callbackType)(int number, const char *name, int ar[3])>

hallo: callback
        <C: void hallo(int (* callback)(int number,const char *name, int ar[3]))>
        ^self externalAccessFailedWith: _errorCode


Doing this in a workspace yields an exception:

lib := Hallo new.
cb := CCallback
        do: [:number :name :ar |
                Transcript show: 'callback'; cr.
                "look at arguments here"
                0
        ]
        ofType: lib callbackType.

lib hallo: cb

The callback gets called, but there are no arguments!? The arguments
array just contains #(nil nil nil).

Alex

Reply | Threaded
Open this post in threaded view
|

Re: CCallbacks

Alexander Lazarevic'
JFYI, I got the tip that changing ar[3] with the equivalent *ar in the
declarations should make the example work. Which it did. :) Thanks.

Alex

Alexander Lazarevic' schrieb:

> Hi,
>
> do CCallbacks work with shared libraries under linux86? I have something
> like this (just a quick example):
>
> libHallo.c
> ----------
> void
> hallo(int (* callback)(int,const char *, int ar[3])) {
>         int res,
>    ar[3] = {88, 77, 99};
>
> printf("install\n");
>
> res = callback(5, "Say hallo", ar);
>
> printf("result : %d\n", res);
> printf("%d %d %d\n", ar[0], ar[1], ar[2]);
> }
>
> sayHallo.c
> ----------
> int callback(int number, const char *name, int ar[3]) {
> printf("%d %s %d %d %d\n", number, name, ar[0], ar[1], ar[2]);
> ar[0] = number;
> ar[1] = number;
> ar[2] = number;
> return number-1;
> }
>
> int main(int argc, char argv[]) {
>         hallo(callback);
> }
>
> Running sayHello gives:
>
> install
> 5 Say hallo 88 77 99
> result : 4
> 5 5 5
>
> Now from VW with the ExternalInterface Hallo and:
>
> callbackType
> <C: typedef int (* callbackType)(int number, const char *name, int ar[3])>
>
> hallo: callback
> <C: void hallo(int (* callback)(int number,const char *name, int ar[3]))>
> ^self externalAccessFailedWith: _errorCode
>
>
> Doing this in a workspace yields an exception:
>
> lib := Hallo new.
> cb := CCallback
> do: [:number :name :ar |
> Transcript show: 'callback'; cr.
> "look at arguments here"
> 0
> ]
> ofType: lib callbackType.
>
> lib hallo: cb
>
> The callback gets called, but there are no arguments!? The arguments
> array just contains #(nil nil nil).
>
> Alex
>