From 6b236e156ce97f06aab5fd36efee6b9011af0c3f Mon Sep 17 00:00:00 2001 From: Jared Goodwin Date: Mon, 4 Mar 2019 06:43:38 -0800 Subject: [PATCH] Rebranding. --- Remotely_Library/Win32/Win32Interop.cs | 2 +- Remotely_ScreenCast/Capture/BitBltCapture.cs | 24 ++++--- Remotely_ScreenCast/Capture/ScreenCaster.cs | 47 ++++++------ Remotely_ScreenCast/Program.cs | 72 ++++++++++++------- .../Sockets/MessageHandlers.cs | 11 ++- 5 files changed, 100 insertions(+), 56 deletions(-) diff --git a/Remotely_Library/Win32/Win32Interop.cs b/Remotely_Library/Win32/Win32Interop.cs index 62c4f28a..5bda1cab 100644 --- a/Remotely_Library/Win32/Win32Interop.cs +++ b/Remotely_Library/Win32/Win32Interop.cs @@ -72,7 +72,7 @@ namespace Remotely_Library.Win32 uint dwCreationFlags; if (hiddenWindow) { - dwCreationFlags = NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW | DETACHED_PROCESS; + dwCreationFlags = NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW; } else { diff --git a/Remotely_ScreenCast/Capture/BitBltCapture.cs b/Remotely_ScreenCast/Capture/BitBltCapture.cs index e1dfdfdc..13c3d399 100644 --- a/Remotely_ScreenCast/Capture/BitBltCapture.cs +++ b/Remotely_ScreenCast/Capture/BitBltCapture.cs @@ -76,7 +76,6 @@ namespace Remotely_ScreenCast.Capture PreviousFrame = new Bitmap(CurrentScreenBounds.Width, CurrentScreenBounds.Height, PixelFormat.Format32bppArgb); graphic = Graphics.FromImage(CurrentFrame); desktopName = Win32Interop.GetCurrentDesktop(); - //hWnd = User32.GetDesktopWindow(); } public void Capture() @@ -103,29 +102,34 @@ namespace Remotely_ScreenCast.Capture { lock (ScreenLock) { + PreviousFrame = (Bitmap)CurrentFrame.Clone(); //hWnd = User32.GetDesktopWindow(); //hDC = User32.GetWindowDC(hWnd); //graphic = Graphics.FromImage(CurrentFrame); //graphDC = graphic.GetHdc(); //var copyResult = GDI32.BitBlt(graphDC, 0, 0, CurrentScreenBounds.Width, CurrentScreenBounds.Height, hDC, CurrentScreenBounds.Left, CurrentScreenBounds.Top, GDI32.TernaryRasterOperations.SRCCOPY); - PreviousFrame = (Bitmap)CurrentFrame.Clone(); + //if (!copyResult) + //{ + // Logger.Write($"BitBlt failed on {currentDesktop}."); + //} graphic.CopyFromScreen(CurrentScreenBounds.Left, CurrentScreenBounds.Top, 0, 0, new Size(CurrentScreenBounds.Width, CurrentScreenBounds.Height)); } } catch (Exception ex) { Logger.Write(ex); + Init(); } finally { - //if (graphDC != IntPtr.Zero) - //{ - // graphic.ReleaseHdc(graphDC); - //} - //if (hDC != IntPtr.Zero) - //{ - // User32.ReleaseDC(hWnd, hDC); - //} + if (graphDC != IntPtr.Zero) + { + graphic.ReleaseHdc(graphDC); + } + if (hDC != IntPtr.Zero) + { + User32.ReleaseDC(hWnd, hDC); + } } } } diff --git a/Remotely_ScreenCast/Capture/ScreenCaster.cs b/Remotely_ScreenCast/Capture/ScreenCaster.cs index ea565e94..fb3c8e93 100644 --- a/Remotely_ScreenCast/Capture/ScreenCaster.cs +++ b/Remotely_ScreenCast/Capture/ScreenCaster.cs @@ -14,34 +14,41 @@ namespace Remotely_ScreenCast.Capture { public class ScreenCaster { - private static object CaptureLock { get; } = new object(); public static async void BeginScreenCasting(HubConnection hubConnection, string viewerID, string requesterName, OutgoingMessages outgoingMessages) { + var inputDesktop = Win32Interop.OpenInputDesktop(); + var result = User32.SetThreadDesktop(inputDesktop); + User32.CloseDesktop(inputDesktop); + Logger.Write($"Set thread desktop begin screencasting to {Win32Interop.GetCurrentDesktop()}: {result}"); + + ICapturer capturer; CaptureMode captureMode; - try - { - if (Program.Viewers.Count == 0) - { - capturer = new DXCapture(); - captureMode = CaptureMode.DirectX; - } - else - { - capturer = new BitBltCapture(); - captureMode = CaptureMode.BitBtl; - } - } - catch (Exception ex) - { - Logger.Write(ex); - capturer = new BitBltCapture(); - captureMode = CaptureMode.BitBtl; - } + capturer = new BitBltCapture(); + captureMode = CaptureMode.BitBtl; + //try + //{ + // if (Program.Viewers.Count == 0) + // { + // capturer = new DXCapture(); + // captureMode = CaptureMode.DirectX; + // } + // else + // { + // capturer = new BitBltCapture(); + // captureMode = CaptureMode.BitBtl; + // } + //} + //catch (Exception ex) + //{ + // Logger.Write(ex); + // capturer = new BitBltCapture(); + // captureMode = CaptureMode.BitBtl; + //} Logger.Write($"Starting screen cast. Requester: {requesterName}. Viewer ID: {viewerID}. Capture Mode: {captureMode.ToString()}. Desktop: {Win32Interop.GetCurrentDesktop()}"); diff --git a/Remotely_ScreenCast/Program.cs b/Remotely_ScreenCast/Program.cs index 0386036d..d680d325 100644 --- a/Remotely_ScreenCast/Program.cs +++ b/Remotely_ScreenCast/Program.cs @@ -31,34 +31,58 @@ namespace Remotely_ScreenCast public static HubConnection Connection { get; private set; } public static OutgoingMessages OutgoingMessages { get; private set; } public static ConcurrentDictionary Viewers { get; } = new ConcurrentDictionary(); + private static string CurrentDesktopName { get; set; } static void Main(string[] args) { - AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; - var argDict = ProcessArgs(args); - Mode = argDict["mode"]; - RequesterID = argDict["requester"]; - Host = argDict["host"]; - - Connection = new HubConnectionBuilder() - .WithUrl($"{Host}/RCDeviceHub") - .Build(); - - Connection.StartAsync().Wait(); - - OutgoingMessages = new OutgoingMessages(Connection); - - MessageHandlers.ApplyConnectionHandlers(Connection, OutgoingMessages); - - CursorIconWatcher.Current.OnChange += CursorIconWatcher_OnChange; - - OutgoingMessages.NotifyRequesterUnattendedReady(RequesterID).Wait(); - - StartWaitForViewerTimer(); - - while (true) + try { - Console.Read(); + AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; + + var inputDesktop = Win32Interop.OpenInputDesktop(); + var success = User32.SetThreadDesktop(inputDesktop); + User32.CloseDesktop(inputDesktop); + CurrentDesktopName = Win32Interop.GetCurrentDesktop(); + Logger.Write($"Set thread desktop on launch to {CurrentDesktopName}: {success}"); + + var argDict = ProcessArgs(args); + Mode = argDict["mode"]; + RequesterID = argDict["requester"]; + Host = argDict["host"]; + + Connection = new HubConnectionBuilder() + .WithUrl($"{Host}/RCDeviceHub") + .Build(); + + Connection.StartAsync().Wait(); + + OutgoingMessages = new OutgoingMessages(Connection); + + MessageHandlers.ApplyConnectionHandlers(Connection, OutgoingMessages); + + CursorIconWatcher.Current.OnChange += CursorIconWatcher_OnChange; + + OutgoingMessages.NotifyRequesterUnattendedReady(RequesterID).Wait(); + + StartWaitForViewerTimer(); + + while (true) + { + var desktopName = Win32Interop.GetCurrentDesktop(); + if (desktopName != CurrentDesktopName) + { + CurrentDesktopName = desktopName; + inputDesktop = Win32Interop.OpenInputDesktop(); + success = User32.SetThreadDesktop(inputDesktop); + User32.CloseDesktop(inputDesktop); + Logger.Write($"Set thread desktop on main thread to {CurrentDesktopName}: {success}"); + } + System.Threading.Thread.Sleep(100); + } + } + catch (Exception ex) + { + Logger.Write(ex); } } diff --git a/Remotely_ScreenCast/Sockets/MessageHandlers.cs b/Remotely_ScreenCast/Sockets/MessageHandlers.cs index 36e16e11..dd2afa7b 100644 --- a/Remotely_ScreenCast/Sockets/MessageHandlers.cs +++ b/Remotely_ScreenCast/Sockets/MessageHandlers.cs @@ -28,7 +28,14 @@ namespace Remotely_ScreenCast.Sockets hubConnection.On("GetScreenCast", (string viewerID, string requesterName) => { - ScreenCaster.BeginScreenCasting(hubConnection, viewerID, requesterName, outgoingMessages); + try + { + ScreenCaster.BeginScreenCasting(hubConnection, viewerID, requesterName, outgoingMessages); + } + catch (Exception ex) + { + Logger.Write(ex); + } }); hubConnection.On("KeyDown", (int keyCode, string viewerID) => @@ -36,6 +43,7 @@ namespace Remotely_ScreenCast.Sockets if (Program.Viewers.TryGetValue(viewerID, out var viewer) && viewer.HasControl) { Win32Interop.SendKeyDown((User32.VirtualKeyShort)keyCode); + //User32.SendKeyDown((User32.VirtualKeyShort)KeyInterop.VirtualKeyFromKey((Key)Enum.Parse(typeof(Key), key))); } }); @@ -44,6 +52,7 @@ namespace Remotely_ScreenCast.Sockets if (Program.Viewers.TryGetValue(viewerID, out var viewer) && viewer.HasControl) { Win32Interop.SendKeyUp((User32.VirtualKeyShort)keyCode); + //User32.SendKeyDown((User32.VirtualKeyShort)KeyInterop.VirtualKeyFromKey((Key)Enum.Parse(typeof(Key), key))); } });