繁体   English   中英

如何编组包含 function 指针的 ac/cpp 结构?

[英]How do I Marshal a c/cpp struct containing a function pointer?

早上好,我正在尝试为 SDL 的音频功能创建一个 C# 点网核心包装器。 特别是,我正在尝试为https://wiki.libsdl.org/SDL_LoadWAV创建一个包装器

现有代码是:

表示正在编组的非基本类型:

    public delegate void SDL_AudioCallback(IntPtr userdata, byte[] stream,out int len);
    [StructLayout(LayoutKind.Sequential)]
    public struct SDL_AudioSpec
    {
        public int freq;
        public ushort format;
        public byte channels;
        public byte silence;
        public ushort samples;
        public UInt32 size;
        public IntPtr callback;
        IntPtr userdata;
    }
    public class SDL_AudioSpecPtr
    {
        public readonly IntPtr NativePointer;

        public SDL_AudioSpecPtr(IntPtr pointer)
        {
            NativePointer = pointer;
        }

        public static implicit operator IntPtr(SDL_AudioSpecPtr Sdl2Window) => Sdl2Window.NativePointer;
        public static implicit operator SDL_AudioSpecPtr(IntPtr pointer) => new SDL_AudioSpecPtr(pointer);
    }

导入方法签名:

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate SDL_AudioSpec SDL_LoadWAV_t(string file, IntPtr spec, byte[] audio_buf, out UInt32 audio_len);
private static SDL_LoadWAV_t s_sdl_LoadWAV = LoadFunction<SDL_LoadWAV_t>("SDL_LoadWAV");
public static void SDL_LoadWAV(string file, IntPtr spec, byte[] audio_buf, out UInt32 audio_len) => s_sdl_LoadWAV(file,spec,audio_buf,out audio_len);

尝试调用 function 会产生 null 异常:

Sdl2Native.SDL_Init(SDLInitFlags.Audio);
SDL_AudioSpec spec = new SDL_AudioSpec();
byte[] bytes=new byte[20000];
uint len;
SDL_AudioSpec* specptr = &spec;
Sdl2Native.SDL_LoadWAV(@"G:\Assets\sfx_movement_jump2.wav", (IntPtr)specptr, bytes, out len);
            

您应该更多地依赖 CLR 的编组而不是自己做,这样更容易犯严重错误。

此外:

  • 字符串需要使用[MarshalAs(UnmanagedType.LPStr)]编组为 ANSI

  • 您不应该像那样将&指针转换为* ,该结构需要编组。

  • 需要保留给非托管代码的回调,以便 GC 不会将其扫除。

目前尚不清楚为什么您有一个LoadFunction设置,而不仅仅是使用标准 P/Invoke,但我已经使用它了。

所以代表应该是

private static SDL_AudioSpec SDL_LoadWAV_t(
    [MarshalAs(UnmanagedType.LPStr)] string file,
    in SDL_AudioSpec spec,
    ref byte[] audio_buf,
    out UInt32 audio_len);

然后SDL_AudioSpec结构应该声明回调

public SDL_AudioCallback callback;

回调也是错误的: len不是out参数,它是按值; 我认为stream应该是ref还是out还是in还不清楚out

public delegate void SDL_AudioCallback(IntPtr userdata, out byte[] stream, int len);

根据回调的使用时间,您需要坚持使用它

  • 如果它只是在单个调用中使用,并且未被非托管代码持有,则放置GC.KeepAlive(callback); 通话后
  • 如果非托管代码进一步持有它,那么您需要将其存储在某个字段中,您可以保证在最后一次回调之前它不会被 GC 删除

您的调用代码是:

var callbackDlgt = new SDL_AudioCallback(myCallbackFunction);
SDL_AudioSpec spec = new SDL_AudioSpec{
    callback = callbackDlgt,
// set other values here
};
byte[] bytes = new byte[20000];
Sdl2Native.SDL_LoadWAV(@"G:\Assets\sfx_movement_jump2.wav", in specptr, ref bytes, out uint len);
GC.KeepAlive(callbackDlgt);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM