473,320 Members | 1,699 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,320 software developers and data experts.

Callback into C++ from C

I have legacy C code that performs some required functionality. I want
to notify my C++ classes after work has been completed in the C code. In
otherwords, I need to call a C++ method from C.

Some PSEUDO code may help:
/* C++ code */
class MyFooClass
{
public:
NotifyHandler();
};

/* C code */

#ifdef __cplusplus
extern "C" {
#endif

void foo()
{
/* Do some work */
/* finish work */
/* Notify C++ class via callback here */
}

#ifdef __cplusplus
};
#endif
Aug 28 '07 #1
4 1728
Anonymous wrote:
I have legacy C code that performs some required functionality. I want
to notify my C++ classes after work has been completed in the C code.
In otherwords, I need to call a C++ method from C.

Some PSEUDO code may help:
/* C++ code */
class MyFooClass
{
public:
NotifyHandler();
};

/* C code */

#ifdef __cplusplus
extern "C" {
#endif

void foo()
{
/* Do some work */
/* finish work */
/* Notify C++ class via callback here */
}

#ifdef __cplusplus
};
#endif
First off, there is some information on callbacks in the FAQ,
have you read it? If not, please do.

Second, the simplest way is to compile your so called C code
as C++ and be done with it, then you can pass your object in
and call anything you want from it.

If that's not something you want or are prepared to do, you
could play the static_cast game, where your class has a static
member function with a 'void*' argument. Inside it casts the
pointer to an object of its type ('MyFooClass') and calls the
member function. The C will need a void* passed to it so it
can pass it "back" to the static function.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 28 '07 #2
"Victor Bazarov" <v.********@comAcast.netwrote in message
news:hv******************************@comcast.com. ..
Anonymous wrote:
[...]
If that's not something you want or are prepared to do, you
could play the static_cast game, where your class has a static
member function with a 'void*' argument.
[...]

How can a static member function be extern "C"?

Aug 28 '07 #3
Chris Thomasson wrote:
"Victor Bazarov" <v.********@comAcast.netwrote in message
news:hv******************************@comcast.com. ..
>Anonymous wrote:
[...]
>If that's not something you want or are prepared to do, you
could play the static_cast game, where your class has a static
member function with a 'void*' argument.

[...]

How can a static member function be extern "C"?
It can't, a better alternative is to use a friend function.

--
Ian Collins.
Aug 28 '07 #4
On 2007-08-27 23:49:28 -0400, Ian Collins <ia******@hotmail.comsaid:
Chris Thomasson wrote:
>"Victor Bazarov" <v.********@comAcast.netwrote in message
news:hv******************************@comcast.com ...
>>Anonymous wrote:
[...]
>>If that's not something you want or are prepared to do, you
could play the static_cast game, where your class has a static
member function with a 'void*' argument.

[...]

How can a static member function be extern "C"?
It can't, a better alternative is to use a friend function.
Use a free function. Whether that function also has to be a friend is
an implementation detail.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Aug 28 '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...
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...
2
by: Evan Burkitt | last post by:
Hi, all. I have a Windows DLL that exports a number of functions. These functions expect to receive a pointer to a callback function and an opaque void* parameter. The callback functions are...
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.