private bool PrepareHeader(bool input, IntPtr handle, IntPtr headerPtr) { MIDIError midierror = MIDIError.MIDI_OK; if (headerPtr != IntPtr.Zero) { if (input) { midierror = Midi.MIDI_InPrepareHeader(handle, headerPtr); } else { midierror = Midi.MIDI_OutPrepareHeader(handle, headerPtr); } } if (midierror != MIDIError.MIDI_OK) { MIDIHDR midihdr = null; try { midihdr = (MIDIHDR)Marshal.PtrToStructure(headerPtr, typeof(MIDIHDR)); } catch { } if (midihdr != null) { try { if (midihdr.data != IntPtr.Zero) { Marshal.FreeHGlobal(midihdr.data); } } catch { } midihdr.data = IntPtr.Zero; try { if (headerPtr != IntPtr.Zero) { Marshal.FreeHGlobal(headerPtr); } } catch { } } } return(midierror == MIDIError.MIDI_OK); }