原文 C# TSF 输入法的获取

起因:

「添雨跟打器」中存在一个问题。在 windows 8/10 里面,输入法就获取不到了。我一直没有去管这样的问题。但是也大致知道,可能是 TSF 架构的问题。

TSF:

Microsoft Windows 文本服务框架(TSF) 是一个包含在Windows XP 及其后继版本操作系统的系统服务。TSF为高级文本输入的通信以及自然语言技术提供了一个简单的可扩展的框架。

以上引自百度百科

MSDN:

于是第一时间去 MSDN 查看了一下关于 TSF 。全英文的文档看得云里雾里。但是搞清了一点情况。TSF 它在 Windows 里面所对应的 dll 文件——msctf.dll

使用 Visual Studio 自带的命令工具查看该 dll 的函数列表如下:

命令:

 
1
dumpbin -exports msctf.dll

结果:

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
Dump of file msctf.dll
 
File Type: DLL
 
  Section contains the following exports for MSCTF.dll
 
    00000000 characteristics
    5699C6A2 time date stamp Sat Jan 16 12:27:14 2016
        0.00 version
           1 ordinal base
          73 number of functions
          73 number of names
 
    ordinal hint RVA      name
 
          2    0 000183E0 CtfImeAssociateFocus
          3    1 000633E0 CtfImeConfigure
          4    2 00046A10 CtfImeConversionList
          5    3 000042E0 CtfImeCreateInputContext
          6    4 0000BF70 CtfImeCreateThreadMgr
          7    5 000633F0 CtfImeDestroy
          8    6 00038C40 CtfImeDestroyInputContext
          9    7 00038840 CtfImeDestroyThreadMgr
         10    8 00037D40 CtfImeDispatchDefImeMessage
         11    9 00046A10 CtfImeEnumRegisterWord
         12    A 00046A10 CtfImeEscape
         13    B 00063410 CtfImeEscapeEx
         14    C 00063490 CtfImeGetGuidAtom
         15    D 00046A10 CtfImeGetRegisterWordStyle
         16    E 00046A10 CtfImeInquire
         17    F 00006E60 CtfImeInquireExW
         18   10 000634F0 CtfImeIsGuidMapEnable
         19   11 00063540 CtfImeIsIME
         20   12 0001C540 CtfImeProcessCicHotkey
         21   13 00018840 CtfImeProcessKey
         22   14 00046A10 CtfImeRegisterWord
         23   15 00046A10 CtfImeSelect
         24   16 00036CC0 CtfImeSelectEx
         25   17 000184A0 CtfImeSetActiveContext
         26   18 000635C0 CtfImeSetCompositionString
         27   19 000635D0 CtfImeSetFocus
         28   1A 00063640 CtfImeToAsciiEx
         29   1B 00046A10 CtfImeUnregisterWord
         30   1C 00004A10 CtfNotifyIME
         31   1D 000049F0 DllCanUnloadNow
         32   1E 000018C0 DllGetClassObject
         33   1F 00061430 DllRegisterServer
         34   20 00061780 DllUnregisterServer
         35   21 0003FDD0 SetInputScope
         36   22 0008A580 SetInputScopeXML
         37   23 0003FE50 SetInputScopes
         38   24 0003FEA0 SetInputScopes2
         39   25 00063650 TF_CUASAppFix
         40   26 000056B0 TF_CanUninitialize
         41   27 00046540 TF_CleanUpPrivateMessages
         42   28 0003E5E0 TF_CreateCategoryMgr
         43   29 00002BB0 TF_CreateCicLoadMutex
         44   2A 000028E0 TF_CreateCicLoadWinStaMutex
         45   2B 00063690 TF_CreateDisplayAttributeMgr
         46   2C 000047F0 TF_CreateInputProcessorProfiles
         47   2D 000636B0 TF_CreateLangBarItemMgr
         48   2E 000396B0 TF_CreateLangBarMgr
         49   2F 000636D0 TF_CreateThreadMgr
         50   30 0000B110 TF_GetAppCompatFlags
         51   31 000636F0 TF_GetCompatibleKeyboardLayout
         52   32 0003EE90 TF_GetGlobalCompartment
         53   33 0006A6F0 TF_GetInitSystemFlags
         54   34 0003A7F0 TF_GetInputScope
         55   35 0008E580 TF_GetShowFloatingStatus
         56   36 00005410 TF_GetThreadFlags
         57   37 0000D5C0 TF_GetThreadMgr
         58   38 00002DB0 TF_InitSystem
         59   39 00046A10 TF_InvalidAssemblyListCacheIfExist
         60   3A 0006F4F0 TF_IsCtfmonRunning
         61   3B 00063880 TF_IsLanguageBarEnabled
         62   3C 00046A10 TF_IsThreadWithFlags
         63   3D 00063980 TF_MapCompatibleHKL
         64   3E 00063A10 TF_MapCompatibleKeyboardTip
         65   3F 000191B0 TF_Notify
         66   40 00005E90 TF_PostAllThreadMsg
          1   41 00063330 TF_RunInputCPL
         67   42 00063A20 TF_SendLangBandMsg
         68   43 00040550 TF_SetDefaultRemoteKeyboardLayout
         69   44 0008E590 TF_SetShowFloatingStatus
         70   45 00046A10 TF_SetThreadFlags
         71   46 00002D10 TF_UninitSystem
         72   47 00002C50 TF_WaitForInitialized
         73   48 000AA2E0 TextInputClientWrapperCreate
 
  Summary
 
        3000 .data
        1000 .didat
        D000 .pdata
       25000 .rdata
        2000 .reloc
       42000 .rsrc
       DF000 .text

