How to write call back in Symbian C++ which can be called from a generic 'C' Code.?

Login to reply to this topic.
Mon, 2008-05-05 14:50
Joined: 2008-04-28
Forum posts: 3

Puzzled
Hi All,

I wanted to know how to write Call back in Symbian C++ ( I assume we need to write MClass) and then how it can be called from Generic C code? As we all know, C won't accept a class pointer or reference... How to call any such MClass method from a C code?

If any link to any example code, it will be a great help.

Thanks.


Tue, 2008-05-06 13:03
Joined: 2005-04-13
Forum posts: 90
Re: How to write call back in Symbian C++ which can be called fr

I am not sure whether it will solve your problem. But one of doing it can be

write one function (let say foo) in Symbian C++, which can have TCallback function, which must call the Static function of a class lets Say X.
X class must have the reference of your M class.

so by calling foo() from your C code, will work as the callback mechanism only.


Jupitar

Wed, 2008-05-07 12:47
Joined: 2004-11-29
Forum posts: 1134
Re: How to write call back in Symbian C++ which can be called fr

If you want to create a function (or anything else, like a global variable) that can be used from C-code, you have to make sure the symbol is exported from the .o-file in the C-standard way, instead of the compiler specific C++-way.

C++ functions are exported with an extended name that also includes argument types, to enable function overloading.
MyFunction(Tint aNumber) would be exported as something like _ZNK10MyFunctionEi

C functions are exported with almost the same name as the function. MyFunction(TInt aNumber) will be called simply _MyFunction within the .o-file

So... how to solve your problem?

Simple! by specifying export "C" before your function, like this:

export "C" void MyFunction(TInt aNumber)

it will be exported in the C-way instead of the C++ way, and you can link your .o together with the rest of the .o-files written in C.

You can also do export "C" as a block, if you have a bunch of symbols you want exposed in the C way:

export "C" {
void MyFunction(TInt aNumber) {
...
}

void MyOtherFunction() {

}
} //export "C"

After this, the problem arises, how to tie this into the tree of classes that your app consist of?
These functions would be just global functions without any connection to your classes.

For this, you will need to somehow pass a class pointer to the function, this can be done in numerous ways.

The simplest is probably just a global variable that you store the pointer to.

For a more dynamic system, you might want to call some function in the C-code, from your C++ code, to tell it what pointer to pass along back to the callback function..
Probably the nicest design is to send this pointer in together with the initialization of your C-code or at the request of whatever you want your C-code to do..

To call C-code from C++, you must make sure that the C++ compiler knows it should expect symbols in the .o-file that is exported according to the C-standard.

This you do with import "C", easiest around the .h-file that you declare your C-funtions in, like this:

import "C" {
#include "mycfunctions.h"
}

Then you can just call them as normal.

  • Login to reply to this topic.