You need to wrap all the necessary functions listed at http://msdn.microsoft.com/en-us/library/dd757277(VS.85).aspx
Not too complicated if you just use short messages, things get a little more complicated if you want to do SysEx or streaming output.
All you need for a basic input port is to get valid input identifiers (InputCount -1), pass a valid Open identifier, start an input, receive messages through a delegate ... Stop the input, and then finally close it. This is a very approximate example of how this can be achieved - you will need to do some checking and be careful that the port is not collected before it is closed, and a closed callback occurs or you lock your system!
Good luck
namespace MIDI
{
public class InputPort
{
private NativeMethods.MidiInProc midiInProc;
private IntPtr handle;
public InputPort ()
{
midiInProc = new NativeMethods.MidiInProc (MidiProc);
handle = IntPtr.Zero;
}
public static int InputCount
{
get {return NativeMethods.midiInGetNumDevs (); }
}
public bool Close ()
{
bool result = NativeMethods.midiInClose (handle)
== NativeMethods.MMSYSERR_NOERROR;
handle = IntPtr.Zero;
return result;
}
public bool Open (int id)
{
return NativeMethods.midiInOpen (
out handle,
id
midiInProc,
IntPtr.Zero,
NativeMethods.CALLBACK_FUNCTION)
== NativeMethods.MMSYSERR_NOERROR;
}
public bool Start ()
{
return NativeMethods.midiInStart (handle)
== NativeMethods.MMSYSERR_NOERROR;
}
public bool Stop ()
{
return NativeMethods.midiInStop (handle)
== NativeMethods.MMSYSERR_NOERROR;
}
private void MidiProc (IntPtr hMidiIn,
int wMsg,
IntPtr dwInstance,
int dwParam1,
int dwParam2)
{
// Receive messages here
}
}
internal static class NativeMethods
{
internal const int MMSYSERR_NOERROR = 0;
internal const int CALLBACK_FUNCTION = 0x00030000;
internal delegate void MidiInProc (
IntPtr hMidiIn,
int wMsg,
IntPtr dwInstance,
int dwParam1,
int dwParam2);
[DllImport ("winmm.dll")]
internal static extern int midiInGetNumDevs ();
[DllImport ("winmm.dll")]
internal static extern int midiInClose (
IntPtr hMidiIn);
[DllImport ("winmm.dll")]
internal static extern int midiInOpen (
out IntPtr lphMidiIn,
int uDeviceID,
MidiInProc dwCallback,
IntPtr dwCallbackInstance,
int dwFlags);
[DllImport ("winmm.dll")]
internal static extern int midiInStart (
IntPtr hMidiIn);
[DllImport ("winmm.dll")]
internal static extern int midiInStop (
IntPtr hMidiIn);
}
} Daveym69
source share