百度:

继续百度搜索一翻。关键字:C#TextServiceFramework。第一篇《微软新一代输入法框架 TSF – Text Service Framework 小小的研究》,跳转至这篇博文时,则找到真正需要的东西。

Wrapper:

VISTA 與輸入法程式介面

整体代码如下:

文件:TSF.cs

该文件定义了 TSF 的结构以及各个方法的接口。其来源来自 C++ 的头文件。

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
///////////////////////////////////////////////////////////////////////////////////////////////
//               Microsoft Text Service Framework Declaration
//                        from C++ header file
//
//////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Security;
 
namespace TSF
{
    [StructLayout(LayoutKind.Sequential)]
    internal struct TF_LANGUAGEPROFILE
    {
        internal Guid clsid;
        internal short langid;
        internal Guid catid;
        [MarshalAs(UnmanagedType.Bool)]
        internal bool fActive;
        internal Guid guidProfile;
    }
 
    [ComImport, SecurityCritical, SuppressUnmanagedCodeSecurity,
Guid("1F02B6C5-7842-4EE6-8A0B-9A24183A95CA"),
     InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    internal interface ITfInputProcessorProfiles
    {
        [SecurityCritical]
        void Register(); //non-implement!! may be is wrong declaration.
        [SecurityCritical]
        void Unregister(); //non-implement!! may be is wrong declaration.
        [SecurityCritical]
        void AddLanguageProfile(); //non-implement!! may be is wrong declaration.
        [SecurityCritical]
        void RemoveLanguageProfile(); //non-implement!! may be is wrong declaration.
        [SecurityCritical]
        void EnumInputProcessorInfo(); //non-implement!! may be is wrong declaration.
        [SecurityCritical]
        int GetDefaultLanguageProfile(short langid, ref Guid catid, out Guid clsid, out Guid profile);
        [SecurityCritical]
        void SetDefaultLanguageProfile(); //non-implement!! may be is wrong declaration.
        [SecurityCritical]
        int ActivateLanguageProfile(ref Guid clsid, short langid, ref Guid guidProfile);
        [PreserveSig, SecurityCritical]
        int GetActiveLanguageProfile(ref Guid clsid, out short langid, out Guid profile);
        [SecurityCritical]
        int GetLanguageProfileDescription(ref Guid clsid, short langid, ref Guid profile, out IntPtr desc);
        [SecurityCritical]
        void GetCurrentLanguage(out short langid); //non-implement!! may be is wrong declaration.
        [PreserveSig, SecurityCritical]
        int ChangeCurrentLanguage(short langid); //non-implement!! may be is wrong declaration.
        [PreserveSig, SecurityCritical]
        int GetLanguageList(out IntPtr langids, out int count);
        [SecurityCritical]
        int EnumLanguageProfiles(short langid, out IEnumTfLanguageProfiles enumIPP);
        [SecurityCritical]
        int EnableLanguageProfile();
        [SecurityCritical]
        int IsEnabledLanguageProfile(ref Guid clsid, short langid, ref Guid profile, out bool enabled);
        [SecurityCritical]
        void EnableLanguageProfileByDefault(); //non-implement!! may be is wrong declaration.
        [SecurityCritical]
        void SubstituteKeyboardLayout(); //non-implement!! may be is wrong declaration.
    }
 
    [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
Guid("3d61bf11-ac5f-42c8-a4cb-931bcc28c744")]
    internal interface IEnumTfLanguageProfiles
    {
        void Clone(out IEnumTfLanguageProfiles enumIPP);
        [PreserveSig]
        int Next(int count, [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)]
TF_LANGUAGEPROFILE[] profiles, out int fetched);
        void Reset();
        void Skip(int count);
    }
 
