首页 新闻 会员 周边

请教.net core 调用 opencc C++ dll 的问题

0
悬赏园豆:30 [已关闭问题] 关闭于 2018-01-23 10:47
复制代码
    class Program
    {

        [DllImport("opencc", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr opencc_open(string configfile);

        [DllImport("opencc", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr opencc_convert_utf8(IntPtr opencc, string buf, int buflen);

        [DllImport("opencc", CallingConvention = CallingConvention.Cdecl)]
        public static extern void opencc_convert_utf8_free(IntPtr buffptr);

        private static IntPtr opencc_ptr = IntPtr.Zero;


        static void Main(string[] args)
        {
            opencc_ptr = opencc_open("t2s.json");
            byte[] u8array = Encoding.UTF8.GetBytes("輸入簡體字");
            string u8str = Encoding.UTF8.GetString(u8array);
            IntPtr result_ptr = opencc_convert_utf8(opencc_ptr, u8str, -1);
            Console.WriteLine(result_ptr);
        }
    }
复制代码

opencc 的 dll 可以在这里下载https://bintray.com/byvoid/opencc/OpenCC#files
我这里调用的时候 opencc_convert_utf8()总返回0,请问我这里调用有问题吗?是什么原因呢

wdwwtzy的主页 wdwwtzy | 初学一级 | 园豆:114
提问于:2018-01-16 10:41
< >
分享
所有回答(3)
0

opencc_open 方法的 C++ 头文件定义是什么?

dudu | 园豆:30979 (高人七级) | 2018-01-16 12:03

开源的,源码
https://github.com/BYVoid/OpenCC/blob/master/src/opencc.h
文档
http://byvoid.github.io/OpenCC/1.0.4/group__opencc__c__api.html

支持(0) 反对(0) wdwwtzy | 园豆:114 (初学一级) | 2018-01-16 14:09
支持(0) 反对(0) dudu | 园豆:30979 (高人七级) | 2018-01-17 14:08
0

解决了,这里的问题

public static extern IntPtr opencc_convert_utf8(IntPtr opencc, string buf, int buflen);

不应该用 string buf,用 IntPtr buf 就好了

wdwwtzy | 园豆:114 (初学一级) | 2018-01-23 10:46
0

你好 借楼问下 这个错误有人遇到过吗
我在netfx 4.6.2 webapi 环境下调用 opencc_convert_utf8
报错如下
An unhandled exception of type 'System.AccessViolationException' occurred in Unknown Module.
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

这是我的代码 在同版本(netfx 4.6.2)的控制台程序能运行 但是webapi不行

 public class OpenCCWrapper : IDisposable
    {
        [DllImport(@"./opencc/opencc", CallingConvention = CallingConvention.Cdecl)]
        private static extern IntPtr opencc_open(string configfile);

        [DllImport(@"./opencc/opencc", CallingConvention = CallingConvention.Cdecl)]
        private static extern IntPtr opencc_convert_utf8(IntPtr opencc, IntPtr input, int length);

        [DllImport(@"./opencc/opencc", CallingConvention = CallingConvention.Cdecl)]
        private static extern void opencc_convert_utf8_free(IntPtr input);

        [DllImport(@"./opencc/opencc", CallingConvention = CallingConvention.Cdecl)]
        private static extern void opencc_close(IntPtr opencc);

        private IntPtr _openCCInstance = IntPtr.Zero;

        public OpenCCWrapper(string configfilename = "s2hk")
        {
            _openCCInstance = opencc_open($@"./opencc/{configfilename}.json");
        }

        // https://stackoverflow.com/a/10773988/288936
        private IntPtr NativeUtf8FromString(string managedString)
        {
            int len = Encoding.UTF8.GetByteCount(managedString);
            byte[] buffer = new byte[len + 1];
            Encoding.UTF8.GetBytes(managedString, 0, managedString.Length, buffer, 0);
            IntPtr nativeUtf8 = Marshal.AllocHGlobal(buffer.Length);
            Marshal.Copy(buffer, 0, nativeUtf8, buffer.Length);
            return nativeUtf8;
        }

        private string StringFromNativeUtf8(IntPtr nativeUtf8)
        {
            int len = 0;
            while (Marshal.ReadByte(nativeUtf8, len) != 0) ++len;
            byte[] buffer = new byte[len];
            Marshal.Copy(nativeUtf8, buffer, 0, buffer.Length);
            return Encoding.UTF8.GetString(buffer);
        }

        public string Convert(string text)
        {
            IntPtr inStr = NativeUtf8FromString(text);
            IntPtr outStr = opencc_convert_utf8(_openCCInstance, inStr, -1);
            Marshal.FreeHGlobal(inStr);
            return StringFromNativeUtf8(outStr);
        }

        public void Dispose()
        {
            opencc_close(_openCCInstance);
        }
    }
封不觉是也 | 园豆:158 (初学一级) | 2020-08-05 17:27
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册