Skip to content

Commit 16ef456

Browse files
authored
Merge pull request #3 from DarkCoderSc/dev
1.0.2 Beta 3
2 parents 484f357 + 9173ead commit 16ef456

7 files changed

+97
-52
lines changed
Binary file not shown.

PowerRemoteDesktop_Server/PowerRemoteDesktop_Server.psm1

+37-40
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@
5252
Add-Type -Assembly System.Windows.Forms
5353
Add-Type -Assembly System.Drawing
5454
Add-Type -MemberDefinition '[DllImport("gdi32.dll")] public static extern int GetDeviceCaps(IntPtr hdc, int nIndex);' -Name GDI32 -Namespace W;
55-
Add-Type -MemberDefinition '[DllImport("User32.dll")] public static extern int GetDC(IntPtr hWnd);[DllImport("User32.dll")] public static extern int ReleaseDC(IntPtr hwnd, int hdc);' -Name User32 -Namespace W;
55+
Add-Type -MemberDefinition '[DllImport("User32.dll")] public static extern int GetDC(IntPtr hWnd);[DllImport("User32.dll")] public static extern int ReleaseDC(IntPtr hwnd, int hdc);[DllImport("User32.dll")] public static extern bool SetProcessDPIAware();' -Name User32 -Namespace W;
5656

57-
$global:PowerRemoteDesktopVersion = "1.0.beta.2"
57+
$global:PowerRemoteDesktopVersion = "1.0.beta.3"
5858

5959
enum TransportMode {
6060
Raw = 1
@@ -501,6 +501,24 @@ function Resolve-AuthenticationChallenge
501501
return $solution
502502
}
503503

