Here is another try at using DllCall and my problem - please help me, if you see how.
I want to do something simple first: open then close midiIn device.
So, to begin with:
Code:
h_midiin := "0000" ; Important!
result := DllCall("winmm.dll\midiInOpen"
, UInt, &h_midiin
, UInt, 0 ;; uDeviceID
, UInt, 0
, UInt, 0
, UInt, 0 ;; dwFlags - set to CALLBACK_NULL (0)
, "UInt")
So far so good.
h_midiin is set with a "handle" to my midiIn device, since midiInOpen expects a pointer to the variable (to which we allocated space through h_midiin = "0000".
By using *(&h_midiin + offset) I can see that the 4 bytes of this handle are set as following 32, 65, 5, 0.
To me this signifies that this "handle" = 0x20410500 = 541132032 in decimal.
Well, so then to close this open device I need to supply midiInClose function with this handle (not the pointer to the handle, but the actual handle). So I first calculate that handle value above by this convoluted manner:
Code:
midi_in_handle_uint0 := *(&h_midiin + 0)
midi_in_handle_uint1 := *(&h_midiin + 1)
midi_in_handle_uint2 := *(&h_midiin + 2)
midi_in_handle_uint3 := *(&h_midiin + 3)
midi_in_handle_uint := midi_in_handle_uint0 * 16777216 + midi_in_handle_uint1 * 65536 + midi_in_handle_uint2 * 256 + midi_in_handle_uint3
That gets me that 541132032 value.
So then I pass it to the midiInClose function:
Code:
result := DllCall("winmm.dll\midiInClose"
, UInt, midi_in_handle_uint)
But my result = 5, rather than 0.
"5" means MMSYSERR_INVALHANDLE. It thinks that I passed it a bad value.
Can you point out where I am failing, please?
...
And I solved my own problem, it seems.
I figured, what if the handle value is big, rather than little endian?
So, I just reversed my byte order above, and it worked!
correct way to translate my h_midiin handle into uint should be:
Code:
midi_in_handle_uint3 := *(&h_midiin + 0)
midi_in_handle_uint2 := *(&h_midiin + 1)
midi_in_handle_uint1 := *(&h_midiin + 2)
midi_in_handle_uint0 := *(&h_midiin + 3)
midi_in_handle_uint := midi_in_handle_uint0 * 16777216 + midi_in_handle_uint1 * 65536 + midi_in_handle_uint2 * 256 + midi_in_handle_uint3
This is great news, because it means I can now proceed with looping inside my midi in AHK function and "listen" for input, and respond accordingly. (Of course, it means that I can't use anyother AHK function in my script....)
We'll see what happens.