title: What is the best way to get a callback from an instance of a native C++ class to a C++/CX ref class?
body: <p>I am writing a C++ WinRT Component DLL for use in my .NET-based WinRT application. The DLL defines a SoundSample ref class that creates an XAudio voice by calling <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/microsoft.directx_sdk.ixaudio2.ixaudio2.createsourcevoice%28v=vs.85%29.aspx" rel="nofollow">IXAudio2::CreateSourceVoice</a>. CreateSourceVoice takes a "IXAudio2VoiceCallback *pCallback" parameter to enable callbacks on various audio events. Now I am trying to implement that callback based on <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ee415769%28v=vs.85%29.aspx" rel="nofollow">this article</a>. XAudio will supposedly just call back into methods of my SoundCallback class defined as:</p>
  
  <pre><code>#pragma once
  #include "xaudio2.h"
  #include "pch.h"
  
  class SoundCallback
      : public IXAudio2VoiceCallback
  {
  private:
      //SoundSample^ sample; //does not compile
  
  public:
      SoundCallback(void);
      ~SoundCallback(void);
  
      //Called when the voice has just finished playing a contiguous audio stream.
      void OnStreamEnd();
      void OnVoiceProcessingPassEnd();
      void OnVoiceProcessingPassStart(UINT32 SamplesRequired);
      void OnBufferEnd(void * pBufferContext);
      void OnBufferStart(void * pBufferContext);
      void OnLoopEnd(void * pBufferContext);
      void OnVoiceError(void * pBufferContext, HRESULT Error);
  };
  </code></pre>
  
  <p>Everything is fine until I try to figure out how to call back from an instance of my native callback class to the parent SoundSample object. I was thinking I could pass an instance of the SoundSample class to the SoundCallback object, but it seems like it does not allow me to declare a ref class field in the native class:</p>
  
  <pre><code>SoundCallback.h(9): error C2143: syntax error : missing ';' before '^'
  SoundCallback.h(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  SoundCallback.h(9): error C3699: '^' : cannot use this indirection on type 'int'
  </code></pre>
  
  <p>I looked back at implementing callbacks in native C++ and I could not find a reasonable solution so far. What is the best/easiest way to do this?</p>