A midi controller in a desktop program freezes the graphic controls, though they still function

Keitel 61 Reputation points
2020-08-28T10:56:33.447+00:00

I have made a music program in Visual Studio for Windows with C and Fmod that contains a graphical keyboard, a selection of sound files etc., and which also contains a midi controller. I use the midi controller by connecting a midi keyboard to the computer. All the functionality of the midi controller is placed in a callback function, which is called when I press a specific button that is supposed to turn the midi functionality on and off. I am, however, not sure if this is necessary. Maybe I rather should turn it on automatically when starting the program, and turn it off only when quitting, though it’s maybe not so important. The button contains among others the following code:

if (!Midion) {
Midion = 1;
    nMidiDeviceNum = midiInGetNumDevs();
    if (nMidiDeviceNum == 0) return -1;
        rv = midiInOpen(&hMidiDevice, nMidiPort, (DWORD)(void*)MidiInProc, 0, CALLBACK_FUNCTION);

    if (rv != MMSYSERR_NOERROR) return -1;

    midiInStart(hMidiDevice);

if (Midion) {
Midion = 0;
    rv = midiInStop(hMidiDevice);
    rv = midiInClose(hMidiDevice);
    hMidiDevice = NULL;
} 

The midi program works fine, and for a while I can at any time stop playing the midi keyboard and set graphic controls. But after about 5 minutes, nothing happen to the graphic controls when clicking them or dragging them, though they still change their function in a correct way. It is however not satisfying when the changes is not shown on the screen.

I have also found that the “rv” variable for midiInStop(hMidiDevice) and midiInClose(hMidiDevice) gives the value 5, which means MMSYSERR_INVALHANDLE, and I don't understand why. This also means that I can not turn off the midi functionality, as I am supposed to. The main problem is however the freezing of the graphic controls.

Thanks in advance.

Sincerely

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,498 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,612 questions
{count} votes

Accepted answer
  1. Rita Han - MSFT 2,161 Reputation points
    2020-09-09T01:23:33.957+00:00

    Hello @Keitel ,

    I have also found that the “rv” variable for midiInStop(hMidiDevice) and midiInClose(hMidiDevice) gives the value 5, which means MMSYSERR_INVALHANDLE, and I don't understand why.

    A handle is simply a number that Windows uses for internal reference to an object. In general, a handle becomes invalid after it has been closed.

    For suggestions to troubleshoot error "handle is invalid":

    • Store the handle value when you get it at the first time. Compare it with the one causes this error. In this question, hMidiDevice is released because out of scope. It is never the initial one you got it.
    • When you found the handle value is incorrect, check if there is any other place close the handle, or it has been changed unexpectedly.

    The main problem is however the freezing of the graphic controls.

    At first, you can narrow down this issue by excluding unrelated part, this often helps.

    Thank you!


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Viorel 114.1K Reputation points
    2020-08-29T10:48:30.58+00:00

    If possible, test it without callback, i.e. ‘midiInOpen(&hMidiDevice, nMidiPort, NULL, 0, CALLBACK_NULL)’.

    Then try ‘midiInOpen(&hMidiDevice, nMidiPort, ***(DWORD_PTR)****&MidiInProc, 0, CALLBACK_FUNCTION)’*. Make sure that the definition (parameters) of MidiInProc is good.