473,387 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Ptr-to-member as callback in a c-function

Hi,

I'm using a c-library in my c++ project.
Therefore I've to call this c-function:
int rm_init(int id, char* ip, unsigned short port, int timeout,
callaback_fnc);

the signatures of the callback function is:
void callback_fnc(int, int, int, char*)

I want to use a class member as the callback-function and the call
shall be done in the ctor of the same class:

struct C
{
void cb(int id, int cmd, int len, char* data);

C()
{

}
};

Jun 26 '07 #1
4 1441
Sorry I pressed enter to fast and forget the question:

how would I call rm_init() from C() and give the member funtions
(C::cb()) instead a c-funtion to it?
I guess I need some bind or mem_fun magic, but Im not shure about ist.

Thank you in advance!

Jun 26 '07 #2
rammel wrote:
Sorry I pressed enter to fast and forget the question:

how would I call rm_init() from C() and give the member funtions
(C::cb()) instead a c-funtion to it?
I guess I need some bind or mem_fun magic, but Im not shure about ist.

Thank you in advance!
Simple answer, you can't.

You must provide a global function for your callback. Your global
function can then call C::cb() if that is what you want to do. The C
library appears to have been badly designed so that this is not easy.

john
Jun 26 '07 #3
On 2007-06-26 08:37, John Harrison wrote:
rammel wrote:
>Sorry I pressed enter to fast and forget the question:

how would I call rm_init() from C() and give the member funtions
(C::cb()) instead a c-funtion to it?
I guess I need some bind or mem_fun magic, but Im not shure about ist.

Thank you in advance!

Simple answer, you can't.

You must provide a global function for your callback. Your global
function can then call C::cb() if that is what you want to do. The C
library appears to have been badly designed so that this is not easy.
Actually it does not have anything to do with the C library, but rather
the difference between a function-pointer and an member-function-
pointer. If you want to call a member-function through a pointer you
must have a way to tell for which object you want to call the member-
function.

--
Erik Wikström
Jun 26 '07 #4
Erik Wikström wrote:
On 2007-06-26 08:37, John Harrison wrote:
>rammel wrote:
>>Sorry I pressed enter to fast and forget the question:

how would I call rm_init() from C() and give the member funtions
(C::cb()) instead a c-funtion to it?
I guess I need some bind or mem_fun magic, but Im not shure about ist.

Thank you in advance!

Simple answer, you can't.

You must provide a global function for your callback. Your global
function can then call C::cb() if that is what you want to do. The C
library appears to have been badly designed so that this is not easy.

Actually it does not have anything to do with the C library, but rather
the difference between a function-pointer and an member-function-
pointer. If you want to call a member-function through a pointer you
must have a way to tell for which object you want to call the member-
function.
Sorry, I meant the C library that the OP is using. The API for that
library appears badly designed.

john
Jun 26 '07 #5

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

Similar topics

1
by: scott ocamb | last post by:
hello I have implemented a solution using async methods. There is one async method that can be invoked multiple times, ie there are multiple async "threads" running at a time. When these...
0
by: vijaya | last post by:
I've to invoke a unmanaged dll fucntion in C# which uses a callback fucntion.The unmanaged dll fucntion is as follows **************************************** The Original Fucntion in the dll...
4
by: ma740988 | last post by:
// file sltest.h #ifndef SLTEST_H #define SLTEST_H class CallbackBase // herb shutters gotW source .. { public: virtual void operator()() const { }; virtual ~CallbackBase() = 0; };
15
by: Felix Kater | last post by:
Hi, in a given library I register callback functions with this function: bool set_callback(int index, int (*callback_function)(long)); I need the callback function to also pass the index...
2
by: MR | last post by:
help! I have an unmanaged DLL that I do not have the source code, so i can't recompile or make changes. the DLL requires a callback function. I would like to implement the callback method in a...
5
by: Maxwell | last post by:
Hello, Newbie question here. I have a VS.NET 2003 MC++ (not C++/cli) project where I have a managed class reference in a unmanaged class...simple enough. To keep things short I am for the most...
6
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...
10
by: SQACPP | last post by:
Hi, I try to figure out how to use Callback procedure in a C++ form project The following code *work* perfectly on a console project #include "Windows.h" BOOL CALLBACK...
0
by: Tim Spens | last post by:
--- On Fri, 6/27/08, Tim Spens <t_spens@yahoo.comwrote: I think I know where the problem is but I'm unsure how to fix it. When I call Register_Handler(...) from python via...
5
by: Jef Driesen | last post by:
I have a C DLL that I want to use from a C# project. The C header file contains these declarations: typedef void (*callback_t) (const unsigned char *data, unsigned int size, void *userdata);...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.