From aff5598c9582d21c30bfb8e0c425fc888a900fe3 Mon Sep 17 00:00:00 2001 From: Jared Goodwin Date: Wed, 29 Jan 2020 19:07:42 -0800 Subject: [PATCH] Fixes for elevating WPF app. --- Desktop.Win/App.xaml.cs | 2 +- Desktop.Win/ViewModels/MainWindowViewModel.cs | 4 +- ScreenCast.Win/Services/WinInput.cs | 49 ++++++++++--------- 3 files changed, 29 insertions(+), 26 deletions(-) diff --git a/Desktop.Win/App.xaml.cs b/Desktop.Win/App.xaml.cs index ea565a60..3cccf8ba 100644 --- a/Desktop.Win/App.xaml.cs +++ b/Desktop.Win/App.xaml.cs @@ -47,7 +47,7 @@ namespace Remotely.Desktop.Win { if (Environment.GetCommandLineArgs().Contains("-elevate")) { - var commandLine = Win32Interop.GetCommandLine().Replace(" -elevate", ""); + var commandLine = Win32Interop.GetCommandLine().Replace(" -elevate", "").Replace("\"", ""); Logger.Write($"Elevating process {commandLine}."); var result = Win32Interop.OpenInteractiveProcess( diff --git a/Desktop.Win/ViewModels/MainWindowViewModel.cs b/Desktop.Win/ViewModels/MainWindowViewModel.cs index 6216691b..461ba2dd 100644 --- a/Desktop.Win/ViewModels/MainWindowViewModel.cs +++ b/Desktop.Win/ViewModels/MainWindowViewModel.cs @@ -132,7 +132,7 @@ namespace Remotely.Desktop.Win.ViewModels WindowStyle = ProcessWindowStyle.Hidden, CreateNoWindow = true }; - var commandLine = Win32Interop.GetCommandLine().Replace(" -elevate", ""); + var commandLine = Win32Interop.GetCommandLine().Replace(" -elevate", "").Replace("\"", ""); Logger.Write($"Creating temporary service with command line {commandLine}."); psi.Arguments = $"/c sc create Remotely_Temp binPath=\"{commandLine} -elevate\""; Process.Start(psi).WaitForExit(); @@ -140,7 +140,7 @@ namespace Remotely.Desktop.Win.ViewModels Process.Start(psi).WaitForExit(); psi.Arguments = "/c sc delete Remotely_Temp"; Process.Start(psi).WaitForExit(); - Environment.Exit(0); + App.Current.Shutdown(); } catch { } }, (param) => diff --git a/ScreenCast.Win/Services/WinInput.cs b/ScreenCast.Win/Services/WinInput.cs index 7e82c262..5dbb9eca 100644 --- a/ScreenCast.Win/Services/WinInput.cs +++ b/ScreenCast.Win/Services/WinInput.cs @@ -26,9 +26,8 @@ namespace Remotely.ScreenCast.Win.Services public void SendKeyDown(string key, Viewer viewer) { - SendOnStaThread(() => + TryOnInputDesktop(() => { - Win32Interop.SwitchToInputDesktop(); var keyCode = ConvertJavaScriptKeyToVirtualKey(key); var union = new InputUnion() { @@ -46,11 +45,12 @@ namespace Remotely.ScreenCast.Win.Services } + + public void SendKeyUp(string key, Viewer viewer) { - SendOnStaThread(() => + TryOnInputDesktop(() => { - Win32Interop.SwitchToInputDesktop(); var keyCode = ConvertJavaScriptKeyToVirtualKey(key); var union = new InputUnion() { @@ -70,9 +70,8 @@ namespace Remotely.ScreenCast.Win.Services public void SendLeftMouseDown(double percentX, double percentY, Viewer viewer) { - SendOnStaThread(() => + TryOnInputDesktop(() => { - Win32Interop.SwitchToInputDesktop(); var xyPercent = GetAbsolutePercentFromRelativePercent(percentX, percentY, viewer.Capturer); // Coordinates must be normalized. The bottom-right coordinate is mapped to 65535. var normalizedX = xyPercent.Item1 * 65535D; @@ -85,9 +84,8 @@ namespace Remotely.ScreenCast.Win.Services public void SendLeftMouseUp(double percentX, double percentY, Viewer viewer) { - SendOnStaThread(() => + TryOnInputDesktop(() => { - Win32Interop.SwitchToInputDesktop(); var xyPercent = GetAbsolutePercentFromRelativePercent(percentX, percentY, viewer.Capturer); // Coordinates must be normalized. The bottom-right coordinate is mapped to 65535. var normalizedX = xyPercent.Item1 * 65535D; @@ -100,9 +98,8 @@ namespace Remotely.ScreenCast.Win.Services public void SendMouseMove(double percentX, double percentY, Viewer viewer) { - SendOnStaThread(() => + TryOnInputDesktop(() => { - Win32Interop.SwitchToInputDesktop(); var xyPercent = GetAbsolutePercentFromRelativePercent(percentX, percentY, viewer.Capturer); // Coordinates must be normalized. The bottom-right coordinate is mapped to 65535. var normalizedX = xyPercent.Item1 * 65535D; @@ -115,9 +112,8 @@ namespace Remotely.ScreenCast.Win.Services public void SendMouseWheel(int deltaY, Viewer viewer) { - SendOnStaThread(() => + TryOnInputDesktop(() => { - Win32Interop.SwitchToInputDesktop(); if (deltaY < 0) { deltaY = -120; @@ -134,9 +130,8 @@ namespace Remotely.ScreenCast.Win.Services public void SendRightMouseDown(double percentX, double percentY, Viewer viewer) { - SendOnStaThread(() => + TryOnInputDesktop(() => { - Win32Interop.SwitchToInputDesktop(); var xyPercent = GetAbsolutePercentFromRelativePercent(percentX, percentY, viewer.Capturer); // Coordinates must be normalized. The bottom-right coordinate is mapped to 65535. var normalizedX = xyPercent.Item1 * 65535D; @@ -149,9 +144,8 @@ namespace Remotely.ScreenCast.Win.Services public void SendRightMouseUp(double percentX, double percentY, Viewer viewer) { - SendOnStaThread(() => + TryOnInputDesktop(() => { - Win32Interop.SwitchToInputDesktop(); var xyPercent = GetAbsolutePercentFromRelativePercent(percentX, percentY, viewer.Capturer); // Coordinates must be normalized. The bottom-right coordinate is mapped to 65535. var normalizedX = xyPercent.Item1 * 65535D; @@ -164,7 +158,7 @@ namespace Remotely.ScreenCast.Win.Services public void SendText(string transferText, Viewer viewer) { - SendOnStaThread(() => + TryOnInputDesktop(() => { SendKeys.SendWait(transferText); }); @@ -292,13 +286,22 @@ namespace Remotely.ScreenCast.Win.Services return keyCode; } - private void SendOnStaThread(Action sendAction) + private void TryOnInputDesktop(Action inputAction) { - var thread = new Thread(() => { - sendAction(); - }); - thread.SetApartmentState(ApartmentState.STA); - thread.Start(); + if (!Win32Interop.SwitchToInputDesktop()) + { + var thread = new Thread(() => + { + Win32Interop.SwitchToInputDesktop(); + inputAction(); + }); + thread.SetApartmentState(ApartmentState.STA); + thread.Start(); + } + else + { + inputAction(); + } } } }