简体   繁体   English

在Windows 10下无法关闭OSK.exe

[英]Can' t close OSK.exe under Windows 10

I use below code to open osk.exe 我使用下面的代码打开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;
}

And then, I want to close the osk.exe when user press enter. 然后,我想在用户按Enter时关闭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);
}

However, this part of code cannot be used to close osk.exe successful. 但是,这部分代码无法成功关闭osk.exe。

I have tried to catch another window, it can be closed successfully. 我试图抓住另一个窗口,它可以成功关闭。

Does it is an issue on Windows 10? 在Windows 10上是否有问题?

if you looking to manifest of osk.exe you can view here next - uiAccess="true" this is User Interface Privilege Isolation (UIPI) , also read about similar problem UIAccess in Manifest Files 如果您想查看osk.exe清单, osk.exe可以在此处查看以下内容uiAccess="true"这是用户界面特权隔离(UIPI) ,也请阅读清单文件中有关类似问题的UIAccess

because osk.exe have uiAccess="true" in manifest it have Mandatory Label\\High Mandatory Level in token. 因为osk.exe在清单中具有uiAccess="true" ,所以令牌中具有“ Mandatory Label\\High Mandatory Level but your app, if running under UAC not elevated, usual have Medium Mandatory Level . 但您的应用程序(如果未在UAC下运行)通常具有Medium Mandatory Level as result: 结果:

A lower-privilege process cannot: 特权较低的进程不能:

  • Use SendMessage or PostMessage to application windows running with higher rights. 使用SendMessage或PostMessage可以以更高的权限运行应用程序窗口。 These APIs return success but silently drop the window message. 这些API返回成功,但会静默删除窗口消息。

but if your application get High Mandatory Level in token - you can close osk.exe by next code: 但是,如果您的应用程序获得令牌中的“ High Mandatory Level ,则可以通过以下代码关闭osk.exe

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

so you need have <requestedExecutionLevel level='requireAdministrator' uiAccess='false'/> in manifest or somehow run your application as elevated 因此,您需要在清单中具有<requestedExecutionLevel level='requireAdministrator' uiAccess='false'/>或以某种方式以提升的身份运行您的应用程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM