Problem calling Virtual Keyboard - Winforms

Asked

Viewed 254 times

2

I have a button that when clicking the virtual keyboard of windows is called.

Process.Start("osk.exe");

By clicking the button an Exception is triggered:

System.ComponentModel.Win32Exception: 'The system cannot find the file specified'

I’ve searched a lot of places, that’s why I’m asking the question. From now on, thank you.

  • Wouldn’t it be the case that you pass all the way to where this executable is? I believe the application is looking in the folder of Solution, and will not find the executable there.

  • The same happens using @"C: WINDOWS system32 osk.exe".

  • 1

    https://stackoverflow.com/questions/2929255

  • I got it, thank you!

2 answers

2

Try it this way:

ProcessStartInfo processStartInfo = new ProcessStartInfo()
{
    UseShellExecute = true,
    FileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "osk.exe"),
    Verb = "runas"
};

Process.Start(processStartInfo);

When defining the property Verb = "runas" we are "telling" Windows to behave as if the process had been invoked by the command "Run as Administrator".

1


I got it with the following code:

[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);

private const UInt32 WM_SYSCOMMAND = 0x112;
private const UInt32 SC_RESTORE = 0xf120;

[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

private string OnScreenKeyboadApplication = "osk.exe";

private void btnKeyboard_Click(object sender, EventArgs e)
        {
            //Process.Start("osk.exe");

            string processName = System.IO.Path.GetFileNameWithoutExtension(OnScreenKeyboadApplication);

            // Check whether the application is not running 
            var query = from process in Process.GetProcesses()
                        where process.ProcessName == processName
                        select process;

            var keyboardProcess = query.FirstOrDefault();

            // launch it if it doesn't exist
            if (keyboardProcess == null)
            {
                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);
                }

                // osk.exe is in windows/system folder. So we can directky call it without path
                using (Process osk = new Process())
                {
                    osk.StartInfo.FileName = OnScreenKeyboadApplication;
                    osk.Start();
                }

                // Re-enable directory virtualisation if it was disabled.
                if (System.Environment.Is64BitOperatingSystem)
                    if (sucessfullyDisabledWow64Redirect)
                        Wow64RevertWow64FsRedirection(ptr);
            }
            else
            {
                // Bring keyboard to the front if it's already running
                var windowHandle = keyboardProcess.MainWindowHandle;
                SendMessage(windowHandle, WM_SYSCOMMAND, new IntPtr(SC_RESTORE), new IntPtr(0));
            }
        }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.