title: Passing VB Callback function to C dll - noob is stuck
body: <p>Callbacks in VB (from C dll).</p>
  
  <p>I need to pass a vb function as a callback to a c function in a dll. I know I need to use
  addressof for the function but I'm getting more and more confused as to how to do it.</p>
  
  <p>Details: </p>
  
  <p>The function in the dll that I'm passing the address of a callback to is defined in C as : </p>
  
  <pre><code>PaError Pa_OpenStream( PaStream** stream,
                         const PaStreamParameters *inputParameters,
                         const PaStreamParameters *outputParameters,
                         double sampleRate,
                         unsigned long framesPerBuffer,
                         PaStreamFlags streamFlags,
                         PaStreamCallback *streamCallback,
                         void *userData );
  </code></pre>
  
  <p>where the function is parameter 7, *streamCallback. The type PaStreamCallback is defines thusly: </p>
  
  <pre><code>typedef int PaStreamCallback(
      const void *input, void *output,
      unsigned long frameCount,
      const PaStreamCallbackTimeInfo* timeInfo,
      PaStreamCallbackFlags statusFlags,
      void *userData );
  </code></pre>
  
  <p>In my vb project I have: </p>
  
  <pre><code>Private Declare Function Pa_OpenStream Lib "portaudio_x86.dll" _
          (     ByVal stream As IntPtr _
              , ByVal inputParameters As IntPtr _
              , ByVal outputParameters As PaStreamParameters _
              , ByVal samprate As Double _
              , ByVal fpb As Double _
              , ByVal paClipoff As Long _
              , ByVal patestCallBack As IntPtr _
              , ByVal data As IntPtr) As Integer
  </code></pre>
  
  <p>(don't worry if I've mistyped some of the other parameters, I'll get to them later! Let's 
  concentrate on the callback for now.)</p>
  
  <p>In module1.vb I have defined the callback function: </p>
  
  <p>Function MyCallback(    ByVal inp As Byte, _
                  ByVal outp As Byte, _
                  ByVal framecount As Long, _
                  ByVal pastreamcallbacktimeinfo As Byte, _
                  ByVal pastreamcallbackflags As Byte, _
                  ByVal userdata As Byte) 
          As Integer
      ' do clever things here
  End Function</p>
  
  <p>The external function in the dll is called with </p>
  
  <pre><code>err = Pa_OpenStream(    ptr, _
              nulthing, _
              outputParameters, _
              SAMPLE_RATE, _
              FRAMES_PER_BUFFER, _
              clipoff, _
              AddressOf MyCallback, _
              dataptr)
  </code></pre>
  
  <p>This is broken in the declaration of the external function - it doesn't like the type IntPtr 
  as a function pointer for AddressOf.</p>
  
  <p>Can anyone show me how to implement passing this callback function please ? </p>
  
  <p>Many thanks
  David</p>