473,748 Members | 2,594 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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 (*callbackfunct ion)(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

callbackfunctio n clientfunction;

void SpecifyCallback function(cbFunc tion 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?
Jun 27 '08 #1
40 2366
Angus 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 (*callbackfunct ion)(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

callbackfunctio n clientfunction;

void SpecifyCallback function(cbFunc tion cbFn)
{
clientfunction = cbFn;
}

Then called like this:
clientfunction( sz); // sz is a C-string.
You should be passing the address of the function, not a string.

int f( const char* );

clientfunction( f );

--
Ian Collins.
Jun 27 '08 #2
Ian Collins said:
Angus 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 (*callbackfunct ion)(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

callbackfuncti on clientfunction;

void SpecifyCallback function(cbFunc tion cbFn)
{
clientfunction = cbFn;
}

Then called like this:
clientfunction (sz); // sz is a C-string.
You should be passing the address of the function, not a string.

int f( const char* );

clientfunction( f );
I doubt it. Since clientfunction is an instance of callbackfunctio n (and
presumably the definition of callbackfunctio n, above, is supposed to be a
typedef), it takes a const char *, not an int(*)(const char *).
>
--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #3
Richard Heathfield wrote:
Ian Collins said:
>Angus 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 (*callbackfunct ion)(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

callbackfunct ion clientfunction;

void SpecifyCallback function(cbFunc tion cbFn)
{
clientfunction = cbFn;
}

Then called like this:
clientfunctio n(sz); // sz is a C-string.
You should be passing the address of the function, not a string.

int f( const char* );

clientfunction ( f );

I doubt it. Since clientfunction is an instance of callbackfunctio n (and
presumably the definition of callbackfunctio n, above, is supposed to be a
typedef), it takes a const char *, not an int(*)(const char *).
OK, I promise never to post pre-caffeine ever again!

I read the OP as passing a string to SpecifyCallback function.

--
Ian Collins.
Jun 27 '08 #4
"Angus" <no****@gmail.c omwrites:
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 (*callbackfunct ion)(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

callbackfunctio n clientfunction;
This won't compile. I suspect you have a typedef that you are not
showing us!

<snip>
But program crashes with access violation when attempt to call
clientfunction

What am I doing wrong?
Try to post a short, compilable, example of the problem. The outline
you posted is sound, the error is in the detail (and is somewhere
else).

--
Ben.
Jun 27 '08 #5

Angus <no****@gmail.c omwrote in message
news:ft******** ***********@new s.demon.co.uk.. .
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 (*callbackfunct ion)(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

callbackfunctio n clientfunction;

void SpecifyCallback function(cbFunc tion 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_func tion(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_func tion(int my_callback_fun ction(const char*)) {
int my_callback_ret urn;
char *my_string;

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

my_callback_ret urn=my_callback _function(my_st ring);

... /* more generic stuff maybe, maybe check my_callback_ret urn */
}

Now, for any source file that you want to use that generic
my_library_func tion(), 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_fun ction(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(voi d) {

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

my_library_func tion(my_specifi c_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

Jun 27 '08 #6
Bill Reid said:
Angus <no****@gmail.c omwrote in message
news:ft******** ***********@new s.demon.co.uk.. .
<snip>
>>
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:
When you make a claim like that, you should back it up with working code.
You didn't. A conforming implementation *must* diagnose, and *may* refuse
to translate, your code.

Your track record of ignoring or even railing against those who notify you
of your mistakes gives me little or no hope that you'll pay any attention
to this, but the OP will at least have fair warning of the kind of
"competence " to which you are exposing him.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #7

Richard Heathfield <rj*@see.sig.in validwrote in message
news:a9******** *************@b t.com...
Bill Reid said:
Angus <no****@gmail.c omwrote in message
news:ft******** ***********@new s.demon.co.uk.. .
<snip>
>
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:

When you make a claim like that, you should back it up with working code.
You didn't. A conforming implementation *must* diagnose, and *may* refuse
to translate, your code.
Well, I didn't really write any code, you insane troll, so I guess
you're "right" again, as always...
Your track record of ignoring or even railing against those who notify you
of your mistakes gives me little or no hope that you'll pay any attention
to this, but the OP will at least have fair warning of the kind of
"competence " to which you are exposing him.
Well, no, I couldn't help but notice that you didn't specify any
actual problems with what I posted, so how would he know what's
wrong with it? Believe me, for his sake, you should let HIM know,
and let ALL of us know, including me, so we can all benefit from
your tremendous "wisdom"... I'd be the first to admit that I make
a LOT of mistakes, have made many here, and there may be
errors or omissions in my post, and I'd genuinely like to be
"set straight", but somehow I think you're just blowin' troll
smoke again, as usual...

So if you got anything of substance, post it; I'm not saying it
would be a first, but along the lines of a rarity...

---
William Ernest Reid

Jun 27 '08 #8
Bill Reid said:
>
Richard Heathfield <rj*@see.sig.in validwrote in message
news:a9******** *************@b t.com...
>Bill Reid said:
Angus <no****@gmail.c omwrote in message
news:ft******** ***********@new s.demon.co.uk.. .
<snip>
>>
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:

When you make a claim like that, you should back it up with working
code. You didn't. A conforming implementation *must* diagnose, and *may*
refuse to translate, your code.

Well, I didn't really write any code, you insane troll, so I guess
you're "right" again, as always...
Here is the code you claim you didn't really write, which I've copied
verbatim from your article.

extern void my_library_func tion(int (*)(const char*));
void my_library_func tion(int my_callback_fun ction(const char*)) {
int my_callback_ret urn;
char *my_string;

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

my_callback_ret urn=my_callback _function(my_st ring);

... /* more generic stuff maybe, maybe check my_callback_ret urn */
}
int my_specific_fun ction(const char* my_string) {

... /* do something with string, probably print it, right? */
}
void my_function(voi d) {

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

my_library_func tion(my_specifi c_function);

... /* and whatever else */
}

Note that my observation about failure to compile does not relate to
obvious "more stuff goes here" conventions such as an occasional ellipsis.
>Your track record of ignoring or even railing against those who notify
you of your mistakes gives me little or no hope that you'll pay any
attention to this, but the OP will at least have fair warning of the
kind of "competence " to which you are exposing him.

Well, no, I couldn't help but notice that you didn't specify any
actual problems with what I posted,
No point. You never listen anyway.
so how would he know what's wrong with it?
Since it doesn't compile, he'll find out pretty quickly that it *is* wrong.
As to *why* it's wrong, that's easy. It was written by someone who doesn't
understand C very well.
So if you got anything of substance, post it;
After you.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #9
On 14 Apr, 08:10, Richard Heathfield <r...@see.sig.i nvalidwrote:
Bill Reid said:
Richard Heathfield <r...@see.sig.i nvalidwrote in message
news:a9******** *************@b t.com...
Bill Reid said:
Angus <nos...@gmail.c omwrote in message
news:ft******** ***********@new s.demon.co.uk.. .
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:
oh the irony...
When you make a claim like that, you should back it up with working
code. You didn't. A conforming implementation *must* diagnose, and *may*
refuse to translate, your code.
Well, I didn't really write any code, you insane troll, so I guess
you're "right" again, as always...

Here is the code you claim you didn't really write, which I've copied
verbatim from your article.
<snip code-like stuff>
Note that my observation about failure to compile does not relate to
obvious "more stuff goes here" conventions such as an occasional ellipsis.
Your track record of ignoring or even railing against those who notify
you of your mistakes gives me little or no hope that you'll pay any
attention to this, but the OP will at least have fair warning of the
kind of "competence " to which you are exposing him.
Well, no, I couldn't help but notice that you didn't specify any
actual problems with what I posted,

No point. You never listen anyway.
so how would he know what's wrong with it?

Since it doesn't compile, he'll find out pretty quickly that it *is* wrong.
As to *why* it's wrong, that's easy. It was written by someone who doesn't
understand C very well.
So if you got anything of substance, post it;

After you.
perhaps, Richard, if you tried to be just a little less gnomic...
Perhaps, even, point out the error :-)

Who remembers the Campaign Againt Grumpiness in c.l.c.?
--
Nick Keighley

If cosmology reveals anything about God, it is that He has
an inordinate fondness for empty space and non-baryonic dark
matter.
Sverker Johansson (talk.origins)
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2537
by: Bj?rn Toft Madsen | last post by:
Hi all, The network library I use communicates with my app using a callback function. This callback function is called with the type of message, a void pointer to he actual message and a user defined void pointer. The callback function expected is a standard C callback function. I have used a static class function, which I certainly know is not entirely correct. Bear with me, I don't think my troubles are related to this hack.
2
2683
by: Chris Morley | last post by:
Hi, I have always done my C++ class callbacks with the age old 'using this pointer in parameter of the class's static callback function' and typecasting it to get the specific instance. However I'm left wondering are there any better ways of doing this? Just before I left work today I came across functors, which look promising. I will have a proper look tomorrow, but I have a few questions in the mean time.
9
8256
by: tropostropos | last post by:
On Solaris, using the Sun compiler, I get annoying warnings from the following code. The problem is that I am passing a C++ member function pointer to the C library function qsort. Is there a solution? Declaring the function extern "C" fails, because linkage declarations must be made at file scope. #include <stdlib.h> //for qsort template <class T> class Sorter
6
8828
by: keepyourstupidspam | last post by:
Hi, I want to pass a function pointer that is a class member. This is the fn I want to pass the function pointer into: int Scheduler::Add(const unsigned long timeout, void* pFunction, void* pParam)
0
3290
by: Jeffrey B. Holtz | last post by:
Has anyone used the multimedia timere timeSetEvent in C#? I'm trying to use it to get a 1ms accurate timer. All the other implementations are far to inaccurate in their resolution. 1ms Timer = 0ms.....60ms. I'm already timing the callbacks using the Win32API QueryPerformanceCounter/Frequency. The code I have is actually performing the callback and it looks like it is close to 1ms but I now need to get my object passed to this static...
1
8299
by: Lenn | last post by:
Hi, I am using .BeginInvoke to make an asynchronous call to a function, and passing in a AsyncCallback, so client gets notification when method completes, relevant code is provided below: AsyncCallback callback = new AsyncCallback(this.NotifyIt); FtpClient ftpC = new FtpClient(FtpServer, FtpUserName, FtpPassword); private void button4_Click_1(object sender, System.EventArgs e) {
2
1543
by: TN | last post by:
I have a bit of C code, that creates an instance of a .Net class that has been built as a type library. Everything is working as expected, I can pass strings to methods in the object. What I would like to also do is pass a pointer to C function to one method to store it as a callback, then later have another method call that callback. I know this is not a "safe" operation, but I am wondering if it can be done. If not what other methods...
3
3062
by: ryan.mitchley | last post by:
Hi all I have a class (cPort) that is designed to receive objects and, depending on the type, call a handler (callback) in any descendant of a cProcessBlock class. Callback functions take a shared_ptr<cBaseas a parameter, and return void. The code was working fine, although I have encountered problems (under a Microsoft compiler, of course - VC 8.0) when I attempt to add callbacks to a class with multiple inheritance. I hate multiple
6
7681
by: smmk25 | last post by:
Before I state the problem, I just want to let the readers know, I am knew to C++\CLI and interop so please forgive any newbie questions. I have a huge C library which I want to be able to use in a .NET application and thus am looking into writing a managed C++ wrapper for in vs2005. Furthermore, this library has many callback hooks which need to be implemented by the C++ wrapper. These callback functions are declared as "extern C...
0
8996
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8832
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9333
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.