如何将Text Services Framework用于.net应用程序

时间:2010-05-04 04:45:33

标签: .net com text-services-framework

我想在我的.net应用程序中使用Com接口, 但这与文本服务框架有关,我对此进行了研究,它就像它只支持COM服务器 谁可以帮我这个事 我可以在我的.net应用程序中使用TSF接口 如果是,请告诉我该怎么做 plzzzzzz :) 感谢

1 个答案:

答案 0 :(得分:3)

嘿我有同样的问题,我正在评估我是否真的必须使用C ++来完成这项工作。您当然可以在任何COM兼容环境中创建COM服务器,因此也可以在.NET中创建。 我到目前为止还没有任何可用状态,但我可以告诉你到目前为止我做了什么。 首先,msctf.dll只有一个标题和一个IDL文件(在Windows SDK 7.0中),必须对其进行修改才能获得类型库(tlb)。我使用了codeplex中的tlbimp2,它​​有一个基于xml的重写机制(这样可以减轻类型库中指针的大量使用: 这是我的批处理文件

set sdk7=C:\Program Files\Microsoft SDKs\Windows\v7.0
set imported=msctf

rem call "%sdk7%\Bin\SetEnv.cmd"
midl "%sdk7%\Include\%imported%.idl"
rem i copied the tlbimp2 into the sdk bin
tlbimp2 /keyfile:TextService.snk %imported%.tlb /config:msctf.xml
rem tlbimp /keyfile:TextService.snk %imported%.tlb

rem not sure about this
gacutil /u %imported%
gacutil /i %imported%.dll

这是我的XML规则文件(当然要扩展) http://clrinterop.codeplex.com/

<Rules>
  <Rule Name="addlangprofile string 1" Category="Signature">
    <Condition>
      <And>
        <NativeParentFunctionName Operator="Equal" Value="AddLanguageProfile" />
        <NativeParameterIndex Operator="Equal" Value="4" />
      </And>
    </Condition>
    <Action Name="ConvertTo">
      <Parameter Key="Direction" Value="[In]" />
      <Parameter Key="ByRef" Value="False" />
      <Parameter Key="ManagedType" Value="LPArray" />
      <Parameter Key="MarshalAs" Value="(default)" />
      <Parameter Key="Attributes" Value="[SizeParamIndexOffset=+1]" />
    </Action>
  </Rule>
  <Rule Name="addlanguageprofile string2" Category="Signature">
    <Condition>
      <And>
        <NativeParentFunctionName Operator="Equal" Value="AddLanguageProfile" />
        <NativeParameterIndex Operator="Equal" Value="6" />
      </And>
     </Condition>
     <Action Name="ConvertTo">
       <Parameter Key="Direction" Value="[In]" />
       <Parameter Key="ByRef" Value="False" />
       <Parameter Key="ManagedType" Value="LPArray" />
       <Parameter Key="MarshalAs" Value="(default)" />
       <Parameter Key="Attributes" Value="[SizeParamIndexOffset=+1]" />
     </Action>
   </Rule>
   <Rule Name="GUID" Category="Type">
     <Condition>
       <And>
         <NativeName Operator="Equal" Value="GUID" />
       </And>
     </Condition>
     <Action Name="ResolveTo">
       <Parameter Key="AssemblyName" Value="mscorlib" />
       <Parameter Key="ManagedTypeFullName" Value="System.Guid" />
     </Action>
   </Rule>
</Rules>

然后我试图将接口包装成更友好的.NET:

使用System;     使用System.Collections.Generic;     使用System.Linq;     使用System.Text;     使用System.Globalization;

using MSCTF;
using System.Runtime.InteropServices;

namespace TextService
{
    public class LanguageProfiles
    {
        private ITfInputProcessorProfiles instance;

        public LanguageProfiles()
        {
            instance = new COMIFace<ITfInputProcessorProfiles>().CreateInstance();
        }

        public CultureInfo CurrentLanguage
        {
            get
            {
                ushort plangid;
                instance.GetCurrentLanguage(out plangid);
                return CultureInfo.GetCultureInfo(plangid);
            }
            set
            {
                instance.ChangeCurrentLanguage((ushort) value.LCID);
            }
        }

        public IEnumerable<TF_LANGUAGEPROFILE> ProfilesOfLanguage(CultureInfo culture)
        {
            IEnumTfLanguageProfiles ppenum;
            instance.EnumLanguageProfiles( (ushort) culture.LCID, out ppenum);

            TF_LANGUAGEPROFILE profile;
            uint fetch;
            do
            {
                ppenum.Next(1, out profile, out fetch);
                yield return profile;
            } while (fetch == 1 && profile.fActive != -1);
        }

        public void Register(ref Guid rclsid)
        {
            instance.Register(ref rclsid);
        }

        public void Unregister(ref Guid rclsid)
        {
            instance.Unregister(ref rclsid);
        }

        public void Add(ref Guid rclsid, CultureInfo info, string name, string icon)
        {
            var empty = Guid.Empty;
            instance.AddLanguageProfile(ref empty, (ushort)info.LCID, ref rclsid, name.ToUShortArray(), name.ULength(), icon.ToUShortArray(), icon.ULength(), 0);
        }

        public void Remove(ref Guid rclsid, CultureInfo info)
        {
            instance.RemoveLanguageProfile(ref rclsid, (ushort)info.LCID, ref rclsid);
        }
    }
}

COMIFace类只是从注册表中获取IID的帮助器,因为我只在使用IDL编译器生成的.c文件中找到它们。更好的是解析我认为的文件,但这也很好。 给定的类工作正常,我可以使用以下批处理注册服务(检查是否在C#项目选项中启用了regasm)

set outtype=Debug
set asmname=TextService
cd bin\%outtype%
gacutil /u %asmname%
gacutil /i %asmname%.dll
cd ..\..

我遇到的问题是所有这些都是一项巨大的努力,并且有许多问题不值得我认为的痛苦。我不确定,并希望任何人都有一些指导。 关键是,人们总是需要遵循这样一个伟大的网站:TSF Aware blog 但是你可以用C ++编写它。 一个选项可能是C ++ / CLR,在C ++中执行所有COM内容和注册以及在C#中使用逻辑,我猜可能

这里有一些测试代码,显示它基本上有效:

var profiles = new LanguageProfiles();

var ko_KR = CultureInfo.GetCultureInfo("ko-KR");

foreach (var profile in profiles.ProfilesOfLanguage(ko_KR))
{
    Console.WriteLine("clsid: " + profile.clsid + " lid: " + CultureInfo.GetCultureInfo(profile.langid) + " catid: " + profile.catid + " active: " + profile.fActive + " guidProf: " + profile.guidProfile);
    var id = profile.clsid;
}