Skip to content

Instantly share code, notes, and snippets.

@MewX
Created May 25, 2025 13:15
Show Gist options
  • Select an option

  • Save MewX/177710fe3af1bf876eb61c14920207f8 to your computer and use it in GitHub Desktop.

Select an option

Save MewX/177710fe3af1bf876eb61c14920207f8 to your computer and use it in GitHub Desktop.
Scripts to disable the super super annoying error: "GOG Galaxy - Error" Connection to Communication Service was lost and could not be restored! GOG Galaxy will now shutdown.
Get-Process | Where-Object {$_.MainWindowTitle -ne ""} | Select-Object MainWindowTitle, Id, MainWindowHandle
pause
Get-Process | Select-Object Name, Id, MainWindowTitle | Format-Table -AutoSize
pause
# --- Configuration ---
#$MessageBoxTitle = "GOG Galaxy -错误" # If in simplified Chinese.
$MessageBoxTitle = "GOG Galaxy - Error" # IMPORTANT: MODIFY THIS with the exact error window title that matches your language.
$TargetFocusWindowTitle = " GOG Galaxy 2.0.83 " # Title of the main window. Note that the leading and traling spaces are super important!!!
$CheckIntervalSeconds = 2 # How often to check
# --- P/Invoke Signatures for Windows API functions ---
$ApiDefinitions = @"
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool IsIconic(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern bool AllowSetForegroundWindow(int dwProcessId);
[DllImport("user32.dll", SetLastError = true)]
public static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);
"@
# Add the API functions to the current session
Add-Type -MemberDefinition $ApiDefinitions -Name "User32" -Namespace "Win32" -PassThru | Out-Null
# Define the P/Invoke signature for EnableWindow
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class User32 {
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnableWindow(IntPtr hWnd, bool bEnable);
}
"@
# --- Constants for ShowWindow ---
$SW_HIDE = 0
$SW_SHOWNORMAL = 1
$SW_RESTORE = 9
# --- Main Loop ---
Write-Host "Starting script."
Write-Host " - Will look for message box: '$MessageBoxTitle'"
Write-Host " - If found, it will be HIDDEN FIRST."
Write-Host " - Then, script will attempt to focus window: '$TargetFocusWindowTitle'"
Write-Host "Press Ctrl+C to stop the script."
while ($true) {
try {
# 1. Check for the message box
$messageBoxProcess = Get-Process | Where-Object { $_.MainWindowTitle -eq $MessageBoxTitle -and $_.MainWindowHandle -ne [System.IntPtr]::Zero } | Select-Object -First 1
if ($messageBoxProcess) {
$hwndMessageBox = $messageBoxProcess.MainWindowHandle
Write-Host "$(Get-Date): Message box '$MessageBoxTitle' found (Handle: $hwndMessageBox)."
# 2. Hide the message box FIRST
Write-Host "$(Get-Date): Attempting to hide message box '$MessageBoxTitle'..."
$hideResult = [Win32.User32]::ShowWindow($hwndMessageBox, $SW_HIDE)
if ($hideResult -or ([Win32.User32]::IsWindowVisible($hwndMessageBox) -eq $false)) { # ShowWindow returns $false if already hidden
Write-Host "$(Get-Date): Message box '$MessageBoxTitle' hidden or was already hidden."
} else {
Write-Warning "$(Get-Date): Could not verify message box '$MessageBoxTitle' was hidden."
}
Start-Sleep -Seconds 1 # Brief pause after hiding
# 3. Find the target window to focus ("GOG")
$gogProcess = Get-Process | Where-Object { $_.MainWindowTitle -eq $TargetFocusWindowTitle -and $_.MainWindowHandle -ne [System.IntPtr]::Zero } | Select-Object -First 1
if ($gogProcess) {
$hwndGOG = $gogProcess.MainWindowHandle
Write-Host "$(Get-Date): Target window '$TargetFocusWindowTitle' found (Handle: $hwndGOG)."
# 4. Attempt to set focus to the "GOG" window
try {
$result = [User32]::EnableWindow($hwndGOG, $true)
} catch {
Write-Warning "$(Get-Date): Error during focus attempt on '$TargetFocusWindowTitle': $($_.Exception.Message)"
}
} else {
Write-Host "$(Get-Date): Target window '$TargetFocusWindowTitle' not found. Cannot shift focus after hiding message box."
}
# Add a longer delay here as an action (hide+focus attempt) has been performed.
Start-Sleep -Seconds ($CheckIntervalSeconds)
} else {
# This part can be very verbose if the message box isn't present.
# Write-Host "$(Get-Date): Message box '$MessageBoxTitle' not found."
}
}
catch {
Write-Warning "$(Get-Date): An error occurred in the main loop: $($_.Exception.Message)"
}
# Wait before checking again if no message box was found in this iteration.
# If a message box WAS found and processed, a longer sleep is already included above.
if (-not $messageBoxProcess) {
Start-Sleep -Seconds $CheckIntervalSeconds
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment