I think your interop with the MIDIHDR struct is wrong. You use a marshal-as attribute and a ref. I think this will not prevent the GC from relocating the (managed) MIDIHDR and when that happens the ref passed to (for instance) midiInAddBuffer is no longer valid. I would use an IntPtr to an unmanaged memory block that contains the MIDIHDR (copied there using Marshal.StructureToPtr).
↧