callback address to waveOutOpen
Author |
Message |
Stratos Malasioti #1 / 8
|
 callback address to waveOutOpen
Hi all, I'm trying to pass the address of a local callback function to the waveOutOpen function. When I do : &FunctionName I get an error (the respective argument of waveOutOpen is DWORD) How can I get and pass the address of a function there? TIA Stratos
|
Tue, 09 Dec 2003 20:14:45 GMT |
|
 |
Joe Delekt #2 / 8
|
 callback address to waveOutOpen
Greets, One does not need to use the address-of operator '&' to get the address of the function. Instead, just pass the name of the function itself (with the appropriate signature) without the parentheses. Regards, Joe
Quote: > Hi all, > I'm trying to pass the address of a local callback function to the > waveOutOpen function. > When I do : &FunctionName I get an error (the respective argument > of waveOutOpen is DWORD) > How can I get and pass the address of a function there? > TIA > Stratos
|
Tue, 09 Dec 2003 20:37:55 GMT |
|
 |
Stratos Malasioti #3 / 8
|
 callback address to waveOutOpen
Thanks for the reply Joe, Unfortunately (if I am not mistaken again), VC doesn't allow me to pass the name of the callback function directly in waveOutOpen, just like we would do when passing the address of TimerProc in SetTimer (Win32 - not MFC) because waveOutOpen is not designed to take a pointer to a function (TIMERPROC pointer in the example of SetTimer) but just a DWORD. When I try to pass it directly as: waveOutOpen(..... , (DWORD)WaveOutProc, ...) the de{*filter*} says that the compiller cannot cast the function to DWORD. Am I missing something else here? Thanks again, Stratos Quote:
> Greets, > One does not need to use the address-of operator '&' to get the address > of the function. Instead, just pass the name of the function itself (with > the appropriate signature) without the parentheses. > Regards, > Joe
> > Hi all, > > I'm trying to pass the address of a local callback function to the > > waveOutOpen function. > > When I do : &FunctionName I get an error (the respective argument > > of waveOutOpen is DWORD) > > How can I get and pass the address of a function there? > > TIA > > Stratos
|
Tue, 09 Dec 2003 20:49:10 GMT |
|
 |
Joe Delekt #4 / 8
|
 callback address to waveOutOpen
Greets, You are correct, that was my oversight. You will need to cast the function pointer to a DWORD value, however, the address of the function as I stated before still stands. For example: (DWORD)WaveOutProc I'm not sure why your de{*filter*} is saying that your compiler cannot cast the function to DWORD or how the de{*filter*} even fits into the picture. However, just cast the function name (without parentheses) to a DWORD value. If the compiler issues an error what exactly is this error? Regards, Joe
Quote: > Thanks for the reply Joe, > Unfortunately (if I am not mistaken again), VC doesn't allow me to pass the > name of the callback function directly in waveOutOpen, just like we would do > when passing the address of TimerProc in SetTimer (Win32 - not MFC) because > waveOutOpen is not designed to take a pointer to a function (TIMERPROC pointer > in the example of SetTimer) but just a DWORD. > When I try to pass it directly as: waveOutOpen(..... , (DWORD)WaveOutProc, ...) > the de{*filter*} says that the compiller cannot cast the function to DWORD. > Am I missing something else here? > Thanks again, > Stratos
> > Greets, > > One does not need to use the address-of operator '&' to get the address > > of the function. Instead, just pass the name of the function itself (with > > the appropriate signature) without the parentheses. > > Regards, > > Joe
> > > Hi all, > > > I'm trying to pass the address of a local callback function to the > > > waveOutOpen function. > > > When I do : &FunctionName I get an error (the respective argument > > > of waveOutOpen is DWORD) > > > How can I get and pass the address of a function there? > > > TIA > > > Stratos
|
Tue, 09 Dec 2003 21:40:37 GMT |
|
 |
Stratos Malasioti #5 / 8
|
 callback address to waveOutOpen
