Any examples for C callback

New to FreeBASIC? Post your questions here.
Post Reply
GWBASICcoder
Posts: 21
Joined: Jul 29, 2022 12:43

Any examples for C callback

Post by GWBASICcoder »

Hi,

I require an FB interface to a C function setCallBack:

Code: Select all

typedef void(myCallback)(char *, myStruct *);

void setCallBack (myCallback *);
Could anyone point me to examples of the FB declaration? I have looked at https://www.freebasic.net/wiki/TutInterfacingWithC but that doesn't show how a function pointer is to be used as a callback.

Thanks
GWBc
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Any examples for C callback

Post by D.J.Peters »

Code: Select all

type myStruct ' a placeholder for the real struct defimition
  as integer dummy 
end type  
type myCallback as sub cdecl (byval as zstring ptr, byval as myStruct ptr)
declare sub setCallBack cdecl (byval as myCallback)
GWBASICcoder
Posts: 21
Joined: Jul 29, 2022 12:43

Re: Any examples for C callback

Post by GWBASICcoder »

Hi - thanks much.

I am a bit confused. The C setCallBack function takes a function pointer parameter - so shouldn't the FB function also be byval as myCallback ptr

Code: Select all

declare sub setCallBack cdecl (byval as myCallback ptr)
GWBASICcoder
Posts: 21
Joined: Jul 29, 2022 12:43

Re: Any examples for C callback

Post by GWBASICcoder »

Never mind, I got it.

Code: Select all

'define the procedure
sub completion (byval as zstring ptr, byval as myStruct ptr)
end sub

'dim a var for the procedure pointer
dim completionSubPtr as  myCallback  = @completion

'set the callback
setCallBack(completionSubPtr)
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Any examples for C callback

Post by D.J.Peters »

You can replace a pointer to a struct with the argument as BYREF also.

Code: Select all

type tStruct
  as integer dummy 
end type  
type tCallback as sub cdecl (byval msg as zstring ptr, byref myStruct as tStruct)
declare sub setCallBack cdecl (byval as tCallback)

dim shared as tCallback cb
sub setCallBack cdecl (byval callback as tCallback)
  cb=callback
end sub

sub myCallback cdecl(byval msg as zstring ptr, byref myStruct as tStruct)
  print "callback"
  print *msg,myStruct.dummy
end sub

setCallback(@myCallback)
dim as tStruct myStruct
myStruct.dummy=42
cb("hello world",myStruct)
sleep
marcov
Posts: 3455
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: Any examples for C callback

Post by marcov »

byref: Can you then still pass NIL to it? (e.g. to clear the callback)
angros47
Posts: 2321
Joined: Jun 21, 2005 19:04

Re: Any examples for C callback

Post by angros47 »

Passing a pointer to a data type, or passing the data type byref is similar, but not exactly equivalent, neither in Basic nor in C. In C, in fact, pointers are passed with the "*" symbol, as "int *a" or "int* a", while the equivalent of ByRef would be the "&" symbol, as "int &a" or "int& a"

When interfacing with code written in C, the two structures are interchangeable. But if the external code is written in C++, they are not, because they affect the name mangling in different ways
Post Reply