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




Angus <nospam@xxxxxxxxx> wrote in message
news:fttgg7$g7k$1$8302bc10@xxxxxxxxxxxxxxxxxxx

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?

Well, just about everything, and most pertinently, asking a question
here, the land of the technically-incompetent trolls...but here's how
you do it:

In the header file for your library, declare the function as follows:

extern void my_library_function(int (*)(const char*));

(Note: as somebody may tell you, "extern" is a redundant
linkage specifier for function declarations, but I use it anyway
and therefore you should too!)

Now write your library function that takes the callback as
a parameter in the source file for your library:

void my_library_function(int my_callback_function(const char*)) {
int my_callback_return;
char *my_string;

... /* generic stuff done here, probably "build up" my_string */

my_callback_return=my_callback_function(my_string);

... /* more generic stuff maybe, maybe check my_callback_return */
}

Now, for any source file that you want to use that generic
my_library_function(), you can call it by first #include'ing the
library header file, then defining a specific callback function that
matches the declaration in the header file:

int my_specific_function(const char* my_string) {

... /* do something with string, probably print it, right? */
}

Then you can call your library function with the callback anywhere
in your source file, as well as any other functions that you have defined
that match the callback signature:

void my_function(void) {

... /* stuff happens here, whatever, maybe nothing, who knows */

my_library_function(my_specific_function);

... /* and whatever else */
}

And that's "all" there is to it...not that bad once you get the hang of
it, just follow the pattern above, sometimes you have to really "think"
about what the perfect "signature" will be for all the various callbacks
you want for a generic library function, what all data you need to
pass for all possible conditions...

---
William Ernest Reid



.



Relevant Pages

  • Re: In the need for some advice!
    ... I am a beginer in VC++ and have discovered how to use child windows and assigning the child window's required logic to a CALLBACK childWndProc function as so: ... I created a header file where this ChildWndProc function resides. ... Now, for my next child window, should I create a new header file for my second ChildWndProc or should I create it in the same header file as the first one. ...
    (microsoft.public.vc.language)
  • Re: In the need for some advice!
    ... LRESULT CALLBACK CW1_WndProc (HWND hwnd, UINT message, ... I created a header file where this ChildWndProc function resides. ... the event where I would need 20 child windows, well, this file would be huge! ...
    (microsoft.public.vc.language)
  • Re: In the need for some advice!
    ... LRESULT CALLBACK CW1_WndProc (HWND hwnd, UINT message, ... I created a header file where this ChildWndProc function resides. ... the event where I would need 20 child windows, well, this file would be huge! ...
    (microsoft.public.vc.language)
  • translating more stuff from C
    ... I'm not even really sure how to phrase this question, ... Given an entry in a C++ header file like such: ... // Callback for displaying error and warning messages. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: confused about global namespace and scope
    ... "Jonathan Wood" wrote: ... > You can declare it in any source file. ... If you declare it in a header file, ...
    (microsoft.public.vc.mfc)