using System; using System.Runtime.InteropServices; namespace LrcTool { public class HiraganaPhoneticHelper : IDisposable { private const string ProgId = "MSIME.Japan"; private readonly IFELanguage _instance; public HiraganaPhoneticHelper() { if (!(Activator.CreateInstance(Type.GetTypeFromProgID(ProgId)) is IFELanguage ifelang)) { throw new Exception($"Could not create object of {ProgId}, is STAThread attribute missing?"); } var hr = ifelang.Open(); if (hr != 0) throw Marshal.GetExceptionForHR(hr); hr = ifelang.GetPhonetic("東京は昨日雪が降りました。", 1, -1, out _); if (hr != 0) throw Marshal.GetExceptionForHR(hr); _instance = ifelang; } public string GetPhonetic(string input) { var hr = _instance.GetPhonetic(input, 1, -1, out var result); if (hr != 0) throw Marshal.GetExceptionForHR(hr); return result; } public void Dispose() => _instance?.Close(); // IFELanguage2 Interface ID //[Guid("21164102-C24A-11d1-851A-00C04FCC6B14")] [ComImport] [Guid("019F7152-E6DB-11d0-83C3-00C04FDDB82E")] [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] private interface IFELanguage { int Open(); int Close(); int GetJMorphResult(uint dwRequest, uint dwCMode, int cwchInput, [MarshalAs(UnmanagedType.LPWStr)] string pwchInput, IntPtr pfCInfo, out object ppResult); int GetConversionModeCaps(ref uint pdwCaps); int GetPhonetic([MarshalAs(UnmanagedType.BStr)] string @string, int start, int length, [MarshalAs(UnmanagedType.BStr)] out string result); int GetConversion([MarshalAs(UnmanagedType.BStr)] string @string, int start, int length, [MarshalAs(UnmanagedType.BStr)] out string result); } } }