金山快译是一款支持中英日互译的离线翻译软件,尝试用c#调用。

参考vnr的调用方法:

https://github.com/Artikash/VNR-Core/blob/6ed038bda9dcd35696040bd45d31afa6a30e8978/py/libs/kingsoft/gts.py#L283

https://github.com/Artikash/VNR-Core/blob/6ed038bda9dcd35696040bd45d31afa6a30e8978/cpp/tests/fastaittest/main.cc

调用GTS目录下的相应dll,以日中翻译为例:

        const string FASTAIT_DLL =@"C:\Kingsoft\FASTAIT_PERSONAL\GTS\JapaneseSChinese\JPNSCHSDK.dll";
        const string PATH = @"C:\Kingsoft\FASTAIT_PERSONAL\GTS\JapaneseSChinese\";
        const string DEFAULT_DIC = "DCT";

        int buffersize = 0x4f4;
        int key = 0x4f4;
        
        [DllImport(FASTAIT_DLL)]
        internal static extern int StartSession(
            [MarshalAs(UnmanagedType.LPWStr)]  string dicpath,
            IntPtr bufferStart,
            IntPtr bufferStop,
            [MarshalAs(UnmanagedType.LPWStr)] string app
        );

        [DllImport(FASTAIT_DLL)]
        internal static extern int EndSession();

        [DllImport(FASTAIT_DLL)]
        internal static extern int OpenEngine(int key);

        [DllImport(FASTAIT_DLL)]
        internal static extern int CloseEngineM(int key);

        [DllImport(FASTAIT_DLL)]
        internal static extern int SimpleTransSentM(        
            int key,
            [MarshalAs(UnmanagedType.LPWStr)] string fr,
            [MarshalAs(UnmanagedType.LPWStr)] StringBuilder t, 
            int unknown, 
            int unknown2
            );

        [DllImport(FASTAIT_DLL)]
        internal static extern int SetBasicDictPathW(         
            int key,
            [MarshalAs(UnmanagedType.LPWStr)]  string fr
            );

        [DllImport(FASTAIT_DLL)]
        internal static extern int SetProfDictPathW(         
            int key,
            [MarshalAs(UnmanagedType.LPWStr)] string path,
            int priority
            );

一个使用例子:

static void Main(string[] args)
        {
            IntPtr buffer = Marshal.AllocHGlobal(buffersize);
            try
            {
                string dicPath = PATH + DEFAULT_DIC;
                StartSession(dicPath, buffer, buffer + buffersize, "DCT");//return 0 成功
                OpenEngine(key); //return 0 成功
                SetBasicDictPathW(key, dicPath);//return 0 成功
                StringBuilder to = new StringBuilder(0x400);
                SimpleTransSentM(key, "憎しみは憎しみしか生まない。", to, 0x28, 0x4);//return 0 成功
                Console.WriteLine(to.ToString());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                CloseEngineM(key);
                EndSession();
                Marshal.FreeHGlobal(buffer);
            }
        }

结果如下:


一沙一世界,一花一天堂。君掌盛无边,刹那成永恒。