2

VB 中的回调(来自 C dll)。

我需要将 vb 函数作为回调传递给 dll 中的 ac 函数。我知道我需要为该功能使用 addressof ,但我对如何做到这一点越来越感到困惑。

细节:

我将回调地址传递给的 dll 中的函数在 C 中定义为:

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

其中函数是参数 7,*streamCallback。PaStreamCallback 类型是这样定义的:

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

在我的 vb 项目中,我有:

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

(如果我输错了其他一些参数,请不要担心,我稍后会找到它们!现在让我们专注于回调。)

在 module1.vb 我定义了回调函数:

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 ' 在这里做聪明的事情 End Function

dll中的外部函数被调用

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

这在外部函数的声明中被破坏了——它不喜欢 IntPtr 类型作为 AddressOf 的函数指针。

谁能告诉我如何实现传递这个回调函数?

非常感谢大卫

4

1 回答 1

1

这篇 MSDN 文章将有所帮助:如何从 C DLL 回调 Visual Basic 函数

于 2009-01-26T21:53:41.997 回答