title: How do I create an apple block from C# interop code?
body: <p>Apple recently deprecated the MIDIDestinationCreate method and replaced it with a MidiDestinationCreateWithProtocol (<a href="https://developer.apple.com/documentation/coremidi/1495347-mididestinationcreate?language=objc" rel="nofollow noreferrer">MIDIDestinationCreate</a>)</p>
  <p>The old method required to pass a simple callback method 'MIDIReadProc' which from C# could be done by creating the following delegate signature, then creating a delegate and assigning a method as the callback. We declare the delegate signature:</p>
  <pre><code>internal delegate void MIDIReadProc(MIDIPacketListPtr pktlist, IntPtr readProcRefCon, IntPtr srcConnRefCon);
  </code></pre>
  <p>Create a delegate to assign our callback method to:</p>
  <pre><code>private CoreMidiInterop.NativeMethods.MIDIReadProc m_readProcDelegate; 
  </code></pre>
  <p>Assign a method to the delegate we created:</p>
  <pre><code>m_readProcDelegate = CallMessageReceived;
  </code></pre>
  <p>Create a callback method, that should receive MIDI messages from macOS Core MIDI:</p>
  <pre><code>private void CallMessageReceived(MIDIPacketListPtr pktlist, IntPtr readProcRefCon, IntPtr srcConnRefCon)
  {
     ...
  }
  </code></pre>
  <p>Eventually pass this delegate to the apple Core MIDI method:</p>
  <pre><code>CoreMidiInterop.NativeMethods
                  .MIDIDestinationCreate(m_clientRef, CoreFoundationUtils.ToCFStringRef(name), m_readProcDelegate, CFStringRef.Zero, out MIDIEndpointRef destinationRef);
  </code></pre>
  <p>This all works as expected.</p>
  <p>That's now deprecated and no longer works on macOS Big Sur. The new method &quot;MidiDestinationCreateWithProtocol&quot; requires an apple block to use as the callback param (called MIDIReceiveBlock readBlock):</p>
  <pre><code>OSStatus MIDIDestinationCreateWithProtocol(MIDIClientRef client, CFStringRef name, MIDIProtocolID protocol, MIDIEndpointRef *outDest, MIDIReceiveBlock readBlock);
  </code></pre>
  <p>The documentation here: <a href="https://developer.apple.com/documentation/coremidi/midireceiveblock?language=objc" rel="nofollow noreferrer">MIDIReceiveBlock</a></p>
  <p>How is it possible to create an apple block with C# code? I've been searching for examples but cannot find any. I did start looking at the underlying implementation of apple blocks here:</p>
  <p><a href="https://clang.llvm.org/docs/Block-ABI-Apple.html#id2" rel="nofollow noreferrer">Block Implementation Specification</a></p>
  <p>It's no simple thing, so any help/example of how to do this in C# would be really helpful.</p>