问题描述
我正在开发TSF文本服务。它取决于TSF-TypeLib。 这是我的项目代码的一部分:
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class EditSession:BaSEObject,ITfEditSession
{
public uint EditCookie;
public HRESULT DoEditSession(uint ec)
{
EditCookie = ec;
return S_OK();
}
}
public class KeyEventSink : BaSEObject,ITfKeyEventSink
{
private uint _clientId;
private ITfThreadMgr _threadMgr;
public KeyEventSink(uint clientId,ITfThreadMgr threadMgr)
{
_clientId = clientId;
_threadMgr = threadMgr;
}
[DllImport("user32.dll")]
static extern uint MapVirtualKey(uint uCode,uint uMapType);
public HRESULT OnTestKeyDown(ITfContext pic,UIntPtr wParam,IntPtr lParam,out bool pfEaten)
{
var res = _threadMgr.GetFocus(out var documentMgr);
res = documentMgr.GetTop(out var context);
char nonVirtualKey = (char)MapVirtualKey(wParam.ToUInt32(),2);
Debug.WriteLine(nonVirtualKey);
if (nonVirtualKey == 'F')
{
var editSession = new EditSession();
res = context.RequestEditSession(_clientId,editSession,TF_ES.TF_ES_READWRITE,out var phrSession);
var phrSeesionHRESULT = new HRESULT { Code = phrSession };
if (phrSeesionHRESULT)
{
var e = editSession.EditCookie;
res = context.GetStart(e,out var range); //A error occured
Debug.WriteLine(res);
Debug.WriteLine((ManagerReturnValues)res.Code);
var chararray = "་".tochararray();
res = range.SetText(e,chararray,chararray.Length);
pfEaten = res;
return res;
}
}
pfEaten = false;
return S_OK();
}
// Other function
}
该错误发生在“ res = context.GetStart(e,超出var范围);”行中。
TF_E_NOLOCK: ec 中的cookie无效。
如何解决这个问题?
解决方法
EditSession Cookie仅在函数DoEditSession中可用。此功能cookie的范围已过期。