0

这是库Secret Rabbit Code (aka libsamplerate)的 .NET 包装器,当在CallbackRead中调用src_callback_read时:AccessViolationException - Attempted to read or write protected memory

从 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;
}
4

1 回答 1

0

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

[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关键字。

于 2012-04-16T17:28:42.830 回答