能'在Windows 10下关闭OSK.exe

时间:2017-02-21 05:50:28

标签: c++ winapi mfc win32gui

我使用下面的代码打开osk.exe

HINSTANCE Dlg::ExecuteOSK()
{
    typedef BOOL (WINAPI * LPFN_IsWow64Process)(HANDLE, PBOOL);
    typedef BOOL (WINAPI * LPFN_Wow64DisableWow64FsRedirection)(PVOID *);
    typedef BOOL (WINAPI * LPFN_Wow64RevertWow64FsRedirection)(PVOID);
    LPFN_IsWow64Process fnIsWow64Process = NULL;
    LPFN_Wow64DisableWow64FsRedirection fnWow64DisableWow64FsRedirection = NULL;
    LPFN_Wow64RevertWow64FsRedirection fnWow64RevertWow64FsRedirection = NULL;
    fnIsWow64Process = (LPFN_IsWow64Process) GetProcAddress(GetModuleHandle(TEXT("kernel32")), "IsWow64Process");
    fnWow64DisableWow64FsRedirection = (LPFN_Wow64DisableWow64FsRedirection) GetProcAddress(GetModuleHandle(TEXT("kernel32")), "Wow64DisableWow64FsRedirection");
    fnWow64RevertWow64FsRedirection = (LPFN_Wow64RevertWow64FsRedirection) GetProcAddress(GetModuleHandle(TEXT("kernel32")), "Wow64RevertWow64FsRedirection");
    BOOL bIsWow64 = FALSE;
    PVOID OldValue = NULL;
    HINSTANCE handle;
    if (NULL == fnIsWow64Process || NULL == fnWow64DisableWow64FsRedirection || NULL == fnWow64RevertWow64FsRedirection)
    {
        handle = ::ShellExecute(NULL, "open", "OSK", NULL, NULL, SW_SHOW);
    }
    else
    {
        fnIsWow64Process(GetCurrentProcess(), &bIsWow64);

        if (TRUE == bIsWow64)
        {
            fnWow64DisableWow64FsRedirection(&OldValue);
            handle = ::ShellExecute(NULL, "open", "OSK", NULL, NULL, SW_SHOW);
            fnWow64RevertWow64FsRedirection(OldValue);
        }
        else
        {
            handle = ::ShellExecute(NULL, "open", "OSK", NULL, NULL, SW_SHOW);
        }
    }
    return handle;
}

然后,我想在用户按回车键时关闭osk.exe。

BOOL CALLBACK EnumWindowsProc(
  _In_ HWND   hwnd,
   _In_ LPARAM lParam)
{
    char name[256];
    GetClassName( hwnd, name, sizeof(name) );
    if(strcmp(name,"OSKMainClass") == 0)
        SendMessage(hwnd, WM_CLOSE  , NULL, NULL); //I have tried WM_DESTROY
    return TRUE;
}

BOOL Dlg::PreTranslateMessage(MSG* pMsg) 
{
    if (pMsg->message == WM_KEYDOWN)
    {       
        int nKey = (int)pMsg->wParam;       

        switch(nKey)            
        {
        case VK_RETURN:     
            EnumWindows(EnumWindowsProc, NULL);

        }
    }

    return CBitmapDialog::PreTranslateMessage(pMsg);
}

但是,这部分代码不能用于关闭osk.exe成功。

我试图抓住另一个窗口,它可以成功关闭。

在Windows 10上是否存在问题?

1 个答案:

答案 0 :(得分:5)

如果您想要显示osk.exe,可以在此处查看 - uiAccess="true"这是User Interface Privilege Isolation (UIPI),也可以阅读类似问题UIAccess in Manifest Files

因为osk.exe在清单中有uiAccess="true",因此令牌中有Mandatory Label\High Mandatory Level。但是你的应用程序,如果在未提升的UAC下运行,通常会Medium Mandatory Level。结果:

  

较低权限的流程不能:

     
      
  • 将SendMessage或PostMessage用于以更高权限运行的应用程序窗口。这些API返回成功但静默地删除窗口   消息。
  •   

但如果您的应用在令牌中获得High Mandatory Level,则可以通过下一代码关闭osk.exe

if (HWND hwnd = FindWindow(L"OSKMainClass", 0))
{
    PostMessage(hwnd, WM_CLOSE, 0, 0);
}

因此您需要在清单中使用<requestedExecutionLevel level='requireAdministrator' uiAccess='false'/>或以某种方式将您的应用程序作为提升

运行