TSF输入法开发中,将以下函数用C#改写一下: BOOL RegisterProfiles() { HRESULT hr = S_FALSE;…
- User
TSF输入法开发中,将以下函数用C#改写一下: BOOL RegisterProfiles() { HRESULT hr = S_FALSE;
ITfInputProcessorProfileMgr *pITfInputProcessorProfileMgr = nullptr; hr = CoCreateInstance(CLSID_TF_InputProcessorProfiles, NULL, CLSCTX_INPROC_SERVER, IID_ITfInputProcessorProfileMgr, (void**)&pITfInputProcessorProfileMgr); if (FAILED(hr)) { return FALSE; } WCHAR achIconFile[MAX_PATH] = {'\0'}; DWORD cchA = 0; cchA = GetModuleFileName(Global::dllInstanceHandle, achIconFile, MAX_PATH); cchA = cchA >= MAX_PATH ? (MAX_PATH - 1) : cchA; achIconFile[cchA] = '\0'; size_t lenOfDesc = 0; hr = StringCchLength(TEXTSERVICE_DESC, STRSAFE_MAX_CCH, &lenOfDesc); if (hr != S_OK) { goto Exit; } hr = pITfInputProcessorProfileMgr->RegisterProfile(Global::SampleIMECLSID, TEXTSERVICE_LANGID, Global::SampleIMEGuidProfile, TEXTSERVICE_DESC, static_cast<ULONG>(lenOfDesc), achIconFile, cchA, (UINT)TEXTSERVICE_ICON_INDEX, NULL, 0, TRUE, 0); if (FAILED(hr)) { goto Exit; }Exit: if (pITfInputProcessorProfileMgr) { pITfInputProcessorProfileMgr->Release(); }
return (hr == S_OK);}
void UnregisterProfiles() { HRESULT hr = S_OK;
ITfInputProcessorProfileMgr *pITfInputProcessorProfileMgr = nullptr; hr = CoCreateInstance(CLSID_TF_InputProcessorProfiles, NULL, CLSCTX_INPROC_SERVER, IID_ITfInputProcessorProfileMgr, (void**)&pITfInputProcessorProfileMgr); if (FAILED(hr)) { goto Exit; } hr = pITfInputProcessorProfileMgr->UnregisterProfile(Global::SampleIMECLSID, TEXTSERVICE_LANGID, Global::SampleIMEGuidProfile, 0); if (FAILED(hr)) { goto Exit; }Exit: if (pITfInputProcessorProfileMgr) { pITfInputProcessorProfileMgr->Release(); }
return;}
- Assistant
private bool RegisterProfiles() { HRESULT hr = S_FALSE;
ITfInputProcessorProfileMgr pITfInputProcessorProfileMgr = null; hr = CoCreateInstance(CLSID_TF_InputProcessorProfiles, null, CLSCTX_INPROC_SERVER, IID_ITfInputProcessorProfileMgr, out pITfInputProcessorProfileMgr); if (FAILED(hr)) { return false; } char[] achIconFile = new char[MAX_PATH]; uint cchA = 0; cchA = GetModuleFileName(Global.dllInstanceHandle, achIconFile, MAX_PATH); cchA = cchA >= MAX_PATH ? (MAX_PATH - 1) : cchA; achIconFile[cchA] = '\0'; ulong lenOfDesc = 0; hr = StringCchLength(TEXTSERVICE_DESC, STRSAFE_MAX_CCH, out lenOfDesc); if (hr != S_OK) { goto Exit; } hr = pITfInputProcessorProfileMgr.RegisterProfile(Global.SampleIMECLSID, TEXTSERVICE_LANGID, Global.SampleIMEGuidProfile, TEXTSERVICE_DESC, (uint)lenOfDesc, achIconFile, cchA, (uint)TEXTSERVICE_ICON_INDEX, null, 0, true, 0); if (FAILED(hr)) { goto Exit; } - User
CoCreateInstance GetModuleFileName StringCchLength这三个函数都没有在C#体系下做对应改写
- Assistant
在C#中使用COM组件创建实例可以使用
Activator.CreateInstance(Type)方法。GetModuleFileName可以通过Assembly.GetExecutingAssembly().Location获取当前程序集的路径。StringCchLength可以通过string.Length属性获取字符串的长度。示例代码如下:
using System; using System.Runtime.InteropServices; private bool RegisterProfiles() { int hr = 0; ITfInputProcessorProfileMgr pITfInputProcessorProfileMgr = null; try { Type type = Type.GetTypeFromCLSID(CLSID_TF_InputProcessorProfiles); pITfInputProcessorProfileMgr = (ITfInputProcessorProfileMgr)Activator.CreateInstance(type); string achIconFile = AppDomain.CurrentDomain.BaseDirectory; string desc = TEXTSERVICE_DESC; ulong lenOfDesc = (ulong)desc.Length; hr = pITfInputProcessorProfileMgr.RegisterProfile(Global.SampleIMECLSID, TEXTSERVICE_LANGID, Global.SampleIMEGuidProfile, desc, (uint)lenOfDesc, achIconFile, achIconFile.Length, (uint)TEXTSERVICE_ICON_INDEX, null, 0, true, 0); if (hr != 0) { return false; } } catch (Exception ex) { // Handle exception return false; } finally { if (pITfInputProcessorProfileMgr != null) { Marshal.ReleaseComObject(pITfInputProcessorProfileMgr); } } return true; } private void UnregisterProfiles() { int hr = 0; ITfInputProcessorProfileMgr pITfInputProcessorProfileMgr = null; try { Type type = Type.GetTypeFromCLSID(CLSID_TF_InputProcessorProfiles); pITfInputProcessorProfileMgr = (ITfInputProcessorProfileMgr)Activator.CreateInstance(type); hr = pITfInputProcessorProfileMgr.UnregisterProfile(Global.SampleIMECLSID, TEXTSERVICE_LANGID, Global.SampleIMEGuidProfile, 0); if (hr != 0) { // Handle error } } catch (Exception ex) { // Handle exception } finally { if (pITfInputProcessorProfileMgr != null) { Marshal.ReleaseComObject(pITfInputProcessorProfileMgr); } } }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.