在 C# 中调用 Pocketsphinx AccesViolationException

2024-04-02

我正在尝试做口袋狮身人面像tutorial http://cmusphinx.sourceforge.net/wiki/tutorialpocketsphinx在 C# 中使用 pinvoke 但当我尝试使用 ps_decode_raw() 进行解码时出现 AccessViolationException。

        IntPtr ps = PocketSphinx.ps_init(config);
        IntPtr fh = Win32Util.fopen(@"goforward.raw", "rb");
        int rv = PocketSphinx.ps_decode_raw(ps, fh, "goforward", -1);

函数包装如下

    //ps_decoder_t* ps_init(cmd_ln_t* config)
    [DllImport("pocketsphinx.dll",
        SetLastError = true,
        CallingConvention = CallingConvention.Cdecl)]
    public extern static IntPtr ps_init(
        IntPtr config);

    //int ps_decode_raw(ps_decoder_t *ps, FILE *rawfh, char const *uttid, long maxsamps);
    [DllImport("pocketsphinx.dll",
        SetLastError = true,
        CallingConvention = CallingConvention.Cdecl)]
    public extern static int ps_decode_raw(
        IntPtr ps,
        IntPtr rawfh,
        [MarshalAs(UnmanagedType.LPStr)] string uttid,
        int maxsamps);

    [DllImport("msvcrt.dll",
        SetLastError = true,
        CallingConvention = CallingConvention.Cdecl)]
    public extern static IntPtr fopen(
        [MarshalAs(UnmanagedType.LPStr)] string _Filename,
        [MarshalAs(UnmanagedType.LPStr)] string _Mode);

我也封装了 C 的 fopen,因为这是我能想到的实现本教程的最快方法。

我尝试在 ps 上调用 cmd_ln_retain 以确保 ps 不会导致问题。 (事实并非如此)。我还删除了上面的调试代码。

我很确定 fopen 出了问题,但我不确定是什么。

有人想要口袋狮身人面像日志。https://justpaste.it/h52t https://justpaste.it/h52t


您不会在任何地方检查错误。而且设置错误SetLastError对于这些函数为 true。他们不会打电话SetLastError.

不过,您的大问题是该库使用 C 运行时的特定实例,具体取决于您构建它的方式。和你的fopen导入来自 C 运行时的不同实例。

您需要向库添加一些代码,以公开要创建和销毁的函数FILE*对象。通过这样做你会得到一个FILE*由正确的运行时制作。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 C# 中调用 Pocketsphinx AccesViolationException 的相关文章

随机推荐