Multimedia Timer Problem in MFC VS2010
-
I want to use a mm_timer for my application by 60Hz or less :-D . This is my testing code. - In "testmmtimerDlg.h" header file:
#include "mmsystem.h"
#pragma once
#pragma comment(lib, "winmm.lib")public:
double m_dcount;
static void CALLBACK TimeProc(UINT wTimerID, UINT msg, DWORD dwUser, DWORD dw1, DWORD dw2);void MMTimerHandler(UINT nIDEvent); void StopTimer(); MMRESULT m\_Timer; DWORD resolution;</pre>
- In "testmmtimerDlg.cpp" file:
void CtestmmtimerDlg::OnBnClickedTest()
{
// TODO: Add your control notification handler code here
m_dcount = 0;
// Set resolution to the minimum supported by the systemTIMECAPS tc; timeGetDevCaps(&tc, sizeof(TIMECAPS)); resolution = min(max(tc.wPeriodMin, 0), tc.wPeriodMax); timeBeginPeriod(resolution); // create the timer m\_Timer = timeSetEvent(10,resolution,TimeProc,(DWORD)this,TIME\_PERIODIC);
}
void CALLBACK TimeProc(UINT wTimerID, UINT msg, DWORD dwUser, DWORD dw1, DWORD dw2)
{
// This is used only to call MMTimerHandler// Typically, this function is static member of CTimersDlg CtestmmtimerDlg\* obj = (CtestmmtimerDlg\*) dwUser; obj->MMTimerHandler(wTimerID);
}
void CtestmmtimerDlg::StopTimer()
{
// destroy the timer
timeKillEvent(m_Timer);// reset the timer timeEndPeriod (resolution);
}
==>> But when I try to debug, I have 2 problem:
- First, "
testmmtimerDlg.obj : error LNK2019: unresolved external symbol "public: static void __stdcall CtestmmtimerDlg::TimeProc(unsigned int,unsigned int,unsigned long,unsigned long,unsigned long)" (?TimeProc@CtestmmtimerDlg@@SGXIIKKK@Z) referenced in function "public: void __thiscall CtestmmtimerDlg::OnBnClickedTest(void)" (?OnBnClickedTest@CtestmmtimerDlg@@QAEXXZ)
1>C:\Users\HungReo\Documents\Visual Studio 2010\Projects\testmmtimer\Debug\testmmtimer.exe : fatal error LNK1120: 1 unresolved externals"
- Second, in header file, I try to change:
void (CALLBACK *TimeProc)(UINT wTimerID, UINT msg, DWORD dwUser, DWORD dw1, DWORD dw2);
==>>>> OxC0000005: Access Violoation
- May someone help me to solve these problem???
-
I want to use a mm_timer for my application by 60Hz or less :-D . This is my testing code. - In "testmmtimerDlg.h" header file:
#include "mmsystem.h"
#pragma once
#pragma comment(lib, "winmm.lib")public:
double m_dcount;
static void CALLBACK TimeProc(UINT wTimerID, UINT msg, DWORD dwUser, DWORD dw1, DWORD dw2);void MMTimerHandler(UINT nIDEvent); void StopTimer(); MMRESULT m\_Timer; DWORD resolution;</pre>
- In "testmmtimerDlg.cpp" file:
void CtestmmtimerDlg::OnBnClickedTest()
{
// TODO: Add your control notification handler code here
m_dcount = 0;
// Set resolution to the minimum supported by the systemTIMECAPS tc; timeGetDevCaps(&tc, sizeof(TIMECAPS)); resolution = min(max(tc.wPeriodMin, 0), tc.wPeriodMax); timeBeginPeriod(resolution); // create the timer m\_Timer = timeSetEvent(10,resolution,TimeProc,(DWORD)this,TIME\_PERIODIC);
}
void CALLBACK TimeProc(UINT wTimerID, UINT msg, DWORD dwUser, DWORD dw1, DWORD dw2)
{
// This is used only to call MMTimerHandler// Typically, this function is static member of CTimersDlg CtestmmtimerDlg\* obj = (CtestmmtimerDlg\*) dwUser; obj->MMTimerHandler(wTimerID);
}
void CtestmmtimerDlg::StopTimer()
{
// destroy the timer
timeKillEvent(m_Timer);// reset the timer timeEndPeriod (resolution);
}
==>> But when I try to debug, I have 2 problem:
- First, "
testmmtimerDlg.obj : error LNK2019: unresolved external symbol "public: static void __stdcall CtestmmtimerDlg::TimeProc(unsigned int,unsigned int,unsigned long,unsigned long,unsigned long)" (?TimeProc@CtestmmtimerDlg@@SGXIIKKK@Z) referenced in function "public: void __thiscall CtestmmtimerDlg::OnBnClickedTest(void)" (?OnBnClickedTest@CtestmmtimerDlg@@QAEXXZ)
1>C:\Users\HungReo\Documents\Visual Studio 2010\Projects\testmmtimer\Debug\testmmtimer.exe : fatal error LNK1120: 1 unresolved externals"
- Second, in header file, I try to change:
void (CALLBACK *TimeProc)(UINT wTimerID, UINT msg, DWORD dwUser, DWORD dw1, DWORD dw2);
==>>>> OxC0000005: Access Violoation
- May someone help me to solve these problem???
void (CALLBACK *TimeProc)(UINT wTimerID, UINT msg, DWORD dwUser, DWORD dw1, DWORD dw2);
You are declaring
TimeProc
as a pointer to a function which is totally incorrect. It should be like:// in the class definition in the header file:
void CALLBACK TimeProc(UINT wTimerID, UINT msg, DWORD dwUser, DWORD dw1, DWORD dw2);// and the declaration in the .cpp file:
void CALLBACK CtestmmtimerDlg::TimeProc(UINT wTimerID, UINT msg, DWORD dwUser, DWORD dw1, DWORD dw2)
{
// implementation code here
} -
void (CALLBACK *TimeProc)(UINT wTimerID, UINT msg, DWORD dwUser, DWORD dw1, DWORD dw2);
You are declaring
TimeProc
as a pointer to a function which is totally incorrect. It should be like:// in the class definition in the header file:
void CALLBACK TimeProc(UINT wTimerID, UINT msg, DWORD dwUser, DWORD dw1, DWORD dw2);// and the declaration in the .cpp file:
void CALLBACK CtestmmtimerDlg::TimeProc(UINT wTimerID, UINT msg, DWORD dwUser, DWORD dw1, DWORD dw2)
{
// implementation code here
}Thank you very much for your help! But when I try to follow your code, the error will be:
error C3867: 'CtestmmtimerDlg::TimeProc': function call missing argument list; use '&CtestmmtimerDlg::TimeProc' to create a pointer to member
-
void (CALLBACK *TimeProc)(UINT wTimerID, UINT msg, DWORD dwUser, DWORD dw1, DWORD dw2);
You are declaring
TimeProc
as a pointer to a function which is totally incorrect. It should be like:// in the class definition in the header file:
void CALLBACK TimeProc(UINT wTimerID, UINT msg, DWORD dwUser, DWORD dw1, DWORD dw2);// and the declaration in the .cpp file:
void CALLBACK CtestmmtimerDlg::TimeProc(UINT wTimerID, UINT msg, DWORD dwUser, DWORD dw1, DWORD dw2)
{
// implementation code here
}I finished to run it ^^. Just move the CALLBACK function before click button and don't need to declare CALLBACK in header file. Thank you so much for your help ^^