    internal static class TSF_NativeAPI
    {
        public static readonly Guid GUID_TFCAT_TIP_KEYBOARD;
 
        static TSF_NativeAPI()
        {
            GUID_TFCAT_TIP_KEYBOARD = new Guid(0x34745c63, 0xb2f0,
0x4784, 0x8b, 0x67, 0x5e, 0x12, 200, 0x70, 0x1a, 0x31);
        }
 
        [SecurityCritical, SuppressUnmanagedCodeSecurity, DllImport("msctf.dll")]
        public static extern int TF_CreateInputProcessorProfiles(out ITfInputProcessorProfiles profiles);
    }
}

文件:TSFWapper.cs

这个文件封装了 TSF 的你需要调用的静态方法。更多的方法,可以在接口定义(上面文件)处找到。

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
public class TSFWapper
    {
        public static short[] GetLangIDs()
        {
            List<short> langIDs = new List<short>();
            ITfInputProcessorProfiles profiles;
            if (TSF_NativeAPI.TF_CreateInputProcessorProfiles(out profiles) == 0)
            {
                IntPtr langPtrs;
                int fetchCount = 0;
                if (profiles.GetLanguageList(out langPtrs, out fetchCount) == 0)
                {
                    for (int i = 0; i < fetchCount; i++)
                    {
                        short id = Marshal.ReadInt16(langPtrs, sizeof(short) * i);
                        langIDs.Add(id);
                    }
                }
                Marshal.ReleaseComObject(profiles);
            }
            return langIDs.ToArray();
        }
 
        public static string[] GetInputMethodList(short langID)
        {
            List<string> imeList = new List<string>();
            ITfInputProcessorProfiles profiles;
            if (TSF_NativeAPI.TF_CreateInputProcessorProfiles(out profiles) == 0)
            {
                try
                {
                    IEnumTfLanguageProfiles enumerator = null;
                    if (profiles.EnumLanguageProfiles(langID, out enumerator) == 0)
                    {
                        if (enumerator != null)
                        {
                            TF_LANGUAGEPROFILE[] langProfile = new TF_LANGUAGEPROFILE[1];
                            int fetchCount = 0;
                            while (enumerator.Next(1, langProfile, out fetchCount) == 0)
                            {
                                IntPtr ptr;
                                if (profiles.GetLanguageProfileDescription(ref langProfile[0].clsid,
                                    langProfile[0].langid, ref langProfile[0].guidProfile, out ptr) == 0)
                                {
                                    bool enabled;
                                    if (profiles.IsEnabledLanguageProfile(ref langProfile[0].clsid,
                                        langProfile[0].langid, ref langProfile[0].guidProfile, out enabled) == 0)
                                    {
                                        if (enabled)
                                            imeList.Add(Marshal.PtrToStringBSTR(ptr));
                                    }
                                }
                                Marshal.FreeBSTR(ptr);
                            }
                        }
                    }
                }
                finally
                {
                    Marshal.ReleaseComObject(profiles);
                }
            }
            return imeList.ToArray();
        }
 
        ///
        /// 获取当前输入法
        ///
        public static void GetCurrentLang(out string[] lang)
        {
            List<short> langIDs = new List<short>();
            ITfInputProcessorProfiles profiles;
            short id = -1;
            if (TSF_NativeAPI.TF_CreateInputProcessorProfiles(out profiles) == 0)
            {
                IntPtr langPtrs;
                int fetchCount = 0;
                profiles.GetCurrentLanguage(out id);
                Marshal.ReleaseComObject(profiles);
            }
            lang = id > -1 ? GetInputMethodList(id) : null;
        }
    }

