Callback function question

BlitzMax Forums/BlitzMax Programming/Callback function question

semar(Posted 2005) [#1]
All,
I have this API function which requires a callback function:
Function midiInOpen(lphMidiIn:Int Var, uDeviceID:Int, dwCallback:Int ,dwInstance:Int, dwFlags:Int)


The callback function:
Function MidiCallBackBM:Int(hmIN:Int, wMsg:Int, dwInstance:Int, dwParam1:Int, dwParam2:Int)"Win32"


The above callback function works.

But, if I remove the "Win32" bit at the end of the function declaration, I get:

Unhandled Exception: Unhandled Memory Exception Error.



The questions:
- what is that "Win32" for ?
- When and where to use it ?
- Is there any documentation to read about ?

Thanks for your attention,
Sergio.


Sweenie(Posted 2005) [#2]
"Win32" tells the compiler that this function is using the Standard Calling Convention.
Basically this means that the parameterstack are to be cleaned by the called application/function(the dll in this case).

Read this...
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_core___stdcall.asp

If you remove "Win32", you will tell the compiler to use the C, C++ calling Convention.
Using this calling convention, the application calling the function is responsible for cleaning the stack(Bmax).

Now if you call the function using the C/C++ convention but the dll is using the Standard Convention I suppose both Bmax and the dll will try to clean the stack.

You can see most of the time what kind of convention a dll is using by checking the decoration of the exported functions.
They often start with an underscore(_) and end with a @ followed by the size of the parameters.


semar(Posted 2005) [#3]
Thank you Sweenie, that shed a bit of light indeed !

So, to figure out if a function needs to be called with a standard calling convention, or with a C,C++ calling convention, as you stated, has to be found taking a look at the exported function decoration.

For example the callback function, which is a parameter in the MidiInOpen function, is described by MidiInProc:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/multimed/htm/_win32_midiinproc.asp

Since it has no 'decoration', if I understand right, that means that is a a standard calling convention. So, "Win32" have to been used at the end of the function declaration.

Or am I missing something ?

Sergio.


Sweenie(Posted 2005) [#4]
Well, the callback function isn't an exported function so you cannot tell by it's name.
However, by reading the MSDN library you can see that the callback function is defined as void CALLBACK and CALLBACK is simply a definition for _stdcall(which is the standard calling convention)

According to MSDN, all Win32 API callbacks are always using the standard calling convention and so does all Win32 API functions.
That's why I'm a bit confused why your midiInOpen function works even though you haven't added "win32" to it.
Have you tried calling "midiInOpen" several times? Because sometimes you won't get an error unless you call it more than once.


semar(Posted 2005) [#5]
That's why I'm a bit confused why your midiInOpen function works even though you haven't added "win32" to it.
That function is an API function, and in the BMAX code is declared between the 'Extern' statements:
Extern "Os"
Function midiInOpen(lphMidiIn:Int Var, uDeviceID:Int, dwCallback:Int ,dwInstance:Int, dwFlags:Int)
End Extern

Have you tried calling "midiInOpen" several times? Because sometimes you won't get an error unless you call it more than once.
Well, I call it one time only, then read the midi input, then close it.
So I didn't try to call it several time because I can't call it more than once - I mean, when the midi input is already opened, I can't re-open it.

By the way, thank you for the explanation. The more I read on this forum, the more I learn.
The problem is that I forget it later !!!

Sergio.