Unable to launch onscreen keyboard (osk.exe) from a 32-bit process on Win7 x64 Unable to launch onscreen keyboard (osk.exe) from a 32-bit process on Win7 x64 wpf wpf

Unable to launch onscreen keyboard (osk.exe) from a 32-bit process on Win7 x64


A 32 bit application running on a 64 bit operating system should start the 64 bit version of osk.exe. Below you see a code snipped written in C# to start the correct on screen keyboard.

    private static void ShowKeyboard()    {        var path64 = @"C:\Windows\winsxs\amd64_microsoft-windows-osk_31bf3856ad364e35_6.1.7600.16385_none_06b1c513739fb828\osk.exe";        var path32 = @"C:\windows\system32\osk.exe";        var path = (Environment.Is64BitOperatingSystem) ? path64 : path32;        Process.Start(path);    }


I don't have a very solid explanation for the exact error message you are getting. But disabling redirection is going to mess up the .NET framework. By default, Process.Start() P/Invokes the ShellExecuteEx() API function to start the process. This function lives in shell32.dll, a DLL that might have to be loaded if that wasn't previously done. You'll get the wrong one when you disable redirection.

A workaround for that is to set ProcessStartInfo.UseShellExecute to false. You don't need it here.

Clearly, disabling redirection is a risky approach with side-effects you cannot really predict. There are lots of DLLs that get demand-loaded. A very small helper EXE that you compile with Platform Target = Any CPU can solve your problem.


Certain things are going on under the hood that require you to start osk.exe from an MTA thread. The reason seems to be that a call to Wow64DisableWow64FsRedirection only affects the current thread. However, under certain conditions, Process.Start will create the new process from a separate thread, e.g. when UseShellExecute is set to false and also when being called from an STA thread as it seems.

The code below checks the apartment state and then makes sure to start the On-Screen Keyboard from an MTA thread:

using System;using System.Diagnostics;using System.IO;using System.Runtime.InteropServices;using System.Threading;class Program{    [DllImport("kernel32.dll", SetLastError = true)]    private static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr);    [DllImport("kernel32.dll", SetLastError = true)]    public static extern bool Wow64RevertWow64FsRedirection(IntPtr ptr);    [DllImport("user32.dll", CharSet = CharSet.Auto)]    static extern IntPtr SendMessage(IntPtr hWnd,         UInt32 Msg,         IntPtr wParam,         IntPtr lParam);    private const UInt32 WM_SYSCOMMAND = 0x112;    private const UInt32 SC_RESTORE = 0xf120;    private const string OnScreenKeyboardExe = "osk.exe";    [STAThread]    static void Main(string[] args)    {        Process[] p = Process.GetProcessesByName(            Path.GetFileNameWithoutExtension(OnScreenKeyboardExe));        if (p.Length == 0)        {            // we must start osk from an MTA thread            if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA)            {                ThreadStart start = new ThreadStart(StartOsk);                Thread thread = new Thread(start);                thread.SetApartmentState(ApartmentState.MTA);                thread.Start();                thread.Join();            }            else            {                StartOsk();            }        }        else        {            // there might be a race condition if the process terminated             // meanwhile -> proper exception handling should be added            //            SendMessage(p[0].MainWindowHandle,                 WM_SYSCOMMAND, new IntPtr(SC_RESTORE), new IntPtr(0));        }    }    static void StartOsk()    {        IntPtr ptr = new IntPtr(); ;        bool sucessfullyDisabledWow64Redirect = false;        // Disable x64 directory virtualization if we're on x64,        // otherwise keyboard launch will fail.        if (System.Environment.Is64BitOperatingSystem)        {            sucessfullyDisabledWow64Redirect =                 Wow64DisableWow64FsRedirection(ref ptr);        }        ProcessStartInfo psi = new ProcessStartInfo();        psi.FileName = OnScreenKeyboardExe;        // We must use ShellExecute to start osk from the current thread        // with psi.UseShellExecute = false the CreateProcessWithLogon API         // would be used which handles process creation on a separate thread         // where the above call to Wow64DisableWow64FsRedirection would not         // have any effect.        //        psi.UseShellExecute = true;        Process.Start(psi);        // Re-enable directory virtualisation if it was disabled.        if (System.Environment.Is64BitOperatingSystem)            if (sucessfullyDisabledWow64Redirect)                Wow64RevertWow64FsRedirection(ptr);    }}