MIDI: detecting a device unplugging

Hello,

I'm using the standard midi routines midiInOpen and midiOutOpen to get handles to external usb midi devices.

But, how do I tell if the device has been unplugged after it is open?

I'm not getting any callback messages to the callback routine and don't see any messages that would apply in that situation anyway.

Any ideas? Should I be using another api?

--Mark
win32? Not that I am aware of any. Maybe you should just tell your consumers they shouldn't unplug their MIDI devices in the middle of a session?
Thanks. i sure can't find a way. This is easy on a Mac. Our midi device is our own product that people have several of and are often unplugging/replugging depending on the current configuration. Making them restart the software is annoying.
Hm... You could keep polling midiInGetDevCaps function with midiInGetNumDevs to check whether the product you are using is still present (by comparing the MIDIINCAPS structures the one you are currently using) - or rather, do this only if the number of input devices changes - , and do the same for the output devices. Doesn't seem like a very elegant approach, but you could try this until you can think of something better (I gotta admit I am not very versatile with the Win API).
Last edited on
Interesting idea. I already get the the MIDIINCAPS for each device. This would catch most of the changes. However, some situations like one of our devices sliding into the slot of an unplugged would look like the wrong device was unplugged. I've been looking for some kind of unique id, but can't find one.
I don't really get your example... I know this would cause problems if you have 2 times exactly the same product plugged (which I admit it really shouldn't do), but other than that it should work, right?
Lets say I have 2 of our devices plugged in and they appear in slots 2,3. If I unplug the device in slot 2, then enumerating again, I will see that our device is in slot 2, but now 3 is something else. I would conclude that device 3 is unplugged. I suppose I could come up with a system of sending a sysex msg to our devices with a unique id, then query for the id.
I should add that the MIDIINCAPS structure looks identical for our devices. there is not some kind of unique id generated from the usb port it was plugged into. So I can only tell if it's different from other midi devices.
Well, but if it's like that:
1: Device Type 1
2: Device Type 2
3: Device Type 1
4: Device Type 2

removed 2:
1: Device Type 1
2: Device Type 1
3: Device Type 2

Now I am not really sure atm whether windows guarantees to keep the order if the number of devices changes, but if it does you could get away with comparing the MIDIINCAPS as long as there are no adjacent identical devices. Of course that doesn't really qualify as a final solution... isn't there some way to get the physical slot a device is connected to? That should be enough for qualifying.
I count on the order being the same, but still would be problems in adjacent slot situations. I do the firmware for the device, and I think I'll give each a unique number at firmware programming time then will query for the number.
hum, I think i see your point. If the order was the same, then one could differentiate.
A little more thinking and I'm back to the adjacent slot problem. Better go with a uniqueID.
Topic archived. No new replies allowed.