I wrote my program using PcommServer based on MFC in Visual Studio 2010. It worked very well until I put the pmacDevice.GetResponseEx into the callback function of timeSetEvent, which is a multi-media timer in winmm.lib.
Once it runs to GetResponseEx, exceptions as following will be shown:
"First-chance exception at 0x00000000 in ***.exe: 0xC0000005: Access violation reading location 0x00000000."
"Unhandled exception at 0x00000000 in ***.exe: 0xC0000005: Access violation reading location 0x00000000."
And I add CoInitialize(NULL) in the callback function, it still doesn't work.
My question is how to use pmacDevice.GetResponseEx in the timeSetEvent callback function?
I attach the code as:
timeSetEvent:
void CTestDlg::OnBnClickedBtntimer()
{
TIMECAPS timecaps;
UINT wAccuracy_Test,TimerID_Test;
if(timeGetDevCaps(&timecaps,sizeof(TIMECAPS))==TIMERR_NOERROR)
{
wAccuracy_Test= min(max(timecaps.wPeriodMin,1),timecaps.wPeriodMax);
timeBeginPeriod(wAccuracy_Test);
while (( TimerID_Test = timeSetEvent(1000, wAccuracy_Test,(LPTIMECALLBACK) TestProc, (DWORD)this,TIME_PERIODIC)) == NULL) ;//
}
}
callback function:
UINT CTestDlg::TestProc(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2 )
{
CoInitializeEx(NULL);
char cmdbuf[255];
BSTR retbuf = ::SysAllocString(L"");
BOOL pStatus;
((CTestDlg*)theApp.m_pMainWnd)->pmacDevice.GetResponse(0, "I122=3",FALSE, &retbuf,&pStatus);
::SysFreeString(retbuf);
CoUninitialize();
return 0;
}
Open device:
void CTestDlg::OnBnClickedBtnopen()
{
CoInitialize(NULL);
testInterface = pmacDevice.CreateDispatch(_T("PcommServer.PmacDevice.1"));
if (!testInterface)
{
AfxMessageBox("Fail to open library!");
return;
}
BOOL ret;
pmacDevice.SelectDevice(NULL,&m_nDevice,&ret);
int pbDriverOpen;
pmacDevice.Open(m_nDevice, &pbDriverOpen);
if(!pbDriverOpen)
{
AfxMessageBox("Fail to open Device!");
}
else
{
pmacDevice.GetDPRAvailable(m_nDevice);
}
}