none
win8以上系统输入法,利用ITfInputProcessorProfileMgr::ActivateProfile无法切换到美式键盘英文状态问题 RRS feed

  • 问题

  • 我在修改游戏输入法问题的时候,发现用ITfInputProcessorProfileMgr::ActivateProfile(2,0x04090409,clsid_NULL,guid_NULL)函数执行成功了,返回S_OK,但是界面上的输入法并没有切换过来,本人的系统是win10,装有搜狗输入法和QQ输入法 (自己在商店输入框 试过,这两个输入法 可以正常工作,也符合win8,win10以上对第三方输入法的要求),也装有以前的美式键盘(英文)。我的目的是游戏内玩家不聊天了,把输入法切换到美式键盘(英文),(为啥要这样,做游戏的都知道shift在游戏内都会被用上,所以只是把输入法的状态切换英文,玩家一按Shift,直接卡住,所以需要切换成纯英文的,不会被shift按一下就修改成中文)。我有完整测试代码:

                   代码看似很多,实际很多是打印东西,主要是三个部分,一个是用ITfInputProcessorProfiles 取目前的中文输入法列表,其他两个是用 ITfInputProcessorProfileMgr来去中文和英文输入法列表。mgr可以把keyboardlayout(就是支持IMM的,不支持TSF)都打印出来,profile不行。                            


    #include <windows.h>
    #include <msctf.h>
    #include <stdio.h>
    #include <wchar.h>
    #include <comutil.h>
    #include <imm.h>
    int main(int argc, TCHAR* argv[])
    {
    CoInitialize(0);
    HRESULT hr = S_OK;

    //PunCha:创建Profiles接口被
    ITfInputProcessorProfiles *pProfiles;

    hr = CoCreateInstance(  CLSID_TF_InputProcessorProfiles,
    NULL,
    CLSCTX_INPROC_SERVER,
    IID_ITfInputProcessorProfiles,
    (LPVOID*)&pProfiles);

    if(SUCCEEDED(hr))
    {
    LANGID langid;  
    hr = pProfiles->GetCurrentLanguage(&langid);  
    CLSID textSrvId, profileId;  
    hr = pProfiles->GetDefaultLanguageProfile(langid, GUID_TFCAT_TIP_KEYBOARD, &textSrvId, &profileId); 
    hr = pProfiles->GetActiveLanguageProfile(textSrvId, &langid, &profileId);  
    printf("Begin_____________:currentLanguage:%08x,",langid);

    BSTR bstrDest;
    hr = pProfiles->GetLanguageProfileDescription(textSrvId,langid,profileId, &bstrDest);
    if (hr == S_OK)
    {
    OutputDebugString(bstrDest);
    const char* tempstr =  _com_util::ConvertBSTRToString(bstrDest); 
    printf(tempstr); 
    printf("\n");
    printf( "CurrentActiveLanguageProfile:{clsid:%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}", textSrvId.Data1, textSrvId.Data2,textSrvId.Data3, textSrvId.Data4[1],textSrvId.Data4[0], textSrvId.Data4[7], textSrvId.Data4[6], textSrvId.Data4[5], textSrvId.Data4[4], textSrvId.Data4[3], textSrvId.Data4[2]);
    printf( "CurrentActiveLanguageProfile:{ProfileID:%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}\n\n\n", profileId.Data1, profileId.Data2, profileId.Data3, profileId.Data4[1],profileId.Data4[0], profileId.Data4[7], profileId.Data4[6], profileId.Data4[5], profileId.Data4[4], profileId.Data4[3], profileId.Data4[2]);

    }
    printf("\n");
    /* hr = pProfiles->ChangeCurrentLanguage(0x409);
    hr = pProfiles->GetCurrentLanguage(&langid);
    hr = pProfiles->GetDefaultLanguageProfile(langid, GUID_TFCAT_TIP_KEYBOARD, &textSrvId, &profileId); 
    hr = pProfiles->GetActiveLanguageProfile(textSrvId, &langid, &profileId); */

    ITfInputProcessorProfileMgr*pProfileMgr;
    hr = pProfiles->QueryInterface(__uuidof(ITfInputProcessorProfileMgr),(LPVOID*)&pProfileMgr);
    if (SUCCEEDED(hr))
    {
    IEnumTfLanguageProfiles* pEnumProf = 0;

    hr = pProfiles->EnumLanguageProfiles(0x804, &pEnumProf);

    if (SUCCEEDED(hr) && pEnumProf)
    {
    TF_LANGUAGEPROFILE proArr[2];
    ULONG feOut = 0;

    while (S_OK == pEnumProf->Next(1, proArr, &feOut))
    {
    BSTR bstrDest;
    hr = pProfiles->GetLanguageProfileDescription(proArr[0].clsid,0x804,proArr[0].guidProfile, &bstrDest);
    OutputDebugString(bstrDest);
    const char* tempstr =  _com_util::ConvertBSTRToString(bstrDest); 
    printf(tempstr); 
    printf("\n");
    BOOL bEnable = false;
    hr = pProfiles->IsEnabledLanguageProfile(proArr[0].clsid, proArr[0].langid, proArr[0].guidProfile, &bEnable);
    if (SUCCEEDED(hr))
    {
    printf("before Enabled %d\n", bEnable);
    }
    // LANGID templangid;
    // pProfiles->GetCurrentLanguage(&templangid);
    // hr = pProfiles->ActivateLanguageProfile(proArr[0].clsid,proArr[0].langid, proArr[0].guidProfile);

    printf("bActive:%d,langid:%d,hkl:%08x",proArr[0].fActive?1:0,proArr[0].langid);
    printf( "{clsid:%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}", proArr[0].clsid.Data1, proArr[0].clsid.Data2, proArr[0].clsid.Data3, proArr[0].clsid.Data4[1],proArr[0].clsid.Data4[0], proArr[0].clsid.Data4[7], proArr[0].clsid.Data4[6], proArr[0].clsid.Data4[5], proArr[0].clsid.Data4[4], proArr[0].clsid.Data4[3], proArr[0].clsid.Data4[2]);
    printf( "{ProfileID:%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}\n\n\n", proArr[0].guidProfile.Data1, proArr[0].guidProfile.Data2, proArr[0].guidProfile.Data3, proArr[0].guidProfile.Data4[1],proArr[0].guidProfile.Data4[0], proArr[0].guidProfile.Data4[7], proArr[0].guidProfile.Data4[6], proArr[0].guidProfile.Data4[5], proArr[0].guidProfile.Data4[4], proArr[0].guidProfile.Data4[3], proArr[0].guidProfile.Data4[2]);

    //

    hr = pProfiles->EnableLanguageProfile(proArr[0].clsid,0x804, proArr[0].guidProfile,true);
    //
    }
    }



    TF_INPUTPROCESSORPROFILE tempPro;

    hr = pProfileMgr->GetActiveProfile(GUID_TFCAT_TIP_KEYBOARD,&tempPro);

    IEnumTfInputProcessorProfiles* pIPP;
    hr = pProfileMgr->EnumProfiles(0x804, &pIPP);

    if (SUCCEEDED(hr) && pIPP)

    TF_INPUTPROCESSORPROFILE proArr[5];
    ULONG feOut = 0;
    int i=0;

    while (S_OK == pIPP->Next(1, proArr, &feOut))
    {
    if (proArr[0].langid != 0x804)
    {
    continue;
    }
    i++;
    printf("bActive:%d,bEnable:%d,SUBSTITUTEDBYINPUTPROCESSOR:%d\n",proArr[0].dwFlags&TF_IPP_FLAG_ACTIVE?1:0,proArr[0].dwFlags&TF_IPP_FLAG_ENABLED?1:0,proArr[0].dwFlags&TF_IPP_FLAG_SUBSTITUTEDBYINPUTPROCESSOR?1:0);
    printf("type:%d,langid:%d,hklsubstitute:%08x,hkl:%08x",proArr[0].dwProfileType,proArr[0].langid,proArr[0].hklSubstitute,proArr[0].hkl);
    printf( "{clsid:%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}", proArr[0].clsid.Data1, proArr[0].clsid.Data2, proArr[0].clsid.Data3, proArr[0].clsid.Data4[1],proArr[0].clsid.Data4[0], proArr[0].clsid.Data4[7], proArr[0].clsid.Data4[6], proArr[0].clsid.Data4[5], proArr[0].clsid.Data4[4], proArr[0].clsid.Data4[3], proArr[0].clsid.Data4[2]);
    printf( "{ProfileID:%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}\n", proArr[0].guidProfile.Data1, proArr[0].guidProfile.Data2, proArr[0].guidProfile.Data3, proArr[0].guidProfile.Data4[1],proArr[0].guidProfile.Data4[0], proArr[0].guidProfile.Data4[7], proArr[0].guidProfile.Data4[6], proArr[0].guidProfile.Data4[5], proArr[0].guidProfile.Data4[4], proArr[0].guidProfile.Data4[3], proArr[0].guidProfile.Data4[2]);


    hr = pProfileMgr->DeactivateProfile(proArr[0].dwProfileType,proArr[0].langid,proArr[0].clsid,proArr[0].guidProfile,proArr[0].hkl,TF_IPPMF_FORPROCESS|TF_IPPMF_FORSESSION);
    if (hr == S_OK)
    {
    printf("DeactivateProfile success!\n");
    }
    else if (hr == E_INVALIDARG)
    {
    printf("DeactivateProfile invalid arg!\n");
    }
    else if (hr == E_FAIL)
    {
    printf("DeactivateProfile fail!\n");
    }
    printf("pProfileMgr->GetActiveProfile begin----");
    hr = pProfileMgr->GetActiveProfile(GUID_TFCAT_TIP_KEYBOARD,&tempPro);
    if (hr == S_OK)
    {
    printf( "now get active,Language:%08x,{ProfileID:%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}\n",tempPro.langid,tempPro.guidProfile.Data1, tempPro.guidProfile.Data2, tempPro.guidProfile.Data3,tempPro.guidProfile.Data4[1],tempPro.guidProfile.Data4[0], tempPro.guidProfile.Data4[7], tempPro.guidProfile.Data4[6], tempPro.guidProfile.Data4[5], tempPro.guidProfile.Data4[4], tempPro.guidProfile.Data4[3], tempPro.guidProfile.Data4[2]);
    }
    else
    {
    printf( "pProfileMgr->GetActiveProfile fail!\n");
    }
    printf("pProfiles->GetActiveLanguageProfile begin---");
    {
    LANGID langid;  
    hr = pProfiles->GetCurrentLanguage(&langid);  
    CLSID textSrvId, profileId;  
    hr = pProfiles->GetDefaultLanguageProfile(langid, GUID_TFCAT_TIP_KEYBOARD, &textSrvId, &profileId); 
    hr = pProfiles->GetActiveLanguageProfile(textSrvId, &langid, &profileId);  
    printf("currentLanguage:%08x\n",langid);

    BSTR bstrDest;
    hr = pProfiles->GetLanguageProfileDescription(textSrvId,langid,profileId, &bstrDest);
    if (hr == S_OK)
    {
    OutputDebugString(bstrDest);
    const char* tempstr =  _com_util::ConvertBSTRToString(bstrDest); 
    printf(tempstr); 
    printf("\n");
    printf( "CurrentActiveLanguageProfile:{clsid:%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}", textSrvId.Data1, textSrvId.Data2,textSrvId.Data3, textSrvId.Data4[1],textSrvId.Data4[0], textSrvId.Data4[7], textSrvId.Data4[6], textSrvId.Data4[5], textSrvId.Data4[4], textSrvId.Data4[3], textSrvId.Data4[2]);
    printf( "CurrentActiveLanguageProfile:{ProfileID:%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}\n", profileId.Data1, profileId.Data2, profileId.Data3, profileId.Data4[1],profileId.Data4[0], profileId.Data4[7], profileId.Data4[6], profileId.Data4[5], profileId.Data4[4], profileId.Data4[3], profileId.Data4[2]);
    }
    }
    printf("\n");
    }
    printf("all inputprocess: %d\n\n\n",i);
    }

    /* hr = pProfiles->ChangeCurrentLanguage(0x409);
     hr = pProfileMgr->GetActiveProfile(GUID_TFCAT_TIP_KEYBOARD,&tempPro);*/
    IEnumTfInputProcessorProfiles* pIPP1;
    hr = pProfileMgr->EnumProfiles(0x409, &pIPP1);

    if (SUCCEEDED(hr) && pIPP1)

    TF_INPUTPROCESSORPROFILE proArr[5];
    ULONG feOut = 0;
    int i=0;

    while (S_OK == pIPP1->Next(1, proArr, &feOut))
    {
    if (proArr[0].langid != 0x409)
    {
    continue;
    }
    i++;
     printf("bActive:%d,bEnable:%d,SUBSTITUTEDBYINPUTPROCESSOR:%d\n",proArr[0].dwFlags&TF_IPP_FLAG_ACTIVE?1:0,proArr[0].dwFlags&TF_IPP_FLAG_ENABLED?1:0,proArr[0].dwFlags&TF_IPP_FLAG_SUBSTITUTEDBYINPUTPROCESSOR?1:0);
    printf("type:%d,langid:%d,hkl:%08x",proArr[0].dwProfileType,proArr[0].langid,proArr[0].hkl);
    printf( "{clsid:%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}", proArr[0].clsid.Data1, proArr[0].clsid.Data2, proArr[0].clsid.Data3, proArr[0].clsid.Data4[1],proArr[0].clsid.Data4[0], proArr[0].clsid.Data4[7], proArr[0].clsid.Data4[6], proArr[0].clsid.Data4[5], proArr[0].clsid.Data4[4], proArr[0].clsid.Data4[3], proArr[0].clsid.Data4[2]);
    printf( "{ProfileID:%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}\n", proArr[0].guidProfile.Data1, proArr[0].guidProfile.Data2, proArr[0].guidProfile.Data3, proArr[0].guidProfile.Data4[1],proArr[0].guidProfile.Data4[0], proArr[0].guidProfile.Data4[7], proArr[0].guidProfile.Data4[6], proArr[0].guidProfile.Data4[5], proArr[0].guidProfile.Data4[4], proArr[0].guidProfile.Data4[3], proArr[0].guidProfile.Data4[2]);
    hr = pProfileMgr->ActivateProfile(proArr[0].dwProfileType,proArr[0].langid,proArr[0].clsid,proArr[0].guidProfile,proArr[0].hkl,TF_IPPMF_FORPROCESS|TF_IPPMF_FORSESSION|TF_IPPMF_ENABLEPROFILE|TF_IPPMF_FORSYSTEMALL|TF_IPPMF_DONTCARECURRENTINPUTLANGUAGE);

    if (hr == S_OK)
    {
    printf("ActivateProfile success!\n");
    }
    else if (hr == E_INVALIDARG)
    {
    printf("ActivateProfile invalid arg!\n");
    }
    else if (hr == E_FAIL)
    {
    printf("ActivateProfile fail!\n");
    }

    hr = pProfileMgr->GetActiveProfile(GUID_TFCAT_TIP_KEYBOARD,&tempPro);
    if (hr == S_OK)
    {
    printf( "now get active,Language:%08x,{ProfileID:%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}\n",tempPro.langid,tempPro.guidProfile.Data1, tempPro.guidProfile.Data2, tempPro.guidProfile.Data3,tempPro.guidProfile.Data4[1],tempPro.guidProfile.Data4[0], tempPro.guidProfile.Data4[7], tempPro.guidProfile.Data4[6], tempPro.guidProfile.Data4[5], tempPro.guidProfile.Data4[4], tempPro.guidProfile.Data4[3], tempPro.guidProfile.Data4[2]);

    }
    else
    {
    printf( "pProfileMgr->GetActiveProfile fail!\n");
    }
    printf("pProfiles->GetActiveLanguageProfile begin---");
    {
    LANGID langid;  
    hr = pProfiles->GetCurrentLanguage(&langid);  
    CLSID textSrvId, profileId;  
    hr = pProfiles->GetDefaultLanguageProfile(langid, GUID_TFCAT_TIP_KEYBOARD, &textSrvId, &profileId); 
    hr = pProfiles->GetActiveLanguageProfile(textSrvId, &langid, &profileId);  
    printf("currentLanguage:%08x\n",langid);

    BSTR bstrDest;
    hr = pProfiles->GetLanguageProfileDescription(textSrvId,langid,profileId, &bstrDest);
    if (hr == S_OK)
    {
    OutputDebugString(bstrDest);
    const char* tempstr =  _com_util::ConvertBSTRToString(bstrDest); 
    printf(tempstr); 
    printf("\n");
    printf( "CurrentActiveLanguageProfile:{clsid:%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}", textSrvId.Data1, textSrvId.Data2,textSrvId.Data3, textSrvId.Data4[1],textSrvId.Data4[0], textSrvId.Data4[7], textSrvId.Data4[6], textSrvId.Data4[5], textSrvId.Data4[4], textSrvId.Data4[3], textSrvId.Data4[2]);
    printf( "CurrentActiveLanguageProfile:{ProfileID:%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}\n", profileId.Data1, profileId.Data2, profileId.Data3, profileId.Data4[1],profileId.Data4[0], profileId.Data4[7], profileId.Data4[6], profileId.Data4[5], profileId.Data4[4], profileId.Data4[3], profileId.Data4[2]);
    }
    }
    printf("\n");

    printf("all inputprocess: %d\n",i);
    hr =  pProfiles->ChangeCurrentLanguage(0x409);
    if (hr == S_OK)
    {
    printf("Change language success");
    }
    printf("After active:\n");

    IEnumTfInputProcessorProfiles* pIPP2;
    hr = pProfileMgr->EnumProfiles(0x409, &pIPP2);
    while (S_OK == pIPP2->Next(1, proArr, &feOut))
    {
    if (proArr[0].langid != 0x409)
    {
    continue;
    }
    i++;
    printf("bActive:%d,bEnable:%d,SUBSTITUTEDBYINPUTPROCESSOR:%d\n",proArr[0].dwFlags&TF_IPP_FLAG_ACTIVE?1:0,proArr[0].dwFlags&TF_IPP_FLAG_ENABLED?1:0,proArr[0].dwFlags&TF_IPP_FLAG_SUBSTITUTEDBYINPUTPROCESSOR?1:0);
    printf("type:%d,langid:%d,hkl:%08x",proArr[0].dwProfileType,proArr[0].langid,proArr[0].hkl);
    printf( "{clsid:%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}", proArr[0].clsid.Data1, proArr[0].clsid.Data2, proArr[0].clsid.Data3, proArr[0].clsid.Data4[1],proArr[0].clsid.Data4[0], proArr[0].clsid.Data4[7], proArr[0].clsid.Data4[6], proArr[0].clsid.Data4[5], proArr[0].clsid.Data4[4], proArr[0].clsid.Data4[3], proArr[0].clsid.Data4[2]);
    printf( "{ProfileID:%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}\n", proArr[0].guidProfile.Data1, proArr[0].guidProfile.Data2, proArr[0].guidProfile.Data3, proArr[0].guidProfile.Data4[1],proArr[0].guidProfile.Data4[0], proArr[0].guidProfile.Data4[7], proArr[0].guidProfile.Data4[6], proArr[0].guidProfile.Data4[5], proArr[0].guidProfile.Data4[4], proArr[0].guidProfile.Data4[3], proArr[0].guidProfile.Data4[2]);

    }

    }

    pProfiles->Release();
    }

    }


    CoUninitialize();

    /* int nSize = GetKeyboardLayoutList(0 , 0);  
    if(nSize != 0)  
    {  
    HKL FAR * lpList = (HKL FAR *)new HKL[nSize];  
    ZeroMemory(lpList, sizeof(HKL) * nSize);  
    if(GetKeyboardLayoutList(nSize, lpList) == nSize)  
    {  
    for(int i=0; i<nSize; i++)  

    printf("%08X\n ",lpList[i]);
    }
    }
    }*/
    /* int input1;
    scanf("%i",&input1);*/
    while(1){};
    return 0;
    }

    测试软件vs2010.

    希望能收到你们的回复,这个问题我查阅TSF以及相关输入法的内容并测试了一周,卡在这里。


    • 已编辑 hfybird 2015年11月30日 4:41
    2015年11月30日 4:19

全部回复

  • 都没人回答。这几年感觉微软明显在没落,连个论坛都没人管理。问一个问题根本找不到人。
    2015年12月19日 8:07