Re: How do I create a function in my library for passing user callback function



On Apr 13, 12:39 pm, "Angus" <nos...@xxxxxxxxx> wrote:
Hello

I am writing a library which will write data to a user defined callback
function. The function the user of my library will supply is:

int (*callbackfunction)(const char*);

In my libary do I create a function where user passes this callback
function? How would I define the function?

I tried this

callbackfunction clientfunction;

void SpecifyCallbackfunction(cbFunction cbFn)
{
clientfunction = cbFn;

}

Then called like this:
clientfunction(sz); // sz is a C-string.

But program crashes with access violation when attempt to call
clientfunction

What am I doing wrong?

Based on what you've written here, it sounds like you want to
"register" the callback so it can be called by other functions within
the library: is that correct? If so, here's one approach:

/* Library header file */
#ifndef LIB_H
#define LIB_H
/**
* Create a typedef for the function pointer type
*/
typedef int (*callback)(const char *);

/**
* Register the callback function
*/
void SpecifyCallbackfunction(callback f);

/**
* Other library functions that will use the callback
*/
void L1(const char *foo);
void L2(void);

#endif

/**
* Library implementation file
*/

/**
* File-scope global for callback
*/
static callback g_callback;

void SpecifyCallbackFunction(callback f)
{
g_callback = f;
}

void L1(const char *foo)
{
int bar = g_callback(foo);
...
}

void L2(void)
{
int bar = g_callback("DUMMY");
...
}

/**
* main
*/

#include "library.h"

int blah(const char *bletch)
{
int retVal;
retVal = /* result of doing something with bletch */
return retVal;
}

int main(void)
{
SpecifyCallbackFunction(blah);
L1("blurga");
L2();
...
return 0;
}

HTH.
.



Relevant Pages

  • Re: function pointer help!
    ... //the return void and input prameters are defined in the manual... ... void MyProjectView::CallHandler(int,unsigned int, unsigned int, void*) ... You are attempting to use a C++ member function as the callback, but the callback is defined in terms or C, not C++. ... The underlying problem is that C++ functions receive a hidden parameter, the 'this' pointer, so their signature is incompatible with C definitions. ...
    (microsoft.public.vc.mfc)
  • RE: Callback not being called
    ... The callback in the unmanaged code appears to fire, ... ppStringArray, int cArray); ... void CALLBACK TimerProc( ... theCallBack,LPTSTR* ppStringArray, int cArray) ...
    (microsoft.public.dotnet.framework.interop)
  • Re: communicating through callback
    ... > and it sends the data I want to the callback function. ... context pointer that is then passed into the callback function ... typedef int (handle h, void * context); ...
    (comp.lang.c)
  • Re: Passing an integer to a function that accepts a void pointer
    ... int main{ ... callback of a library I can't change and the prototype of the callback ... is well defined by the library to accept void pointer. ...
    (comp.lang.c)
  • Re: Passing an integer to a function that accepts a void pointer
    ... int main{ ... For the cast to void*: ... callback of a library I can't change and the prototype of the callback ... Just create an object containing the value 10, and then pass a pointer ...
    (comp.lang.c)