TSF输入法列表获取

本文介绍了一个在Windows7 SP1 (x64) 和 Embaracadero Delphi 10.2 中遇到的TSF输入法切换异常问题,详细展示了引发访问违规错误的代码段,并提供了解决方案。通过检查并正确使用TSF API,可以避免在尝试读取或写入无效地址时出现异常。
摘要由CSDN通过智能技术生成

Windows 7 SP1(x64) 

Embarcadero® Delphi 10.2 Version 25.0.26309.314

---------------------------
Debugger Exception Notification
---------------------------
Project Project1.exe raised exception class $C0000005 with message 'access violation at 0xfeeefeee: read of address 0xfeeefeee'.
---------------------------
 

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Winapi.MsCTF;

type
  TForm1 = class(TForm)
    btnSTF: TButton;
    Memo1: TMemo;
    procedure btnSTFClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btnSTFClick(Sender: TObject);
var
  profiles: PPTfInputProcessorProfiles;
  Iprofiles: ITfInputProcessorProfiles;
  plangid: Word;
  enumerator: IEnumTfLanguageProfiles;
  langProfile: TF_LANGUAGEPROFILE;
  FetchedItems: Cardinal;
  pbstrProfile: WideString;
  pfEnable: Integer;
begin
  if not IsMSCTFAvailable() then
    Exit;

  if TF_CreateInputProcessorProfiles(profiles) = (S_OK) then
  begin
    Iprofiles := ITfInputProcessorProfiles(profiles^);
    Iprofiles.GetCurrentLanguage(plangid);
    Memo1.Lines.Add(IntToStr(plangid));

    if plangid > -1 then
    begin
      Iprofiles.EnumLanguageProfiles(plangid, enumerator);

      if Assigned(enumerator) then
      begin
        enumerator.Next(1, langProfile, FetchedItems);
        while FetchedItems > 0 do
        begin
          //          Memo1.Lines.Add(langProfile.clsid.ToString());
          //          Memo1.Lines.Add(langProfile.langid.ToString());
          //          Memo1.Lines.Add(langProfile.fActive.ToString());  // -1时为当前激活输入法

          pbstrProfile := '';
          if Iprofiles.GetLanguageProfileDescription(langProfile.clsid, langProfile.langid, langProfile.guidProfile, pbstrProfile) = S_OK then
          begin
            //Memo1.Lines.Add(pbstrProfile);
            if Iprofiles.IsEnabledLanguageProfile(langProfile.clsid, langProfile.langid, langProfile.guidProfile, pfEnable) = S_OK then
            begin
              if pfEnable = 1 then
              begin
                //显示TSF输入法名称
                Memo1.Lines.Add(pbstrProfile);

                //激活指定输入法(clsid、 guidProfile为空时,取消激活)
                if pbstrProfile = '微软ABC' then
                  Iprofiles.ActivateLanguageProfile(langProfile.clsid, langProfile.langid, langProfile.guidProfile);
              end;
            end;
          end;

          enumerator.Next(1, langProfile, FetchedItems);
        end;
      end;

    end;

  end;
end;

end.

转载:https://blog.csdn.net/harvardfeng/article/details/77962291

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值