0

我正在开发 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, 0, charArray, charArray.Length);
                    pfEaten = res;
                    return res;
                }

            }
            pfEaten = false;
            return S_OK();
        }
    // Other function
    }

错误发生在“res = context.GetStart(e, out var range);”的行中。

TF_E_NOLOCK:ec中的cookie无效。

如何解决这个问题?

4

1 回答 1

0

EditSession Cookie 仅在函数 DoEditSession 中可用。超出此功能范围的 cookie 已过期。

于 2020-08-19T08:36:14.650 回答