CALLBACK macro (QT)

2019-09-13 19:42发布

问题:

I've got a function with a signature like this

int iV_SetSampleCallback(pDLLSetSample pSampleCallbackFunction)  

Where pDLLSetSample is

typedef int (CALLBACK * pDLLSetSample) (struct SampleStruct rawDataSample);

I want to pass my member function as callback

int sampleCallbackFunction(struct SampleStruct sample);

And when I call it like

iV_SetSampleCallback(&MainWindow::sampleCallbackFunction);

I got an error

error: C2664: 'int iV_SetSampleCallback(pDLLSetSample)': cannot convert argument 1 from 'int (__thiscall MainWindow::* )(SampleStruct)' to 'pDLLSetSample'

And I can't understand why it happens. What is CALLBACK macro is? Where is __thiscall came from? How should I correctly pass my callback to this function?

回答1:

Your error is occurring because you are trying to pass a pointer to a non-static class member function sampleCallbackFunction instead of a pointer to a regular function. Note that non-static class member functions have explicit this parameter. CALLBACK macro is often used in Windows and typically stands for stdcall calling convention.

To fix this error you need to

  • declare member callback function as static
  • prepend CALLBACK macro to member function so it will have expected calling convention (or whatever CALLBACK expands to)
static int CALLBACK sampleCallbackFunction(struct SampleStruct sample);


标签: c++ qt macros