I'm developing a TSF Text Services. It depend on TSF-TypeLib. Here is a part of the code of my project:
[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, 0, charArray, charArray.Length);
pfEaten = res;
return res;
}
}
pfEaten = false;
return S_OK();
}
// Other function
}
The error occured in the line that "res = context.GetStart(e, out var range); ".
TF_E_NOLOCK:The cookie in ec is invalid.
How to solove this problem?
EditSession Cookie only available in the function DoEditSession. Out the scope of this function cookie is out of date.