开发者

convert Class member callback from __stdcall to DWORD_PTR

开发者 https://www.devze.com 2022-12-15 03:21 出处:网络
I\'m trying to use a class member as a callback but the compiler gives me the following error: Error 2 error C2440: \'type cast\' : cannot convert from \'void (__stdcall CWaveIn::* )(HWAVEIN,UINT,DW

I'm trying to use a class member as a callback but the compiler gives me the following error:

Error 2 error C2440: 'type cast' : cannot convert from 'void (__stdcall CWaveIn::* )(HWAVEIN,UINT,DWORD_PTR,DWORD_PTR,DWORD_PTR)' to 'DWORD_PTR'

Is it possible to use a member function as a callback this way? and how do I convert the stdcall member pointer to the DWORD_PTR requested by the winapi function?

class CWaveIn
{
private:
    void CALLBACK WaveInProc(HWAVEIN hwi, UINT uMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD_PTR dwParam2);
};

void CWaveIn::Open() 
{
    (...)
    MMRESULT r开发者_开发百科esult = ::waveInOpen(&hWaveIn, currentInputDeviceId, waveFormat, (DWORD_PTR)CWaveIn::WaveInProc, 0, CALLBACK_FUNCTION | WAVE_FORMAT_DIRECT);
}


You cannot directly pass in class methods.

This is the right way :

class CWaveIn
{
private:
    static void CALLBACK staticWaveInProc(HWAVEIN hwi, UINT uMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
    {
        CWaveIn* pThis = reinterpret_cast<CWaveIn*>( dwParam1 );
        pThis->WaveInProc( ... );
    }
    void WaveInProc(HWAVEIN hwi, UINT uMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
    {
       // your code
    }
};

void CWaveIn::Open() 
{
     (...)
     MMRESULT result = ::waveInOpen(&hWaveIn, currentInputDeviceId, waveFormat, CWaveIn::staticWaveInProc, this, CALLBACK_FUNCTION | WAVE_FORMAT_DIRECT);
}


The general, though by no means perfect, solution is to make the function static.


Is it possible to use a member function as a callback this way?

No. They have different signatures. A member function expects an implicit this parameter in addition to the ones listed. It can not be called as a non-member function.

and how do I convert the stdcall member pointer to the DWORD_PTR requested by the winapi function?

You can't. You'll have to write a wrapper function to use as callback instead. It should be either a static member function, or a non-member function.

0

精彩评论

暂无评论...
验证码 换一张
取 消