I get the error : g:\library\codelib\cpp\soundsource\wavesoundgenerator.cpp(115) : error C2440: 'type cast' : cannot convert from 'void (__stdcall CWaveSoundGenerator::*)(struct HWAVEOUT__ *,unsigned int, unsigned long,unsigned long,unsigned long)' to 'unsigned long' Conversion is a valid standard conversion, which can be performed implicitly or by use of static_cast, C-style cast or function-style cast when I do: waveOutOpen (&hWaveOut,WAVE_MAPPER,&WaveFormat,(DWORD)waveOutProc,0,CALLBACK_FUNCTION) ( The function is declared as : MMRESULT waveOutOpen( LPHWAVEOUT phwo, UINT uDeviceID, LPWAVEFORMATEX pwfx, DWORD dwCallback, DWORD dwCallbackInstance, DWORD fdwOpen ); ) Any ideas ? TIA Quote:
> Greets, > You are correct, that was my oversight. You will need to cast the > function pointer to a DWORD value, however, the address of the function as I > stated before still stands. For example: > (DWORD)WaveOutProc > I'm not sure why your de{*filter*} is saying that your compiler cannot cast > the function to DWORD or how the de{*filter*} even fits into the picture. > However, just cast the function name (without parentheses) to a DWORD value. > If the compiler issues an error what exactly is this error? > Regards, > Joe
> > Thanks for the reply Joe, > > Unfortunately (if I am not mistaken again), VC doesn't allow me to pass > the > > name of the callback function directly in waveOutOpen, just like we would > do > > when passing the address of TimerProc in SetTimer (Win32 - not MFC) > because > > waveOutOpen is not designed to take a pointer to a function (TIMERPROC > pointer > > in the example of SetTimer) but just a DWORD. > > When I try to pass it directly as: waveOutOpen(..... , (DWORD)WaveOutProc, > ...) > > the de{*filter*} says that the compiller cannot cast the function to DWORD. > > Am I missing something else here? > > Thanks again, > > Stratos
> > > Greets, > > > One does not need to use the address-of operator '&' to get the > address > > > of the function. Instead, just pass the name of the function itself > (with > > > the appropriate signature) without the parentheses. > > > Regards, > > > Joe
> > > > Hi all, > > > > I'm trying to pass the address of a local callback function to the > > > > waveOutOpen function. > > > > When I do : &FunctionName I get an error (the respective argument > > > > of waveOutOpen is DWORD) > > > > How can I get and pass the address of a function there? > > > > TIA > > > > Stratos
|
Tue, 09 Dec 2003 22:01:21 GMT |
|
 |
Joe Delekt #6 / 8
|
 callback address to waveOutOpen
Greets, The problem is that you are trying to pass a class member function as your callback. To do this, you will need to make your class member function static. The problem with doing this, however, is that your callback will not have a 'this' pointer. You can, however, when making the call, pass the 'this' pointer as the callback instance parameter. When you receive the callback, cast the callback instance parameter passed back to you to a pointer to your class, then access the member data / functions through this pointer. Regards, Joe
Quote: > I get the error : > g:\library\codelib\cpp\soundsource\wavesoundgenerator.cpp(115) : error C2440: > 'type cast' : cannot convert from 'void (__stdcall > CWaveSoundGenerator::*)(struct HWAVEOUT__ *,unsigned int, > unsigned long,unsigned long,unsigned long)' to 'unsigned long' > Conversion is a valid standard conversion, which can be performed implicitly or > by use of static_cast, C-style cast or function-style cast > when I do: > waveOutOpen > (&hWaveOut,WAVE_MAPPER,&WaveFormat,(DWORD)waveOutProc,0,CALLBACK_FUNCTION) > ( > The function is declared as : > MMRESULT waveOutOpen( LPHWAVEOUT phwo, UINT uDeviceID, > LPWAVEFORMATEX pwfx, DWORD dwCallback, DWORD dwCallbackInstance, > DWORD fdwOpen ); > ) > Any ideas ? > TIA
> > Greets, > > You are correct, that was my oversight. You will need to cast the > > function pointer to a DWORD value, however, the address of the function as I > > stated before still stands. For example: > > (DWORD)WaveOutProc > > I'm not sure why your de{*filter*} is saying that your compiler cannot cast > > the function to DWORD or how the de{*filter*} even fits into the picture. > > However, just cast the function name (without parentheses) to a DWORD value. > > If the compiler issues an error what exactly is this error? > > Regards, > > Joe
> > > Thanks for the reply Joe, > > > Unfortunately (if I am not mistaken again), VC doesn't allow me to pass > > the > > > name of the callback function directly in waveOutOpen, just like we would > > do > > > when passing the address of TimerProc in SetTimer (Win32 - not MFC) > > because > > > waveOutOpen is not designed to take a pointer to a function (TIMERPROC > > pointer > > > in the example of SetTimer) but just a DWORD. > > > When I try to pass it directly as: waveOutOpen(..... , (DWORD)WaveOutProc, > > ...) > > > the de{*filter*} says that the compiller cannot cast the function to DWORD. > > > Am I missing something else here? > > > Thanks again, > > > Stratos
> > > > Greets, > > > > One does not need to use the address-of operator '&' to get the > > address > > > > of the function. Instead, just pass the name of the function itself > > (with > > > > the appropriate signature) without the parentheses. > > > > Regards, > > > > Joe
> > > > > Hi all, > > > > > I'm trying to pass the address of a local callback function to the > > > > > waveOutOpen function. > > > > > When I do : &FunctionName I get an error (the respective argument > > > > > of waveOutOpen is DWORD) > > > > > How can I get and pass the address of a function there? > > > > > TIA > > > > > Stratos
|
Tue, 09 Dec 2003 22:12:12 GMT |
|
 |
