Windows Microsoft TSF-像IUIAutomation::GetFocusedElement一样使用ITfThreadMgr::GetFocus

Windows Microsoft TSF-像IUIAutomation::GetFocusedElement一样使用ITfThreadMgr::GetFocus,windows,automation,text-services-framework,Windows,Automation,Text Services Framework,当我尝试在文本服务的上下文之外附加到Microsoft文本服务框架(TSF)时,我无法检索与活动应用程序的文本交互所需的信息 #include <SDKDDKVer.h> #include <stdio.h> #include <tchar.h> #include <msctf.h> #include <atlbase.h> #include <iostream> // Macro throws a descriptive

当我尝试在文本服务的上下文之外附加到Microsoft文本服务框架(TSF)时,我无法检索与活动应用程序的文本交互所需的信息

#include <SDKDDKVer.h>
#include <stdio.h>
#include <tchar.h>
#include <msctf.h>
#include <atlbase.h>
#include <iostream>

// Macro throws a descriptive WCHAR* exception if(FAILED(hr))
#define OK(hrExpr)  do{std::cout<<"IN: "<<#hrExpr<<std::endl; HRESULT returnCode = hrExpr; if( FAILED(returnCode) ){std::cout<<std::endl<<"ERROR! "<<#hrExpr<<" @"<<__FILE__<<":"<<__LINE__<<" returned "<<returnCode<<std::endl; throw L#hrExpr; }else{std::cout << "OK: " << #hrExpr << std::endl;}}while(0)

int _tmain(int argc, _TCHAR* argv[])
{
    ::CoInitialize(0);

    ITfThreadMgr* pThreadMgr = NULL;
    TfClientId clientId = NULL;
    ITfDocumentMgr* docMgr = NULL;
    ITfContext* pContext = NULL;

    try
    {
        std::cout << "Use the next 5 seconds to switch to a TSF-enabled application and make a text selection." << std::endl;
        Sleep(5000);

        OK( CoCreateInstance(  CLSID_TF_ThreadMgr, 
                        NULL, 
                        CLSCTX_INPROC_SERVER, 
                        IID_ITfThreadMgr, 
                        (void**)&pThreadMgr) );

        OK( pThreadMgr->Activate(&clientId) );

        OK( pThreadMgr->GetFocus(&docMgr) );

        if (docMgr == NULL)
        {
            throw L"NULL document manager";
        }

        OK( docMgr->GetTop(&pContext) );

        // TODO: use ITfContext to get selection or make document edits
    }
    catch(LPCSTR msg)
    {
        std::wcout << L"Exception: " << msg << std::endl;
    }
    catch(LPCWSTR msg)
    {
        std::wcout << L"Exception: " << msg << std::endl;
    }

    if (pContext)
        pContext->Release();    

    if (docMgr)
        docMgr->Release();

    ::CoUninitialize();
    return 0;
}
#包括
#包括
#包括
#包括
#include(:),但返回的文档管理器为空。根据API文档,这意味着目前没有重点关注的ITfDocumentMgr,但是:

如果我采取完全不同的方法:注册一个自定义文本服务并通过接收ITfThreadMgr引用,我就能够通过ITfThreadMgr::GetFocus检索WordPad的重点ITfDocumentMgr。但是由于各种原因,我不希望安装定制的inproc文本服务


是否有任何方法可以跨流程边界连接到TSF API,如使用AutomationElement.FocusedElement
IUIAAutomation::GetFocusedElement
AccessibleObjectFromWindow

文本服务框架不能跨流程工作。既没有代理也没有存根可用。对不起

顺便说一句,您的代码正试图为您的流程获取重点文档管理器。您可以使用
CLSCTX\u INPROC\u服务器创建
ITfThreadMgr
,它在进程中而不是在目标中启动线程管理器