504+
function Get-ResolutionScaleFactor
505+
{
506+
<#
507+
.SYNOPSIS
508+
Return current screen scale factor
509+
#>
510+
511+
$hdc = [W.User32]::GetDC(0)
512+
try
513+
{
514+
return [W.GDI32]::GetDeviceCaps($hdc, 117) / [W.GDI32]::GetDeviceCaps($hdc, 10)
515+
}
516+
finally
517+
{
518+
[W.User32]::ReleaseDC(0, $hdc) | Out-Null
519+
}
520+
}
521+
504522
function Get-LocalMachineInformation
505523
{
506524
<#
@@ -521,10 +539,10 @@ function Get-LocalMachineInformation
521539
WindowsVersion = [Environment]::OSVersion.VersionString
522540

523541
ScreenInformation = New-Object -TypeName PSCustomObject -Property @{
524-
Width = $screenBounds.Width
542+
Width = $screenBounds.Width
525543
Height = $screenBounds.Height
526-
X = $screenBounds.X
527-
Y = $screenBounds.Y
544+
X = $screenBounds.X
545+
Y = $screenBounds.Y
528546
}
529547
}
530548
}
@@ -1018,25 +1036,7 @@ $global:DesktopStreamScriptBlock = {
10181036
.PARAMETER syncHash.Param.Client
10191037
A ClientIO Object containing an active connection. This is where, desktop updates will be
10201038
sent over network.
1021-
#>
1022-
1023-
function Get-ResolutionScaleFactor
1024-
{
1025-
<#
1026-
.SYNOPSIS
1027-
Return the scale factor of target screen to capture.
1028-
#>
1029-
1030-
$hdc = [W.User32]::GetDC(0)
1031-
try
1032-
{
1033-
return [W.GDI32]::GetDeviceCaps($hdc, 117) / [W.GDI32]::GetDeviceCaps($hdc, 10)
1034-
}
1035-
finally
1036-
{
1037-
[W.User32]::ReleaseDC(0, $hdc) | Out-Null
1038-
}
1039-
}
1039+
#>
10401040

10411041
function Get-DesktopImage {
10421042
<#
@@ -1048,25 +1048,19 @@ $global:DesktopStreamScriptBlock = {
10481048
At this time, PowerRemoteDesktop only supports PrimaryScreen.
10491049
Even if multi-screen capture is a very easy feature to implement, It will probably be present
10501050
in final version 1.0
1051-
1052-
.PARAMETER ScaleFactor
1053-
Define target monitor scale factor to adjust bounds.
10541051
#>
1055-
param (
1056-
[int] $ScaleFactor = 1
1057-
)
10581052
try
10591053
{
10601054
$primaryDesktop = [System.Windows.Forms.Screen]::PrimaryScreen
10611055

10621056
$size = New-Object System.Drawing.Size(
1063-
($primaryDesktop.Bounds.Size.Width * $ScaleFactor),
1064-
($primaryDesktop.Bounds.Size.Height * $ScaleFactor)
1057+
$primaryDesktop.Bounds.Size.Width,
1058+
$primaryDesktop.Bounds.Size.Height
10651059
)
10661060

10671061
$location = New-Object System.Drawing.Point(
1068-
($primaryDesktop.Bounds.Location.X * $ScaleFactor),
1069-
($primaryDesktop.Bounds.Location.Y * $ScaleFactor)
1062+
$primaryDesktop.Bounds.Location.X,
1063+
$primaryDesktop.Bounds.Location.Y
10701064
)
10711065

10721066
$bitmap = New-Object System.Drawing.Bitmap($size.Width, $size.Height)
@@ -1102,15 +1096,13 @@ $global:DesktopStreamScriptBlock = {
11021096
$encoderParameters = New-Object System.Drawing.Imaging.EncoderParameters(1)
11031097
$encoderParameters.Param[0] = New-Object System.Drawing.Imaging.EncoderParameter([System.Drawing.Imaging.Encoder]::Quality, $imageQuality)
11041098

1105-
$scaleFactor = Get-ResolutionScaleFactor
1106-
1107-
$packetSize = 4096
1099+
$packetSize = 4096
11081100

11091101
while ($true)
11101102
{
11111103
try
11121104
{
1113-
$desktopImage = Get-DesktopImage -ScaleFactor 1
1105+
$desktopImage = Get-DesktopImage
11141106

11151107
$imageStream = New-Object System.IO.MemoryStream
11161108

@@ -1534,6 +1526,7 @@ function Invoke-RemoteDesktopServer
15341526
[switch] $DisableVerbosity
15351527
)
15361528

1529+
15371530
[System.Collections.Generic.List[PSCustomObject]]$runspaces = @()
15381531

15391532
$oldErrorActionPreference = $ErrorActionPreference
@@ -1553,6 +1546,11 @@ function Invoke-RemoteDesktopServer
15531546

15541547
Write-Banner
15551548

1549+
if (Get-ResolutionScaleFactor -ne 1)
1550+
{
1551+
[W.User32]::SetProcessDPIAware()
1552+
}
1553+
15561554
if (-not (Test-Administrator) -and -not $CertificateFile -and -not $EncodedCertificate)
15571555
{
15581556
throw "Insuficient Privilege`r`n`
@@ -1638,8 +1636,7 @@ function Invoke-RemoteDesktopServer
16381636
$clientControl = $server.PullClient(10 * 1000);
16391637

16401638
# Create Runspace #1 for Desktop Streaming.
1641-
$param = New-Object -TypeName PSCustomObject -Property @{
1642-
TransportMode = $TransportMode
1639+
$param = New-Object -TypeName PSCustomObject -Property @{
16431640
Client = $clientDesktop
16441641
}
16451642

Binary file not shown.

PowerRemoteDesktop_Viewer/PowerRemoteDesktop_Viewer.psm1

+53-8
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@
5050
-------------------------------------------------------------------------------#>
5151

5252
Add-Type -Assembly System.Windows.Forms
53+
Add-Type -MemberDefinition '[DllImport("gdi32.dll")] public static extern int GetDeviceCaps(IntPtr hdc, int nIndex);' -Name GDI32 -Namespace W;
54+
Add-Type -MemberDefinition '[DllImport("User32.dll")] public static extern int GetDC(IntPtr hWnd);[DllImport("User32.dll")] public static extern int ReleaseDC(IntPtr hwnd, int hdc);[DllImport("User32.dll")] public static extern bool SetProcessDPIAware();' -Name User32 -Namespace W;
5355

54-
$global:PowerRemoteDesktopVersion = "1.0.beta.2"
56+
$global:PowerRemoteDesktopVersion = "1.0.beta.3"
5557

5658
function Write-Banner
5759
{
@@ -80,6 +82,24 @@ function Write-Banner
8082
Write-Host ""
8183
}
8284

85+
function Get-ResolutionScaleFactor
86+
{
87+
<#
88+
.SYNOPSIS
89+
Return current screen scale factor
90+
#>
91+
92+
$hdc = [W.User32]::GetDC(0)
93+
try
94+
{
95+
return [W.GDI32]::GetDeviceCaps($hdc, 117) / [W.GDI32]::GetDeviceCaps($hdc, 10)
96+
}
97+
finally
98+
{
99+
[W.User32]::ReleaseDC(0, $hdc) | Out-Null
100+
}
101+
}
102+
83103
function Get-SHA512FromString
84104
{
85105
<#
@@ -860,6 +880,11 @@ function Invoke-RemoteDesktopViewer
860880
$VerbosePreference = "continue"
861881

862882
Write-Banner
883+
884+
if (Get-ResolutionScaleFactor -ne 1)
885+
{
886+
[W.User32]::SetProcessDPIAware()
887+
}
863888

864889
Write-Verbose "Server address: ""${ServerAddress}:${ServerPort}"""
865890

@@ -885,9 +910,13 @@ function Invoke-RemoteDesktopViewer
885910
$screenRect = $virtualDesktopForm.Form.RectangleToScreen($virtualDesktopForm.Form.ClientRectangle)
886911
$captionHeight = $screenRect.Top - $virtualDesktopForm.Form.Top
887912

913+
$localScreenWidth = $locationResolutionInformation.WorkingArea.Width
914+
$localScreenHeight = $locationResolutionInformation.WorkingArea.Height
915+
$localScreenHeight -= $captionHeight
916+
888917
$requireResize = (
889-
($locationResolutionInformation.WorkingArea.Width -le $session.SessionInformation.ScreenInformation.Width) -or
890-
(($locationResolutionInformation.WorkingArea.Height - $captionHeight) -le $session.SessionInformation.ScreenInformation.Height)
918+
($localScreenWidth -le $session.SessionInformation.ScreenInformation.Width) -or
919+
($localScreenHeight -le $session.SessionInformation.ScreenInformation.Height)
891920
)
892921

893922
$virtualDesktopWidth = 0
@@ -897,11 +926,27 @@ function Invoke-RemoteDesktopViewer
897926

898927
if ($requireResize)
899928
{
900-
$virtualDesktopWidth = [math]::Round(($session.SessionInformation.ScreenInformation.Width * $resizeRatio) / 100)
901-
$virtualDesktopHeight = [math]::Round(($session.SessionInformation.ScreenInformation.Height * $resizeRatio) / 100)
929+
$adjustVertically = $localScreenWidth -gt $localScreenHeight
930+
931+
if ($adjustVertically)
932+
{
933+
$virtualDesktopWidth = [math]::Round(($localScreenWidth * $resizeRatio) / 100)
934+
935+
$remoteResizedRatio = [math]::Round(($virtualDesktopWidth * 100) / $session.SessionInformation.ScreenInformation.Width)
936+
937+
$virtualDesktopHeight = [math]::Round(($session.SessionInformation.ScreenInformation.Height * $remoteResizedRatio) / 100)
938+
}
939+
else
940+
{
941+
$virtualDesktopHeight = [math]::Round(($localScreenHeight * $resizeRatio) / 100)
942+
943+
$remoteResizedRatio = [math]::Round(($virtualDesktopHeight * 100) / $session.SessionInformation.ScreenInformation.Height)
944+
945+
$virtualDesktopWidth = [math]::Round(($session.SessionInformation.ScreenInformation.Width * $remoteResizedRatio) / 100)
946+
}
902947
}
903948
else
904-
{
949+
{
905950
$virtualDesktopWidth = $session.SessionInformation.ScreenInformation.Width
906951
$virtualDesktopHeight = $session.SessionInformation.ScreenInformation.Height
907952
}
@@ -911,8 +956,8 @@ function Invoke-RemoteDesktopViewer
911956

912957
# Center Virtual Desktop Form
913958
$virtualDesktopForm.Form.Location = [System.Drawing.Point]::new(
914-
(($locationResolutionInformation.WorkingArea.Width - $virtualDesktopForm.Form.Width) / 2),
915-
(($locationResolutionInformation.WorkingArea.Height - $virtualDesktopForm.Form.Height) / 2)
959+
(($localScreenWidth - $virtualDesktopForm.Form.Width) / 2),
960+
(($localScreenHeight - $virtualDesktopForm.Form.Height) / 2)
916961
)
917962

918963
# WinForms Events (If enabled, I recommend to disable control when testing on local machine to avoid funny things)

README.md

+7-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# PowerRemoteDesktop
66

7-
<img src="Screenshot 2022-01-07 at 16.43.53.png" width="100%"/>
7+
<img src="demo.png" width="100%"/>
88

99
*Power Remote Desktop* is a fully functional Remote Desktop Application entirely coded in PowerShell.
1010

@@ -19,7 +19,7 @@ Tested on:
1919

2020
## Features
2121

22-
* Captures Remote Desktop Image with support of HDPI (Beta).
22+
* Captures Remote Desktop Image with support of HDPI.
2323
* Supports Mouse Click (Left, Right, Middle), Mouse Moves and Mouse Wheel.
2424
* Supports Keystrokes Simulation (Sending remote key strokes) and few useful shortcuts.
2525
* Traffic is encrypted by default using TLSv1.2 and optionnally using TLSv1.3 (TLS 1.3 might not be possible on older systems).
@@ -220,7 +220,7 @@ Then pass the encoded string to parameter `EncodedCertificate`.
220220

221221
## Changelog
222222

223-
### 11 January 2021
223+
### 11 January 2021 (1.0.1 Beta 2)
224224

225225
* Desktop images are now transported in raw bytes instead of base64 string thus slightly improving performances. Base64 Transport Method is still available through an option but disabled by default.
226226
* Protocol has drastically changed. It is smoother to read and less prone to errors.
@@ -230,6 +230,10 @@ Then pass the encoded string to parameter `EncodedCertificate`.
230230
* Possibility to disable verbose.
231231
* Server & Viewer version synchronization. Same version must be used between the two.
232232

233+
### 12 January 2021 (1.0.2 Beta 3)
234+
235+
* HDPI is completely supported.
236+
233237
### List of ideas and TODO
234238

235239
* 🟢 Do a deep investigation about SecureString and if it applies to current project (to protect password)
@@ -243,7 +247,6 @@ Then pass the encoded string to parameter `EncodedCertificate`.
243247
* 🟠 Server Concurrency.
244248
* 🟠 Listen for local/remote screen resolution update event.
245249
* 🟠 Multiple Monitor Support.
246-
* 🟠 Improve HDPI Scaling / Quality.
247250
* 🔴 Motion Update for Desktop Streaming (Only send and update changing parts of desktop).
248251

249252
🟢 = Easy

Screenshot 2022-01-07 at 16.43.53.png

-4.87 MB
Binary file not shown.

demo.png

5.28 MB
Loading

0 commit comments

Comments
 (0)