0

I'm developing a wpf application with Awesomium but in the same time I want to use OSK (windows 10 native on-screen keyboard). I'm getting an error saying "Could not start on screen keyboard" when I simply run Process.Start("osk.exe").

If I remove Prefer-32-bit option, I can run the OSK. But I need Awesomium. How do I handle this?

edit: I've set the UseShellExecute to true but the same result.

        Process osk = new Process();
        osk.StartInfo.UseShellExecute = true;
        osk.StartInfo.FileName = "osk.exe"; //  path;//  "osk";
        osk.StartInfo.WindowStyle = ProcessWindowStyle.Normal;

Prefer 32 Bit is what Awesomium requires

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Kubi
  • 2,139
  • 6
  • 35
  • 62

1 Answers1

3

OSK is a very special process since it can be used to inject input into other applications and is a potential security weakness. One of the consequences of the security measures that the OS puts around it are that it cannot easily be started by a WOW64 process. To deal with this you need to create a simple x64 executable that starts OSK. Then from your x86 process running under WOW64, start that intermediary executable whenever you need to start OSK. Obviously you should only do this from a WOW64 process. If you are running on a 32 bit system, then you must not attempt to start a 64 bit executable.

Note that the answers at this question (Unable to launch onscreen keyboard (osk.exe) from a 32-bit process on Win7 x64) suggest disabling file system redirection while you launch OSK. However, disabling file system redirection, even for a short period of time, is a very dangerous act and I would not recommend doing so. Whilst it might seem messy to spin up another process just so that you can start yet another process, I regard that to be the best solution to your problem.

There are some more interesting observations here: https://stackoverflow.com/a/23128178/505088. Of particular interest is the fact that CreateProcess is blocked from starting OSK. It has to be done by ShellExecuteEx. Or presumably ShellExecute but as regular readers of my posts will know, I shun ShellExecute because of its broken error handling mechanism.

I would say, as an aside, that if you really must execute as an x86 process, then it seems wrong to target AnyCPU. You should target x86.


Update

It's clear from the comments and your question edit that you have not fully understood this answer. I'll try spell it out a little more clearly. You need the following:

  1. A 32 bit process in order to use Awesomium.
  2. A 64 bit process in order to start OSK.

So, that's two processes. Your main process is the 32 bit one. When you need to show the OSK, start the 64 bit process which in turn starts OSK. The 64 bit process can terminate as soon as it has started OSK.

Community
  • 1
  • 1
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • thank you for your answer. Targetting x86 is not solving the osk problem. The fastest solution for my case is to set path to x64 path and in my case it is "C:\Windows\WinSxS\amd64_microsoft-windows-osk_31bf3856ad364e35_10.0.10586.0_none_37426bc50445e4b2\osk.exe". Does this path changes in other OS? edit: It changes – Kubi Jan 20 '16 at 15:37
  • I don't think you understood what I said. You should start OSK from a 64 bit process with `UseShellExecute` set to `true`. That's what I said. Just to repeat the key point to be 100% clear: **"To deal with this you need to create a simple x64 executable that starts OSK."** Perhaps you might read the answer again a little more closely. – David Heffernan Jan 20 '16 at 15:39
  • The final paragraph makes the point that you have a requirement that you must run your code in a 32 bit process in order to use Awesomium. In which case why would you select AnyCPU and entertain the prospect of running in a 64 bit prospect. If your code must execute in a 32 bit process, target x86. If you have code that must run in a 64 bit process, then you need two processes. – David Heffernan Jan 20 '16 at 15:41
  • I have other dependencies which requires 64 bit. I'm confused now. But setting the 64 bit osk path seems like an easy solution for now. – Kubi Jan 20 '16 at 15:47
  • *But setting the 64 bit osk path seems like an easy solution for now.* No, that's no good at all. – David Heffernan Jan 20 '16 at 15:59
  • ok but 1. my main process is not x86 so I currently can not target x86. That's why I put that in the title of the question. 2. "When you need to show the OSK, start the 64 bit process which in turn starts OSK. The 64 bit process can terminate as soon as it has started OSK." I don't know how to do this. Should I give it an argument? and why would not changing the path be good? I'm developing this app for only one person who needs it. I know it's not the best but it currently solves the problem. – Kubi Jan 20 '16 at 16:04
  • I think you are confused. Your main process is running as a 32 bit process. That's what happens when you select prefer 32 bit. That's why Awesomium works. The small 64 bit process is just a simple process that calls `Process.Start` and finishes, having done its job. No need for any arguments, it does one thing only. The link in my question goes to an answer with all the code you need. Hard coding the path is no good if you want your program to run on more than one machine. If you want it to run on exactly one machine, that's fine. – David Heffernan Jan 20 '16 at 16:09
  • I can't debug if I target x86 because of the referenced libraries. I'm not sure what the main logic behind choosing Any CPU and putting a "Prefer 32 Bit" as an option is. But I have to do this. Thank you for the comments. – Kubi Jan 20 '16 at 16:16
  • @Kubi I don't think that makes sense... you can't have an x64 process running 32 bit libraries, so if it happens that you run your AnyCPU code in a x64 machine which doesn't offer the x86 instruction set, Awesomium will complain. So in essence, you are running on x86 when selecting `prefer 32 bit`: if you can debug that, you can debug an x86 target – Jcl Jan 20 '16 at 18:42
  • the app does not only consist of awesomium and x86 code. I have x64 libraries and I can't debug the app if I don't target AnyCPU. Wasn't this obvious ? :( – Kubi Jan 20 '16 at 20:58