Reputation: 5539
I am trying to run and resize OSK from my winform application but I am getting this error:
The requested operation requires elevation.
I am running visual studio as administrator.
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName = "c:\\windows\\system32\\osk.exe";
process.StartInfo.Arguments = "";
process.StartInfo.WorkingDirectory = "c:\\";
process.Start(); // **ERROR HERE**
process.WaitForInputIdle();
SetWindowPos(process.MainWindowHandle,
this.Handle, // Parent Window
this.Left, // Keypad Position X
this.Top + 20, // Keypad Position Y
panelButtons.Width, // Keypad Width
panelButtons.Height, // Keypad Height
SWP_SHOWWINDOW | SWP_NOZORDER); // Show Window and Place on Top
SetForegroundWindow(process.MainWindowHandle);
However,
System.Diagnostics.Process.Start("osk.exe");
Works just fine but it wont let me resize the keyboard
Upvotes: 3
Views: 2508
Reputation: 3853
process.StartInfo.UseShellExecute = false
will prohibit you from doing what you want to do. osk.exe
is a bit special as only one instance can run at a time. So you must let the os handle the startup (UseShellExecute
must be true).
(...) Works just fine but it wont let me resize the keyboard
Just make sure that process.MainWindowHandle
is not IntPtr.Zero
. Could take a while though, your'e not allowed to ask the process instance with process.WaitForInputIdle()
, probably because the proc is run by the os. You could poll the handle though and then run your code. like this:
System.Diagnostics.Process process = new System.Diagnostics.Process();
// process.StartInfo.UseShellExecute = false;
// process.StartInfo.RedirectStandardOutput = true;
// process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName = "c:\\windows\\system32\\osk.exe";
process.StartInfo.Arguments = "";
process.StartInfo.WorkingDirectory = "c:\\";
process.Start(); // **ERROR WAS HERE**
//process.WaitForInputIdle();
//Wait for handle to become available
while(process.MainWindowHandle == IntPtr.Zero)
Task.Delay(10).Wait();
SetWindowPos(process.MainWindowHandle,
this.Handle, // Parent Window
this.Left, // Keypad Position X
this.Top + 20, // Keypad Position Y
panelButtons.Width, // Keypad Width
panelButtons.Height, // Keypad Height
SWP_SHOWWINDOW | SWP_NOZORDER); // Show Window and Place on Top
SetForegroundWindow(process.MainWindowHandle);
Due note: use of Wait()
(or Thread.Sleep
); should be very limited in WinForms, it renders the ui thread unresponsive. You should probably use Task.Run(async () => ...
here instead, to be able to use await Task.Delay(10)
, but that's a different story and complicates the code slightly.
Upvotes: 3