If you want to interact with a MIDI device in real time, your best bet is to use an existing library. The task is more complex than it seems at first time involving timing and scheduling of events.
You can look into PortMidi (part of PortMedia project: http://portmedia.sourceforge.net/) or MidiShare (http://midishare.sourceforge.net/).
Of course you should learn about the MIDI protocol itself. There are many short descriptions on the network you can start from the MMA site (the organization that oversees the MIDI standard) http://www.midi.org/aboutmidi/tut_techomidi.php (or just google for "midi protocol").
Consider that those are very high level descriptions, you may want to buy a more detailed book like "Maximum MIDI - Music Applications in C++" ( http://www.amazon.com/Maximum-MIDI-Music-Applications-C/dp/1884777449 ). It's an old book that explains how to create MIDI applications using the standard Windows API but the concepts are all there.