gpt4 book ai didi

c++ - 将类成员回调从 __stdcall 转换为 DWORD_PTR

转载 作者:行者123 更新时间:2023-11-30 01:32:08 24 4
gpt4 key购买 nike

我正在尝试使用类成员作为回调,但编译器给我以下错误:

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

是否可以通过这种方式将成员函数用作回调?以及如何将 stdcall 成员指针转换为 winapi 函数请求的 DWORD_PTR?

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

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

最佳答案

不能直接传入类方法。

这是正确的方法:

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);
}

关于c++ - 将类成员回调从 __stdcall 转换为 DWORD_PTR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1994910/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com