Passing VB Callback function to C dll - noob is stuck.

Posted by WaveyDavey on Stack Overflow See other posts from Stack Overflow or by WaveyDavey
Published on 2009-01-26T21:45:01Z Indexed on 2010/03/25 5:03 UTC
Read the original article Hit count: 404

Filed under:
|
|
|

Callbacks in VB (from C dll).

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.

Details:

The function in the dll that I'm passing the address of a callback to is defined in C as :

PaError Pa_OpenStream( PaStream** stream,
                       const PaStreamParameters *inputParameters,
                       const PaStreamParameters *outputParameters,
                       double sampleRate,
                       unsigned long framesPerBuffer,
                       PaStreamFlags streamFlags,
                       PaStreamCallback *streamCallback,
                       void *userData );

where the function is parameter 7, *streamCallback. The type PaStreamCallback is defines thusly:

typedef int PaStreamCallback(
    const void *input, void *output,
    unsigned long frameCount,
    const PaStreamCallbackTimeInfo* timeInfo,
    PaStreamCallbackFlags statusFlags,
    void *userData );

In my vb project I have:

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

(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.)

In module1.vb I have defined the callback function:

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

The external function in the dll is called with

err = Pa_OpenStream(    ptr, _
    		nulthing, _
    		outputParameters, _
    		SAMPLE_RATE, _
    		FRAMES_PER_BUFFER, _
    		clipoff, _
    		AddressOf MyCallback, _
    		dataptr)

This is broken in the declaration of the external function - it doesn't like the type IntPtr as a function pointer for AddressOf.

Can anyone show me how to implement passing this callback function please ?

Many thanks David

© Stack Overflow or respective owner

Related posts about vb

Related posts about callbacks