C# TSF 输入法的获取的更多相关文章

  1. Android输入法框架系统(上)

    输入法,就是用来输入字符(包括英文,俄文,中文)的工具.输入法你可以看成是一种字符发生器,它将输入数据触摸事件或者按键事件转化为其他更丰富的字符.在PC时代,输入法的原始输入来自实体键盘,鼠标,然后输 ...

  2. Rime 小狼毫 注意事项

    https://rime.im/https://github.com/rime/weasel/pulse 打不出中文可能是,没有五笔需要的文件: wubi_pinyin.schema.yamlCtrl ...

  3. 2. Python3输入与输出

    数据的输入和输出操作是计算机最基本的操作,本节只研究基本的输入与输出,基本输入是指从键盘上输入数据的操作,基本输出是指屏幕上显示输出结果的操作. 2.1基本输入和输出 常用的输入与输出设备有很多,如摄 ...

  4. 用Jquery控制文本框只能输入数字和字母及jquery自定义方法$.fn

    封装成onlyNum(),onlyAlpha()和onlyNumAlpha()3个Jquery扩展方法,方便复用,由于里面一些JS代码涉及到了"禁用输入法,获取剪切板的内容",而& ...

  5. ADB常用指令

    adb 命令是adb程序自带的一些命令:adb shell则是调用Android系统的命令,Android系统特有的命令都放在Android设备的/system/bin目录中 MonkeyRunner ...

  6. ADB 常用命令学习

    参考文档:https://www.cnblogs.com/bravesnail/articles/5850335.html非常感谢作者的分享,以下是我学习的记录.Android 常用adb 命令汇总- ...

  7. Android输入法界面管理(打开/关闭/状态获取)

    最近做一个带发表情的聊天界面,需要管理系统输入法的状态, 一.打开输入法窗口: InputMethodManager inputMethodManager = (InputMethodManager) ...

  8. 微软新一代输入法框架 TSF - Text Service Framework 小小的研究

    实际上windows中有两套输入法框架,一套叫做imm32.一套叫做tsf,win7以后的新系统都是优先使用tsf的,现在新出的输入法基本也是基于tsf的. 你可以参考一下这篇文章,虽然是c++的代码 ...

  9. Android开发 - 获取系统输入法高度的正确姿势

    问题与解决 在Android应用的开发中,有一些需求需要我们获取到输入法的高度,但是官方的API并没有提供类似的方法,所以我们需要自己来实现. 查阅了网上很多资料,试过以后都不理想. 比如有的方法通过 ...

随机推荐

  1. Java 学习(20):Java Applet 基础 & Java 文档注释

    -- Java Applet 基础 -- Java 文档注释 Java Applet 基础 Applet 是一种 Java 程序.它一般运行在支持 Java 的 Web 浏览器内.因为它有完整的 Ja ...

  2. [疯狂Java]JDBC:事务管理、中间点、批量更新

    1. 数据库事务的概念:     1) 事务的目的就是为了保证数据库中数据的完整性.     2) 设想一个银行转账的过程,假设分两步,第一步是A的账户-1000,第二步是B的账户+1000.这两个动 ...

  3. [TypeStyle] Style CSS pseudo-classes using TypeStyle with $nest

    TypeStyle is a very thin layer on top of CSS. In this lesson we show how to change styles based on p ...

  4. iOS开发项目实战——Swift实现ScrollView滚动栏功能

    手机作为一个小屏设备,须要显示的信息往往无法在一个屏幕上显示,此时就须要使用到滚动栏,当然除了像TableView这样能够自带滚动功能的. 假设一个界面上View较多,那就必须要使用到ScrollVi ...

  5. 经验总结56--mybatis返回主键

    使用mybatis框架时,有时候须要新插入的数据的主键是多少. 1.oracle 因为oracle是建的序列文件,获取ID值. <insert id="insert" par ...

  6. hdu3360National Treasures (最大匹配,拆点法)

    National Treasures Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...

  7. js中json数据简单处理(JSON.parse()和js中嵌套html)

    js中json数据简单处理(JSON.parse()和js中嵌套html) 一.总结 1.html中嵌套js:<script>js代码</script> 2.js中嵌套html ...

  8. [转载]Ocelot简易教程(五)之集成IdentityServer认证以及授权

    作者:依乐祝 原文地址:https://www.cnblogs.com/yilezhu/p/9807125.html 最近比较懒,所以隔了N天才来继续更新第五篇Ocelot简易教程,本篇教程会先简单介 ...

  9. rabbitmq.config详细配置参数

    原文:rabbitmq.config详细配置参数 rabbitmq.config详细配置参数 详细使用方法请点击:http://blog.csdn.net/Super_RD/article/detai ...

  10. SDE 空间表操作

    1. 创建空间表(包含st_geometry属性字段) CREATE TABLE sensitive_areas (area_id integer, name varchar(128), area_s ...