如何使用来自 C# 的 C 回调

本文关键字:回调 何使用 | 更新日期: 2023-09-27 18:31:24

这是库秘密兔子代码(又名libsamplerate)的.NET包装器,在回调中调用src_callback_read时读取: 访问违规异常 - 试图读取或写入受保护的内存

从 C 标头:

/* Opaque data type SRC_STATE. */
typedef struct SRC_STATE_tag SRC_STATE ;
typedef long (*src_callback_t) (void *cb_data, float **data) ;
SRC_STATE* src_callback_new (src_callback_t func, int converter_type, int channels,                 int *error, void* cb_data) ;
long src_callback_read (SRC_STATE *state, double src_ratio, long frames, float *

然后我创建了以下内容:

调用 C 库的 C++/CLI 类库:

public delegate long SRCCallback ( void *cb_data, float **data ) ;
static  IntPtr CallbackNew ( SRCCallback ^func, RabbitConverter converter_type, int channels, int *error, void* cb_data )
        {
            src_callback_t* cb = ( src_callback_t* ) &func;
            SRC_STATE* state =  src_callback_new ( *cb, ( int ) converter_type, channels, error, cb_data );
            IntPtr ptr = IntPtr ( state );
            return ptr;
        }
static long CallbackRead ( IntPtr  src_state, double src_ratio, long frames , float * data )
        {
            void* toPointer = src_state.ToPointer();
            SRC_STATE* state = ( SRC_STATE* ) toPointer;
            long l = src_callback_read ( state, src_ratio, frames, data );
            return l;
        }

使用 C++/CLI 库的 C# 应用程序:

private static readonly unsafe SRCCallback _callback = target;
private static unsafe void Callback()
{
    var input = GetSine(1000.0d, 44100, 1);
    fixed (float* f = input)
    {
        RabbitError error = RabbitError.SRC_ERR_NO_ERROR;
        var @void = Rabbit.CallbackNew(_callback, RabbitConverter.SRC_SINC_BEST_QUALITY, 1, (int*) (&error),
                                        (void*) IntPtr.Zero);
        var callbackRead = Rabbit.CallbackRead(@void  , 1d, input.Length/2, f);
        if (callbackRead != input.Length)
        {
            // throw new InvalidOperationException();
        }
    }
}
private static unsafe int target(void* cb_data, float** data)
{
    return 9;
}

如何使用来自 C# 的 C 回调

我已经使用以下方法修复了它:

[System::Runtime::InteropServices::UnmanagedFunctionPointerAttribute ( CallingConvention::Cdecl )]
public delegate long SRCCallback ( void *cb_data, float **data ) ;
static  IntPtr CallbackNew ( SRCCallback ^ % func, RabbitConverter converter_type, int channels, int *error, void* cb_data ) { ... }

简而言之:现在它使用 C 调用约定和 C# ref 关键字。