Forums | developer.brewmp.com Forums | developer.brewmp.com

Developer

Forums

Forums:

Hi!

I am not very sure how to use callbacks in while working with BREW in C++

mainly about the third parameter pcx

CALLBACK_Init(pcb,pcf,pcx)

Please help

Pcx is a pointer to any data you'd want to use in the callback. Usually you'd pass in a pointer to your applet structure.

Pcx is a pointer to any data you'd want to use in the callback. Usually you'd pass in a pointer to your applet structure.

mohlendo wrote:Pcx is a pointer to any data you'd want to use in the callback. Usually you'd pass in a pointer to your applet structure.
But the problem is I am working in C++ so I don't have an applet structure. every thing here is class member

mohlendo wrote:Pcx is a pointer to any data you'd want to use in the callback. Usually you'd pass in a pointer to your applet structure.
But the problem is I am working in C++ so I don't have an applet structure. every thing here is class member

where r u using the CALLBACK_Init(pcb,pcf,pcx)
in the Applet class member functions u have created or in some other class member funtions
cause i have had problems with the callback so what i did was made pcf a friend function to the applet class and used the applet pointer i defined in the Applet class to pass to the pcf.
u assign the IApplet pointer the instance of ur applet class

where r u using the CALLBACK_Init(pcb,pcf,pcx)
in the Applet class member functions u have created or in some other class member funtions
cause i have had problems with the callback so what i did was made pcf a friend function to the applet class and used the applet pointer i defined in the Applet class to pass to the pcf.
u assign the IApplet pointer the instance of ur applet class

This is the code snippet of what I am doing
//these are my class members
INetMgr * pNet;
AEEDNSResult pRes;
AEECallback cb;
//MyApp is the class name
void MyApp::netCallBack(MyApp *pThis)
{
char str[20];
int i = 0;
DBGPRINTF("netCallBack()");
i = pThis->pRes.nResult;
DBGPRINTF("nResult = %x", i);
DBGPRINTF("addrs[0] = %#x", pThis->pRes.addrs[0]);
INET_NTOA(pThis->pRes.addrs[0], str, sizeof(str));
DBGPRINTF("%s", str);

void MyApp::getHost()
{
DBGPRINTF("getHost()");
if ( ISHELL_CreateInstance(pIShell, AEECLSID_NET, (void**)(&pNet) ) != SUCCESS)
{
DBGPRINTF("error getting INetMgr");
return;
}
CALLBACK_Init(&cb, netCallBack, this);
INETMGR_GetHostByName(pNet, &pRes, "msn.com", &cb);
}
//ERROR message on VC 6.0 for the line callback_init
error C2440: 'type cast' : cannot convert from '' to 'void (__cdecl *)(void *)'

This is the code snippet of what I am doing
//these are my class members
INetMgr * pNet;
AEEDNSResult pRes;
AEECallback cb;
//MyApp is the class name
void MyApp::netCallBack(MyApp *pThis)
{
char str[20];
int i = 0;
DBGPRINTF("netCallBack()");
i = pThis->pRes.nResult;
DBGPRINTF("nResult = %x", i);
DBGPRINTF("addrs[0] = %#x", pThis->pRes.addrs[0]);
INET_NTOA(pThis->pRes.addrs[0], str, sizeof(str));
DBGPRINTF("%s", str);

void MyApp::getHost()
{
DBGPRINTF("getHost()");
if ( ISHELL_CreateInstance(pIShell, AEECLSID_NET, (void**)(&pNet) ) != SUCCESS)
{
DBGPRINTF("error getting INetMgr");
return;
}
CALLBACK_Init(&cb, netCallBack, this);
INETMGR_GetHostByName(pNet, &pRes, "msn.com", &cb);
}
//ERROR message on VC 6.0 for the line callback_init
error C2440: 'type cast' : cannot convert from '' to 'void (__cdecl *)(void *)'

Quote:CALLBACK_Init(&cb, netCallBack, this); make netCallBack static function

Quote:CALLBACK_Init(&cb, netCallBack, this); make netCallBack static function

Yes, if you change the function as static function it would certainly work, however
why you can't use C++ member function as BREW callback function easily, well the answer lies in the fact that how member function is different from static function.
If you have the following
Class MyObj
{
int Func(int a, bool b);
;
it is equivalent of C function like
int Func(MyObj* this, int a, bool b)
it is possible to use a C++ member function as a callback function C function however we need to handle the ‘this’ parameter correctly.
class CTimer
{
void startTimer();
void doTimer();
;
int (* PFNCALLBACK)(void *);
int(CTimer::*PFNMEMBERCALLBACK)();
PFNCALLBACK functionCast( PFNMEMBERCALLBACK pMemberCallback)
{
union
{
PFNCALLBACK m_callback;
PFNMEMBERCALLBACK m_member;
} a;
a.m_member = pMemberCallback;
return a.m_callback;

void CTimer::startTimer()
{
ISHELL_SetTimer( pIShell, timeInterval, functionCast(&CTimer::doTImer), this );

Yes, if you change the function as static function it would certainly work, however
why you can't use C++ member function as BREW callback function easily, well the answer lies in the fact that how member function is different from static function.
If you have the following
Class MyObj
{
int Func(int a, bool b);
;
it is equivalent of C function like
int Func(MyObj* this, int a, bool b)
it is possible to use a C++ member function as a callback function C function however we need to handle the ‘this’ parameter correctly.
class CTimer
{
void startTimer();
void doTimer();
;
int (* PFNCALLBACK)(void *);
int(CTimer::*PFNMEMBERCALLBACK)();
PFNCALLBACK functionCast( PFNMEMBERCALLBACK pMemberCallback)
{
union
{
PFNCALLBACK m_callback;
PFNMEMBERCALLBACK m_member;
} a;
a.m_member = pMemberCallback;
return a.m_callback;

void CTimer::startTimer()
{
ISHELL_SetTimer( pIShell, timeInterval, functionCast(&CTimer::doTImer), this );

Hi ruben,
Great way. i will try to use it.
But i have one question when doTImer() is not taking any member variable why are you passing this pointer. :confused:

Hi ruben,
Great way. i will try to use it.
But i have one question when doTImer() is not taking any member variable why are you passing this pointer. :confused:

You don't need to pass the "this" pointer, I just left it for clarity

You don't need to pass the "this" pointer, I just left it for clarity

ok. Great thank you for your prompt reply.

ok. Great thank you for your prompt reply.