Fix null window handle to prevent GetWindowRect error (#3279)

This commit is contained in:
Nilesh Mukherjee 2025-04-02 20:49:55 +05:30
parent cdd601d7da
commit fbd2423d19

View File

@ -351,46 +351,53 @@ Add-Type @"
"@ "@
} }
foreach ($proc in (Get-Process).where{ $_.MainWindowTitle -and $_.MainWindowTitle -like "*titus*" }) { # Initialize window handle with a default value
$windowHandle = [System.IntPtr]::Zero
foreach ($proc in (Get-Process).where{ $_.MainWindowTitle -and $_.MainWindowTitle -like "*titus*" }) {
# Check if the process's MainWindowHandle is valid # Check if the process's MainWindowHandle is valid
if ($proc.MainWindowHandle -ne [System.IntPtr]::Zero) { if ($proc.MainWindowHandle -ne [System.IntPtr]::Zero) {
Write-Debug "MainWindowHandle: $($proc.Id) $($proc.MainWindowTitle) $($proc.MainWindowHandle)" Write-Debug "MainWindowHandle: $($proc.Id) $($proc.MainWindowTitle) $($proc.MainWindowHandle)"
$windowHandle = $proc.MainWindowHandle $windowHandle = $proc.MainWindowHandle
} else { } else {
Write-Warning "Process found, but no MainWindowHandle: $($proc.Id) $($proc.MainWindowTitle)" Write-Warning "Process found, but no MainWindowHandle: $($proc.Id) $($proc.MainWindowTitle)"
} }
} }
$rect = New-Object RECT $rect = New-Object RECT
[Window]::GetWindowRect($windowHandle, [ref]$rect) # Call GetWindowRect only if a valid window handle is present
$width = $rect.Right - $rect.Left if ($windowHandle -ne [System.IntPtr]::Zero) {
$height = $rect.Bottom - $rect.Top [Window]::GetWindowRect($windowHandle, [ref]$rect)
$width = $rect.Right - $rect.Left
$height = $rect.Bottom - $rect.Top
Write-Debug "UpperLeft:$($rect.Left),$($rect.Top) LowerBottom:$($rect.Right),$($rect.Bottom). Width:$($width) Height:$($height)" Write-Debug "UpperLeft:$($rect.Left),$($rect.Top) LowerBottom:$($rect.Right),$($rect.Bottom). Width:$($width) Height:$($height)"
# Load the Windows Forms assembly # Load the Windows Forms assembly
Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Windows.Forms
$primaryScreen = [System.Windows.Forms.Screen]::PrimaryScreen $primaryScreen = [System.Windows.Forms.Screen]::PrimaryScreen
# Check if the primary screen is found # Check if the primary screen is found
if ($primaryScreen) { if ($primaryScreen) {
# Extract screen width and height for the primary monitor # Extract screen width and height for the primary monitor
$screenWidth = $primaryScreen.Bounds.Width $screenWidth = $primaryScreen.Bounds.Width
$screenHeight = $primaryScreen.Bounds.Height $screenHeight = $primaryScreen.Bounds.Height
# Print the screen size # Print the screen size
Write-Debug "Primary Monitor Width: $screenWidth pixels" Write-Debug "Primary Monitor Width: $screenWidth pixels"
Write-Debug "Primary Monitor Height: $screenHeight pixels" Write-Debug "Primary Monitor Height: $screenHeight pixels"
# Compare with the primary monitor size # Compare with the primary monitor size
if ($width -gt $screenWidth -or $height -gt $screenHeight) { if ($width -gt $screenWidth -or $height -gt $screenHeight) {
Write-Debug "The specified width and/or height is greater than the primary monitor size." Write-Debug "The specified width and/or height is greater than the primary monitor size."
[void][Window]::MoveWindow($windowHandle, 0, 0, $screenWidth, $screenHeight, $True) [void][Window]::MoveWindow($windowHandle, 0, 0, $screenWidth, $screenHeight, $True)
} else {
Write-Debug "The specified width and height are within the primary monitor size limits."
}
} else { } else {
Write-Debug "The specified width and height are within the primary monitor size limits." Write-Debug "Unable to retrieve information about the primary monitor."
} }
} else { } else {
Write-Debug "Unable to retrieve information about the primary monitor." Write-Debug "No valid window handle found for window positioning."
} }
Invoke-WPFTab "WPFTab1BT" Invoke-WPFTab "WPFTab1BT"