jonatha #7 / 8
|
 callback address to waveOutOpen
Here's an example: /////////////////////////////////////////////////////////////////////////// / ///////////////////////////////// //... if((m_Result = waveOutOpen( &m_pHandleWaveOut, device, &m_pWaveFormat, (DWORD)WavHandler, (DWORD)(Your_class_name*)this, CALLBACK_FUNCTION)) != MMSYSERR_NOERROR ) { return false; } //... // This function is declared global void FAR Pascal WavHandler(DWORD l_dwDevice, WORD l_wMsg, DWORD l_dwInstanceData, DWORD l_dwWaveHdr, DWORD l_dwParam3) { //... Quote: }
//////////////////////////////////////////////////////////////////////////// //////////////////////// But I would suggest you to use events instead of callback functions. A good example can be found at http://www.relisoft.com/Freeware/recorder.html In MSDN library "waveOutProc" you can read: Applications should not call any system-defined functions from inside a callback function, except for EnterCriticalSection, LeaveCriticalSection, midiOutLongMsg, midiOutShortMsg, OutputDebugString, PostMessage, PostThreadMessage, SetEvent, timeGetSystemTime, timeGetTime, timeKillEvent, and timeSetEvent. Calling other wave functions will cause deadlock. And I had a lot of deadlock just trying to delete objects. I hope this will help. Jonathan
Quote: > Hi all, > I'm trying to pass the address of a local callback function to the > waveOutOpen function. > When I do : &FunctionName I get an error (the respective argument > of waveOutOpen is DWORD) > How can I get and pass the address of a function there? > TIA > Stratos
|
Tue, 09 Dec 2003 23:30:52 GMT |
|
 |
Joe Delekt #8 / 8
|
 callback address to waveOutOpen
Greets, Just to add further to your example; one will need to make sure that this construct is used within a class member, otherwise, the 'this' pointer is not available. The double cast "(DWORD)(Your_class_name*)this" is superfluous. Using "(DWORD)this" should be sufficient. However, when the callback is received in the static member function, one will need to use: Your_class_name* pInstance=(Your_class_name*)dwCallbackInstance; One will then have all the benefits of membership through the pointer to the class instance. :) Regards, Joe
Quote: > Here's an example:
/////////////////////////////////////////////////////////////////////////// / Quote: > ///////////////////////////////// > //... > if((m_Result = waveOutOpen( &m_pHandleWaveOut, > device, > &m_pWaveFormat, > (DWORD)WavHandler, > (DWORD)(Your_class_name*)this, > CALLBACK_FUNCTION)) != MMSYSERR_NOERROR ) > { > return false; > } > //... > // This function is declared global > void FAR PASCAL WavHandler(DWORD l_dwDevice, > WORD l_wMsg, > DWORD l_dwInstanceData, > DWORD l_dwWaveHdr, > DWORD l_dwParam3) > { > //... > }
//////////////////////////////////////////////////////////////////////////// Quote: > //////////////////////// > But I would suggest you to use events instead of callback functions. A good > example can be found at http://www.relisoft.com/Freeware/recorder.html > In MSDN library "waveOutProc" you can read: > Applications should not call any system-defined functions from inside a > callback function, except for EnterCriticalSection, LeaveCriticalSection, > midiOutLongMsg, midiOutShortMsg, OutputDebugString, PostMessage, > PostThreadMessage, SetEvent, timeGetSystemTime, timeGetTime, timeKillEvent, > and timeSetEvent. Calling other wave functions will cause deadlock. > And I had a lot of deadlock just trying to delete objects. > I hope this will help. > Jonathan
> > Hi all, > > I'm trying to pass the address of a local callback function to the > > waveOutOpen function. > > When I do : &FunctionName I get an error (the respective argument > > of waveOutOpen is DWORD) > > How can I get and pass the address of a function there? > > TIA > > Stratos
|
Tue, 09 Dec 2003 23:52:26 GMT |
|
|
|