473,796 Members | 2,903 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
SetTimerCallbac kFunction. 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 SetTimerCallbac kFunction(MMTIM ERCALLBACK 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::SetTi merCallbackFunc tion(MMTIMERCAL LBACK pCallback)

{

m_pCallback = pCallback;

}

BOOL CMMTimer::Start (UINT uPeriod)

{

if (!m_bInitialize d) 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(<T IMERT, 0, 0); #error#

// and this

pThis->m_pCallback(<T IMERT, 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 0xccccccccccccc c

I really would like this problem solved.

Thank you

pvdm

}
--
Posted via http://dbforums.com
Jul 19 '05 #1
1 8869
On Tue, 09 Sep 2003 05:02:58 -0400, pvdm <me*********@db forums.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
SetTimerCallba ckFunction. 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 SetTimerCallbac kFunction(MMTIM ERCALLBACK 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::SetTi merCallbackFunc tion(MMTIMERCAL LBACK pCallback)

{

m_pCallback = pCallback;

}

BOOL CMMTimer::Start (UINT uPeriod)
{
if (!m_bInitialize d) 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(<T IMERT, 0, 0); #error#
That looks correct, apart from "<TIMERT" which isn't valid C++.
// and this

pThis->m_pCallback(<T IMERT, 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 0xccccccccccccc c
That suggests that m_pCallback hasn't been initialized. Perhaps you
are calling SetTimerCallbac kFunction 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
2045
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 causes my program to crash or freeze, or do strange stuff. When I remove this callback function it works great. Problem is I need this callback function to implement my state machine for the program so I think I need to encapsulate this callback...
0
3942
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. It is almost like it is trying to implement it's own COM interfaces... below is the header, and a link to the dll+code: Zip file with header, example, and DLL:...
4
5144
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
7592
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
3455
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
7684
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 a .NET application and thus am looking into writing a managed C++ wrapper for in vs2005. Furthermore, this library has many callback hooks which need to be implemented by the C++ wrapper. These callback functions are declared as "extern C...
2
2637
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 function... I have mentioned my doubts in code comments. My code is... void myfun_callback(void(*fp)(char*)) //is this function is a call back? {
10
7001
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 MyEnumWindowsProc(HWND hwnd, LPARAM lparam) {
6
2164
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 right that if function A is: function A(*function pointer to B aka callback func, other arguments) { call (B); //calls }
0
9679
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9527
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10172
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9050
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.