473,324 Members | 2,214 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,324 software developers and data experts.

Using function pointer in callback function


Hi,

I am writing an app which encapsulates a multimedia timer. I implemented
a TimerProc as static member function and a static member variable pThis
as pseudo this variable to access in the static TimerProc function.
timeSetEvent uses TimerProc to set the callback function.

m_pCallback is a function that is passed using the
SetTimerCallbackFunction. It's a function that an object owning the
CMMTimer object can pass to the CMMTimer class. What I want is that
every time the TimerProc function is called, the m_pCallback function is
executed, resulting in the object owning the timer processing the timer.

Header file looks like this:

/***************************************/

typedef void (CALLBACK * MMTIMERCALLBACK) (TRIGGER eTrigger, UINT uID,
int iVal);

CMMTimer

{

public:

BOOL Stop();

BOOL Start(UINT uPeriod);

BOOL Initialize(UINT uResolution);

CMMTimer();

virtual ~CMMTimer();

void SetTimerCallbackFunction(MMTIMERCALLBACK pCallback);

private:

MMTIMERCALLBACK m_pCallback;

UINT m_uResolution;

UINT m_uPeriod;

UINT m_uTimerID;

BOOL m_bInitialized;

static CMMTimer *pThis;

static void CALLBACK TimerProc(UINT wTimerID, UINT msg, DWORD dwUser,
DWORD dw1, DWORD dw2);

}

/***************************************/

Parts of cpp file look like this

/***************************************/

CMMTimer* CMMTimer::pThis = NULL;

void CMMTimer::SetTimerCallbackFunction(MMTIMERCALLBACK pCallback)

{

m_pCallback = pCallback;

}

BOOL CMMTimer::Start(UINT uPeriod)

{

if (!m_bInitialized) return FALSE;

m_uPeriod = uPeriod;

pThis = this;

m_uTimerID =

timeSetEvent(

m_uPeriod,

m_uResolution,

TimerProc,

(DWORD) this,

TIME_PERIODIC );

if(! m_uTimerID)

return FALSE;

else

return TRUE;

}

void CALLBACK TimerProc(UINT wTimerID, UINT msg, DWORD dwUser, DWORD
dw1, DWORD dw2)

{

// I tried this

CMMTimer * pseudoThis = (CMMTimer *) dwUser;

pseudoThis->m_pCallback(<TIMERT, 0, 0); #error#

// and this

pThis->m_pCallback(<TIMERT, 0, 0); #error#

}

I also tried helper functions.

The problem is that this code compiles perfectly, but when I execute the
code, the debugger shows problems at the lines #error#. m_pCallback
cannot be called, the pointer points at memory address 0xcccccccccccccc

I really would like this problem solved.

Thank you

pvdm

}
--
Posted via http://dbforums.com
Jul 19 '05 #1
1 8812
On Tue, 09 Sep 2003 05:02:58 -0400, pvdm <me*********@dbforums.com>
wrote:

Hi,

I am writing an app which encapsulates a multimedia timer. I implemented
a TimerProc as static member function and a static member variable pThis
as pseudo this variable to access in the static TimerProc function.
Why do you need pThis? You are passing the this argument to the
callback anyway.
timeSetEvent uses TimerProc to set the callback function. m_pCallback is a function that is passed using the
SetTimerCallbackFunction. It's a function that an object owning the
CMMTimer object can pass to the CMMTimer class. What I want is that
every time the TimerProc function is called, the m_pCallback function is
executed, resulting in the object owning the timer processing the timer.
Right, sounds reasonable.
Header file looks like this:

typedef void (CALLBACK * MMTIMERCALLBACK) (TRIGGER eTrigger, UINT uID,
int iVal);

CMMTimer
class CMMTimer?

{

public:

BOOL Stop();

BOOL Start(UINT uPeriod);

BOOL Initialize(UINT uResolution);

CMMTimer();

virtual ~CMMTimer();

void SetTimerCallbackFunction(MMTIMERCALLBACK pCallback);

private:

MMTIMERCALLBACK m_pCallback;

UINT m_uResolution;

UINT m_uPeriod;

UINT m_uTimerID;

BOOL m_bInitialized;

static CMMTimer *pThis;
I don't see any use of pThis. Remove it.

static void CALLBACK TimerProc(UINT wTimerID, UINT msg, DWORD dwUser,
DWORD dw1, DWORD dw2);

}
CMMTimer* CMMTimer::pThis = NULL;
Again, scrub this.
void CMMTimer::SetTimerCallbackFunction(MMTIMERCALLBACK pCallback)

{

m_pCallback = pCallback;

}

BOOL CMMTimer::Start(UINT uPeriod)
{
if (!m_bInitialized) return FALSE;
m_uPeriod = uPeriod;
pThis = this;
What if you have more than one timer at a time?

In any case, you need to check the callback here:

if (!m_pCallback)
{
return FALSE;
}

and make sure m_pCallback is initialized to NULL by your constructor.
m_uTimerID =
timeSetEvent(
m_uPeriod,
m_uResolution,
TimerProc,
(DWORD) this,
TIME_PERIODIC );
if(! m_uTimerID)
return FALSE;
else
return TRUE;
}
That looks ok.
void CALLBACK TimerProc(UINT wTimerID, UINT msg, DWORD dwUser, DWORD
dw1, DWORD dw2)

{

// I tried this

CMMTimer * pseudoThis = (CMMTimer *) dwUser;

pseudoThis->m_pCallback(<TIMERT, 0, 0); #error#
That looks correct, apart from "<TIMERT" which isn't valid C++.
// and this

pThis->m_pCallback(<TIMERT, 0, 0); #error#
No need for this. Where is pThis initialized, anyway?
}

I also tried helper functions.
How so?
The problem is that this code compiles perfectly, but when I execute the
code, the debugger shows problems at the lines #error#. m_pCallback
cannot be called, the pointer points at memory address 0xcccccccccccccc
That suggests that m_pCallback hasn't been initialized. Perhaps you
are calling SetTimerCallbackFunction on a different instance of your
timer class to the one you call Start on.
I really would like this problem solved.


The code you've posted looks generally ok. It must be the code using
your timer class that is at fault. However, drop the static member
variable, and add the test to Start, and you'll probably find your
problem.

Tom
Jul 19 '05 #2

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

Similar topics

3
by: Jim | last post by:
I have a windows application that is accessing unmanaged code and providing a callback function that will create events at unknown time spans. When the EventHandler (callback function) fires it...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
4
by: H.B. | last post by:
Hi, I successfully implement a static callback function for my dll usign delegates. Now, I need to use member function instead of static function. How can I make that (in Managed C++). Hugo
1
by: pheres | last post by:
Hi, I'm trying to pass pointers to member functions around in my code. The test with pointers to non-member function works fine: Code: void callOut( void (*callback)() ) { callback();
6
by: JDT | last post by:
Hi, Can we pass a member function in a class as a callback function? Someone instucted me that I can only use a static functon or a global function as a callback. Your help is appreciated. JD
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: Pradeep | last post by:
Hi all, Can any one explain me what is callback function.... I have written some code after reading some tutorials from internet... But I am not sure is it a right way to write a call back...
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...
6
by: jmDesktop | last post by:
In a function that takes another function (function pointer) as a argument, or the callback function, which is the one that "calls back"? I'm having a hard time understanding the language. Am I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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....

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.