1. Wrapping C Libraries callback

Hello,

I am wondering how you properly wrap a callback function from C library? When you see something like void(*call back) or something function(void*)? I am sure there is a way to properly wrap these. Usually I've done something like the code below. Though I am sure there is a better way to actually wrap these callback functions.

atom dll = open("dll.dll") 
 
if dll = -1 then 
  puts(1,"Could not load DLL!\n") 
  abort(0) 
end if 
 
--Code here is just for example 
 
constant xmyFunction = define_c_func(dll,"myFunction",{C_POINTER},C_POINTER) --where the pointer is the *callback from the C function 
 
function myFunction(atom x) 
 return c_func(xmyFunction,{x}) 
end function 

Again, I think there is more to then just simply wrapping it like that. I think a couple extra steps are needed when you have a more complex function to wrap. Any is appericated.

new topic     » topic index » view message » categorize

2. Re: Wrapping C Libraries callback

Icy_Viking said...

When you see something like void(*call back) or something function(void*)?

Please show the actual C code.

You may (very rarely) need to use define_c_func({},addr,args), if what you've got is the raw address of a callback from a C function, but more likely if you need to provide a callback to a C function you need to pass a result from call_back() to c_func.

new topic     » goto parent     » topic index » view message » categorize

3. Re: Wrapping C Libraries callback

petelomax said...
Icy_Viking said...

When you see something like void(*call back) or something function(void*)?

Please show the actual C code.

You may (very rarely) need to use define_c_func({},addr,args), if what you've got is the raw address of a callback from a C function, but more likely if you need to provide a callback to a C function you need to pass a result from call_back() to c_func.

FGAPI void    FGAPIENTRY glutMouseWheelFunc( void (* callback)( int, int, int, int ) ); 

This is the actual C code. I'm sure there is a special way you need to wrap the callback pointer in there.

new topic     » goto parent     » topic index » view message » categorize

4. Re: Wrapping C Libraries callback

Icy_Viking said...
FGAPI void    FGAPIENTRY glutMouseWheelFunc( void (* callback)( int, int, int, int ) ); 

This is the actual C code. I'm sure there is a special way you need to wrap the callback pointer in there.

That translates to (obviously untested, and somewhat based on the first hit I got from google being "void glutMouseWheelFunc(void(*)(int wheel, int direction, int x, int y) callback)")

-- library code: 
constant xglutMouseWheelFunc = define_c_proc(lib,"glutMouseWheelFunc",{C_POINTER}) 
 
procedure glutMouseWheelFunc(atom callback) 
    c_proc(xglutMouseWheelFunc,{callback}) 
end procedure 
 
-- application code: 
function glutMouseWheelFunc_cb(integer wheel, integer direction, integer x, integer y) 
    -- do what your app must do 
end function 
 
glutMouseWheelFunc(call_back(routine_id("glutMouseWheelFunc_cb"))) 

Or at least that would be my first stab.

I think that in C "void(* callback)()" and "void(*)() callback" are actually exactly the same thing, go figure.

Pete

new topic     » goto parent     » topic index » view message » categorize

5. Re: Wrapping C Libraries callback

petelomax said...
Icy_Viking said...
FGAPI void    FGAPIENTRY glutMouseWheelFunc( void (* callback)( int, int, int, int ) ); 

This is the actual C code. I'm sure there is a special way you need to wrap the callback pointer in there.

That translates to (obviously untested, and somewhat based on the first hit I got from google being "void glutMouseWheelFunc(void(*)(int wheel, int direction, int x, int y) callback)")

-- library code: 
constant xglutMouseWheelFunc = define_c_proc(lib,"glutMouseWheelFunc",{C_POINTER}) 
 
procedure glutMouseWheelFunc(atom callback) 
    c_proc(xglutMouseWheelFunc,{callback}) 
end procedure 
 
-- application code: 
function glutMouseWheelFunc_cb(integer wheel, integer direction, integer x, integer y) 
    -- do what your app must do 
end function 
 
glutMouseWheelFunc(call_back(routine_id("glutMouseWheelFunc_cb"))) 

Or at least that would be my first stab.

I think that in C "void(* callback)()" and "void(*)() callback" are actually exactly the same thing, go figure.

Pete

That seems fairly easy enough, I thought a more complicated process would be needed.

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu