public string GetYomi(string str) { string yomi = String.Empty; Guid pclsid; int res; // 文字列の CLSID から CLSID へのポインタを取得する res = CLSIDFromString("MSIME.Japan", out pclsid); if (res != S_OK) { this.Dispose(); return(yomi); } Guid riid = new Guid("019F7152-E6DB-11D0-83C3-00C04FDDB82E "); IntPtr ppv; res = CoCreateInstance(pclsid, IntPtr.Zero, CLSCTX_SERVER, riid, out ppv); if (res != S_OK) { this.Dispose(); return(yomi); } IFELanguage language = Marshal.GetTypedObjectForIUnknown(ppv, typeof(IFELanguage)) as IFELanguage; res = language.Open(); if (res != S_OK) { this.Dispose(); return(yomi); } IntPtr result; res = language.GetJMorphResult(FELANG_REQ_REV, FELANG_CMODE_PINYIN | FELANG_CMODE_NOINVISIBLECHAR, str.Length, str, IntPtr.Zero, out result); if (res != S_OK) { this.Dispose(); return(yomi); } yomi = Marshal.PtrToStringUni(Marshal.ReadIntPtr(result, 4), Marshal.ReadInt16(result, 8)); language.Close(); return(yomi); }
public string GetYomi(string str) { IntPtr result; int res; res = language.GetJMorphResult(FELANG_REQ_REV, FELANG_CMODE_PINYIN | FELANG_CMODE_NOINVISIBLECHAR, str.Length, str, IntPtr.Zero, out result); if (res != S_OK || result == IntPtr.Zero) { return ""; } return Marshal.PtrToStringUni(Marshal.ReadIntPtr(result, 4), Marshal.ReadInt16(result, 8)); }
public void ZhunYin(string input) { if (!coInitialized) { CoInitialize(IntPtr.Zero); coInitialized = true; } //textBox2.Clear(); Guid vGuidIme; int vError; vError = CLSIDFromString("MSIME.China", out vGuidIme); if (vError != S_OK) { throw new Exception(GetMessage(vError)); return; } Guid vGuidLanguage = new Guid("019F7152-E6DB-11D0-83C3-00C04FDDB82E"); IntPtr vPPV; vError = CoCreateInstance(vGuidIme, IntPtr.Zero, CLSCTX_SERVER, vGuidLanguage, out vPPV); if (vError != S_OK) { //MessageBox.Show(GetMessage(vError)); return; } IFELanguage vLanguage = Marshal.GetTypedObjectForIUnknown(vPPV, typeof(IFELanguage)) as IFELanguage; vError = vLanguage.Open(); if (vError != S_OK) { //MessageBox.Show(GetMessage(vError)); return; } IntPtr vMorrslt; string vInput = "中文";//textBox1.Text; vError = vLanguage.GetJMorphResult(FELANG_REQ_REV, FELANG_CMODE_PINYIN | FELANG_CMODE_NOINVISIBLECHAR, vInput.Length, vInput, IntPtr.Zero, out vMorrslt); if (vError != S_OK) { //MessageBox.Show(GetMessage(vError)); return; } string vPinYin = Marshal.PtrToStringUni(Marshal.ReadIntPtr(vMorrslt, 4), Marshal.ReadInt16(vMorrslt, 8)); //textBox2.AppendText("=" + vPinYin + "\r\n"); IntPtr vMonoRubyPos = Marshal.ReadIntPtr(vMorrslt, 28); IntPtr vReadIdxWDD = Marshal.ReadIntPtr(vMorrslt, 24); int iReadIdxWDD = 0; int iMonoRubyPos = Marshal.ReadInt16(vMonoRubyPos); vMonoRubyPos = (IntPtr)((int)vMonoRubyPos + 2); int i = 0; while (i < vInput.Length) { while (i < Marshal.ReadInt16(vReadIdxWDD)) { i++; if (i >= Marshal.ReadInt16(vReadIdxWDD)) { Console.WriteLine(Marshal.ReadInt16(vMonoRubyPos)); string s = vPinYin.Substring(iMonoRubyPos, Marshal.ReadInt16(vMonoRubyPos) - iMonoRubyPos); if (s != string.Empty) { s = vInput.Substring(iReadIdxWDD, i - iReadIdxWDD) + "(" + s + ")"; } else { s = vInput.Substring(iReadIdxWDD, i - iReadIdxWDD); } //textBox2.AppendText(s + "\r\n"); iReadIdxWDD = i; iMonoRubyPos = Marshal.ReadInt16(vMonoRubyPos); break; } vMonoRubyPos = (IntPtr)((int)vMonoRubyPos + 2); vReadIdxWDD = (IntPtr)((int)vReadIdxWDD + 2); } vMonoRubyPos = (IntPtr)((int)vMonoRubyPos + 2); vReadIdxWDD = (IntPtr)((int)vReadIdxWDD + 2); } CoTaskMemFree(vMorrslt); vLanguage.Close(); }