From 310ffd989563fca814b84aaf2646dbfbb465c9ae Mon Sep 17 00:00:00 2001 From: ram/haidar <49301219+ramhaidar@users.noreply.github.com> Date: Sat, 14 Nov 2026 19:26:18 +0700 Subject: [PATCH] refactor(win/nvfbcwrp): make patch script idempotent and rollback safe - Rewrite Patch.ps1 around a read-only pre-flight plan that refuses missing wrappers, wrong wrapper PE architecture, stale or wrapper backups, and a non-NVIDIA file at the original path before anything is changed. - Use takeown/icacls instead of Set-Acl, record each owner SID and whether an Administrators ACE already existed, and restore both so a temporary unlock is undone on rollback and after a successful move. - Stage the wrapper as a .nvfbcwrp-tmp copy, verify SHA-256 after every copy and move, and revert recorded copy, move and ACL steps in reverse when any target fails. - Add -Unattended and -SkipGenuineCheck switches, quote elevation arguments correctly for Windows PowerShell 5.1, and replace pause/Read-Host with explicit exit codes. - RunPatch.bat confines ExecutionPolicy Bypass to its own process and propagates the exit code; README.md documents the re-run, refusal and manual-restore behavior. - Target: win/nvfbcwrp --- win/README.md | 17 +- win/nvfbcwrp/Patch.ps1 | 646 +++++++++++++++++++++++++++++++------- win/nvfbcwrp/README.md | 2 +- win/nvfbcwrp/RunPatch.bat | 26 +- 4 files changed, 559 insertions(+), 132 deletions(-) diff --git a/win/README.md b/win/README.md index 8f56435..1449b67 100644 --- a/win/README.md +++ b/win/README.md @@ -1,4 +1,5 @@ -# NVENC and NvFBC patches for Windows Nvidia drivers +NVENC and NvFBC patches for Windows Nvidia drivers +================================================== ![GitHub last commit](https://img.shields.io/github/last-commit/keylase/nvidia-patch.svg) ![Latest GeForce version](https://img.shields.io/badge/latest%20GeForce%20version-621.24-brightgreen.svg) ![Latest Quadro version](https://img.shields.io/badge/latest%20Quadro%20version-582.16-blue.svg) @@ -25,7 +26,7 @@ If you like this project, best way to contribute is by sending PRs and fixing do - Windows Server 2012 R2 - Windows Server 2016 - Windows Server 2019 -- NVENC-compatible GPU () +- NVENC-compatible GPU (https://developer.nvidia.com/video-encode-decode-gpu-support-matrix#Encoder) - Nvidia driver. Patch available for versions in [table below](#version-table). ## NVENC Patch Step-by-Step Guide @@ -61,6 +62,7 @@ Credits to [TroubleChute channel](https://www.youtube.com/channel/UCkih2oVTbXPEp ### Windows 10, Windows 11 drivers + | Product series | Version | x64 library patch | x86 library patch | Driver link | |----------------|---------|-------------------|-------------------|-------------| | GeForce | 391.35 | [x64 library patch](https://raw.githubusercontent.com/keylase/nvidia-patch/master/win/win10_x64/391.35/nvencodeapi64.1337) | [x86 library patch](https://raw.githubusercontent.com/keylase/nvidia-patch/master/win/win10_x64/391.35/nvencodeapi.1337) | [Driver link](https://international.download.nvidia.com/Windows/391.35/391.35-desktop-win10-64bit-international-whql.exe) | @@ -315,6 +317,7 @@ Credits to [TroubleChute channel](https://www.youtube.com/channel/UCkih2oVTbXPEp | GeForce | 621.14 DCH | [x64 library patch](https://raw.githubusercontent.com/keylase/nvidia-patch/master/win/win10_x64/621.14/nvencodeapi64.1337) | [x86 library patch](https://raw.githubusercontent.com/keylase/nvidia-patch/master/win/win10_x64/621.14/nvencodeapi.1337) | [Driver link](https://international.download.nvidia.com/Windows/621.14/621.14-desktop-win10-win11-64bit-international-dch-whql.exe) | | GeForce | 621.24 DCH | [x64 library patch](https://raw.githubusercontent.com/keylase/nvidia-patch/master/win/win10_x64/621.24/nvencodeapi64.1337) | [x86 library patch](https://raw.githubusercontent.com/keylase/nvidia-patch/master/win/win10_x64/621.24/nvencodeapi.1337) | [Driver link](https://international.download.nvidia.com/Windows/621.24/621.24-desktop-win10-win11-64bit-international-dch-whql.exe) | + | Product series | Version | x64 library patch | x86 library patch | Driver link | |----------------|---------|-------------------|-------------------|-------------| | Quadro | 416.78 | [x64 library patch](https://raw.githubusercontent.com/keylase/nvidia-patch/master/win/win10_x64/quadro_416.78/nvencodeapi64.1337) | [x86 library patch](https://raw.githubusercontent.com/keylase/nvidia-patch/master/win/win10_x64/quadro_416.78/nvencodeapi.1337) | [Driver link](https://international.download.nvidia.com/Windows/Quadro_Certified/416.78/416.78-quadro-desktop-notebook-win10-64bit-international-whql.exe) | @@ -366,8 +369,10 @@ Credits to [TroubleChute channel](https://www.youtube.com/channel/UCkih2oVTbXPEp | Quadro | 553.35 DCH | [x64 library patch](https://raw.githubusercontent.com/keylase/nvidia-patch/master/win/win10_x64/quadro_553.35/nvencodeapi64.1337) | [x86 library patch](https://raw.githubusercontent.com/keylase/nvidia-patch/master/win/win10_x64/quadro_553.35/nvencodeapi.1337) | [Driver link](https://us.download.nvidia.com/Windows/Quadro_Certified/553.35/553.35-quadro-rtx-desktop-notebook-win10-win11-64bit-international-dch-whql.exe) | | Quadro | 582.16 | [x64 library patch](https://raw.githubusercontent.com/keylase/nvidia-patch/master/win/win10_x64/quadro_582.16/nvencodeapi64.1337) | [x86 library patch](https://raw.githubusercontent.com/keylase/nvidia-patch/master/win/win10_x64/quadro_582.16/nvencodeapi.1337) | [Driver link](https://international.download.nvidia.com/Windows/Quadro_Certified/582.16/582.16-quadro-rtx-desktop-notebook-win10-win11-64bit-international-dch-whql.exe) | + ### Windows 7, Windows 8, Windows 8.1 drivers + | Product series | Version | x64 library patch | x86 library patch | Driver link | |----------------|---------|-------------------|-------------------|-------------| | GeForce | 431.60 | [x64 library patch](https://raw.githubusercontent.com/keylase/nvidia-patch/master/win/win7_x64/431.60/nvencodeapi64.1337) | [x86 library patch](https://raw.githubusercontent.com/keylase/nvidia-patch/master/win/win7_x64/431.60/nvencodeapi.1337) | [Driver link](https://international.download.nvidia.com/Windows/431.60/431.60-desktop-win8-win7-64bit-international-whql.exe) | @@ -437,6 +442,7 @@ Credits to [TroubleChute channel](https://www.youtube.com/channel/UCkih2oVTbXPEp | GeForce | 471.96 | [x64 library patch](https://raw.githubusercontent.com/keylase/nvidia-patch/master/win/win7_x64/471.96/nvencodeapi64.1337) | [x86 library patch](https://raw.githubusercontent.com/keylase/nvidia-patch/master/win/win7_x64/471.96/nvencodeapi.1337) | [Driver link](https://international.download.nvidia.com/Windows/471.96/471.96-desktop-win8-win7-64bit-international-whql.exe) | | GeForce | 472.12 | [x64 library patch](https://raw.githubusercontent.com/keylase/nvidia-patch/master/win/win7_x64/472.12/nvencodeapi64.1337) | [x86 library patch](https://raw.githubusercontent.com/keylase/nvidia-patch/master/win/win7_x64/472.12/nvencodeapi.1337) | [Driver link](https://international.download.nvidia.com/Windows/472.12/472.12-desktop-win8-win7-64bit-international-whql.exe) | + | Product series | Version | x64 library patch | x86 library patch | Driver link | |----------------|---------|-------------------|-------------------|-------------| | Quadro | 431.02 | [x64 library patch](https://raw.githubusercontent.com/keylase/nvidia-patch/master/win/win7_x64/quadro_431.02/nvencodeapi64.1337) | [x86 library patch](https://raw.githubusercontent.com/keylase/nvidia-patch/master/win/win7_x64/quadro_431.02/nvencodeapi.1337) | [Driver link](https://international.download.nvidia.com/Windows/Quadro_Certified/431.02/431.02-quadro-desktop-notebook-win8-win7-64bit-international-whql.exe) | @@ -450,8 +456,10 @@ Credits to [TroubleChute channel](https://www.youtube.com/channel/UCkih2oVTbXPEp | Quadro | 441.28 | [x64 library patch](https://raw.githubusercontent.com/keylase/nvidia-patch/master/win/win7_x64/quadro_441.28/nvencodeapi64.1337) | [x86 library patch](https://raw.githubusercontent.com/keylase/nvidia-patch/master/win/win7_x64/quadro_441.28/nvencodeapi.1337) | [Driver link](https://international.download.nvidia.com/Windows/Quadro_Certified/441.28/441.28-quadro-desktop-notebook-win8-win7-64bit-international-whql.exe) | | Quadro | 441.66 | [x64 library patch](https://raw.githubusercontent.com/keylase/nvidia-patch/master/win/win7_x64/quadro_441.66/nvencodeapi64.1337) | [x86 library patch](https://raw.githubusercontent.com/keylase/nvidia-patch/master/win/win7_x64/quadro_441.66/nvencodeapi.1337) | [Driver link](https://international.download.nvidia.com/Windows/Quadro_Certified/441.66/441.66-quadro-desktop-notebook-win8-win7-64bit-international-whql.exe) | + ### Windows Server 2008R2, 2012, 2012R2 drivers + | Product series | Version | x64 library patch | x86 library patch | Driver link | |----------------|---------|-------------------|-------------------|-------------| | Quadro | 430.64 | [x64 library patch](https://raw.githubusercontent.com/keylase/nvidia-patch/master/win/ws2012_x64/quadro_430.64/nvencodeapi64.1337) | [x86 library patch](https://raw.githubusercontent.com/keylase/nvidia-patch/master/win/ws2012_x64/quadro_430.64/nvencodeapi.1337) | [Driver link](https://international.download.nvidia.com/Windows/Quadro_Certified/430.64/430.64-quadro-winserv2008r2-2012-2012r2-64bit-international-whql.exe) | @@ -486,8 +494,10 @@ Credits to [TroubleChute channel](https://www.youtube.com/channel/UCkih2oVTbXPEp | Quadro | 472.12 | [x64 library patch](https://raw.githubusercontent.com/keylase/nvidia-patch/master/win/ws2012_x64/quadro_472.12/nvencodeapi64.1337) | [x86 library patch](https://raw.githubusercontent.com/keylase/nvidia-patch/master/win/ws2012_x64/quadro_472.12/nvencodeapi.1337) | [Driver link](https://international.download.nvidia.com/Windows/Quadro_Certified/472.12/472.12-quadro-rtx-winserv-2012r2-64bit-international-whql.exe) | | Quadro | 472.84 | [x64 library patch](https://raw.githubusercontent.com/keylase/nvidia-patch/master/win/ws2012_x64/quadro_472.84/nvencodeapi64.1337) | [x86 library patch](https://raw.githubusercontent.com/keylase/nvidia-patch/master/win/ws2012_x64/quadro_472.84/nvencodeapi.1337) | [Driver link](http://international.download.nvidia.com/Windows/Quadro_Certified/472.84/472.84-quadro-rtx-winserv-2012r2-64bit-international-whql.exe) | + ### Windows Server 2016, 2019 drivers + | Product series | Version | x64 library patch | x86 library patch | Driver link | |----------------|---------|-------------------|-------------------|-------------| | Quadro | 430.86 | [x64 library patch](https://raw.githubusercontent.com/keylase/nvidia-patch/master/win/ws2016_x64/quadro_430.86/nvencodeapi64.1337) | [x86 library patch](https://raw.githubusercontent.com/keylase/nvidia-patch/master/win/ws2016_x64/quadro_430.86/nvencodeapi.1337) | [Driver link](https://international.download.nvidia.com/Windows/Quadro_Certified/430.86/430.86-quadro-winserv-2016-2019-64bit-international-whql.exe) | @@ -521,6 +531,7 @@ Credits to [TroubleChute channel](https://www.youtube.com/channel/UCkih2oVTbXPEp | Quadro | 472.12 | [x64 library patch](https://raw.githubusercontent.com/keylase/nvidia-patch/master/win/ws2016_x64/quadro_472.12/nvencodeapi64.1337) | [x86 library patch](https://raw.githubusercontent.com/keylase/nvidia-patch/master/win/ws2016_x64/quadro_472.12/nvencodeapi.1337) | [Driver link](https://international.download.nvidia.com/Windows/Quadro_Certified/472.12/472.12-quadro-rtx-winserv-2016-2019-64bit-international-whql.exe) | | Quadro | 472.84 | [x64 library patch](https://raw.githubusercontent.com/keylase/nvidia-patch/master/win/ws2016_x64/quadro_472.84/nvencodeapi64.1337) | [x86 library patch](https://raw.githubusercontent.com/keylase/nvidia-patch/master/win/ws2016_x64/quadro_472.84/nvencodeapi.1337) | [Driver link](http://international.download.nvidia.com/Windows/Quadro_Certified/472.84/472.84-quadro-rtx-winserv-2016-2019-64bit-international-whql.exe) | + ## Benchmarks -- [Plex Media Server: nVidia Hardware Transcoding Calculator for Plex Estimates](https://www.elpamsoft.com/?p=Plex-Hardware-Transcoding) - useful benchmark of achieved simultaneous transcodes with various stream quality and hardware with patched drivers. +* [Plex Media Server: nVidia Hardware Transcoding Calculator for Plex Estimates](https://www.elpamsoft.com/?p=Plex-Hardware-Transcoding) - useful benchmark of achieved simultaneous transcodes with various stream quality and hardware with patched drivers. diff --git a/win/nvfbcwrp/Patch.ps1 b/win/nvfbcwrp/Patch.ps1 index 5035700..9d71711 100644 --- a/win/nvfbcwrp/Patch.ps1 +++ b/win/nvfbcwrp/Patch.ps1 @@ -1,5 +1,25 @@ -# NVFBC DLL patch script -# Requires administrator privileges to run +# NVFBC DLL patch script. See README.md in this folder. +# +# Renames the genuine NvFBC64.dll (System32) and NvFBC.dll (SysWOW64) to +# NvFBC64_.dll / NvFBC_.dll and puts the nvfbcwrp wrappers in their place. +# The wrappers load the renamed genuine DLLs, NvFBC64_.dll / NvFBC_.dll, so keep the backups. +# +# Exit codes: 0 = patched (or already patched); 1 = refused or failed. +# A failed run rolls back every change it made. + +[CmdletBinding()] +param( + [string]$System32Path = "$env:SystemRoot\System32", + [string]$SysWOW64Path = "$env:SystemRoot\SysWOW64", + # No UAC relaunch and no "press Enter" prompts. For tests and already-elevated callers. + [switch]$Unattended, + # Escape hatch (also used by tests): skips the check that the file at the original path is the + # genuine NVIDIA DLL (CompanyName contains 'NVIDIA'). Use only if you are sure the file is genuine. + [switch]$SkipGenuineCheck +) + +$ErrorActionPreference = 'Stop' + Function Test-Administrator { [OutputType([bool])] param() @@ -8,144 +28,534 @@ Function Test-Administrator { return $user.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator); } } #end function Test-Administrator + Function Test-CommandExist { Param ($Command) $OldPreference = $ErrorActionPreference - $ErrorActionPreference = 'stop' + $ErrorActionPreference = 'Stop' try { if (Get-Command $Command) { RETURN $true } } Catch { RETURN $false } Finally { $ErrorActionPreference = $OldPreference } } #end function Test-CommandExist -If (Test-CommandExist pwsh.exe) { - $pwsh = "pwsh.exe" + +Function Wait-BeforeExit { + if (-not $Unattended) { $null = Read-Host 'Press Enter to exit' } } -Else { - $pwsh = "powershell.exe" -} -If (-Not (Test-Administrator)) { - Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass -Force - $Proc = Start-Process -PassThru -Verb RunAs $pwsh -Args "-ExecutionPolicy Bypass -Command Set-Location '$PSScriptRoot'; &'$PSCommandPath' EVAL" - If ($null -Ne $Proc) { - $Proc.WaitForExit() + +# Quotes one command-line argument. Windows PowerShell 5.1 joins -ArgumentList items +# with spaces and does not quote them, so paths must be quoted here. +Function Format-Argument { + param([Parameter(Mandatory)][AllowEmptyString()][string]$Value) + if ($Value -eq '' -or $Value -match '[\s"]') { + $escaped = $Value -replace '(\\*)"', '$1$1\"' # backslashes before a quote are doubled + $escaped = $escaped -replace '(\\+)$', '$1$1' # ...and before the closing quote + return '"' + $escaped + '"' } - If ($null -Eq $Proc -Or $Proc.ExitCode -Ne 0) { - Write-Warning "Failed to launch start as Administrator`r`nPress any key to exit" - $null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown'); + return $Value +} + +# Returns the argument list for the elevated copy of this script (no string building). +Function Get-ElevationArguments { + param( + [Parameter(Mandatory)][string]$ScriptPath, + [Parameter(Mandatory)][string]$System32Dir, + [Parameter(Mandatory)][string]$SysWOW64Dir + ) + return @( + '-NoProfile', '-ExecutionPolicy', 'Bypass', + '-File', (Format-Argument $ScriptPath), + '-System32Path', (Format-Argument $System32Dir), + '-SysWOW64Path', (Format-Argument $SysWOW64Dir) + ) +} + +# Relaunches this script elevated (one UAC prompt) and returns the child's exit code. +Function Start-ElevatedPatch { + $psExe = if (Test-CommandExist 'pwsh.exe') { 'pwsh.exe' } else { 'powershell.exe' } + $argList = Get-ElevationArguments -ScriptPath $PSCommandPath -System32Dir $System32Path -SysWOW64Dir $SysWOW64Path + if ($SkipGenuineCheck) { $argList += '-SkipGenuineCheck' } # the elevated copy must get the same switch + Write-Host 'Administrator rights are required. Approve the UAC prompt to continue...' + try { + $proc = Start-Process -FilePath $psExe -ArgumentList ($argList -join ' ') -Verb RunAs -Wait -PassThru + $code = $proc.ExitCode + if ($null -eq $code) { return 1 } + return [int]$code + } + catch { + Write-Host "ERROR: could not start the elevated process: $($_.Exception.Message)" -ForegroundColor Red + Wait-BeforeExit + return 1 } - exit } -ElseIf (($args.Count -Eq 1) -And ($args[0] -Eq "EVAL")) { - Start-Process $pwsh -NoNewWindow -Args "-ExecutionPolicy Bypass -Command Set-Location '$PSScriptRoot'; &'$PSCommandPath'" - exit + +# Runs a native tool (takeown.exe, icacls.exe) and returns ExitCode and the combined stdout+stderr +# text. $ErrorActionPreference is 'Continue' only for this call: on Windows PowerShell 5.1 a native +# stderr line under 'Stop' throws before $LASTEXITCODE can be read. Callers decide ONLY on ExitCode. +# 5.1 joins -ArgumentList style arrays without quotes, so each argument is quoted there. +Function Invoke-Native { + param( + [Parameter(Mandatory)][string]$FilePath, + [Parameter(Mandatory)][AllowEmptyCollection()][AllowEmptyString()][string[]]$Arguments + ) + $prevPreference = $ErrorActionPreference + $ErrorActionPreference = 'Continue' + try { + if ($PSVersionTable.PSEdition -eq 'Desktop') { + $passed = @($Arguments | ForEach-Object { Format-Argument $_ }) + } + else { + $passed = $Arguments + } + $out = & $FilePath @passed 2>&1 + $code = $LASTEXITCODE + } + finally { $ErrorActionPreference = $prevPreference } + $text = (@($out) | ForEach-Object { "$_" }) -join "`n" + if ($null -eq $code) { $code = 1 } # no exit code reported: treat as failure + return [pscustomobject]@{ ExitCode = [int]$code; Output = $text.Trim() } } + +# True when the file at $Path looks like the genuine NVIDIA DLL: the version resource CompanyName +# contains 'NVIDIA' (case-insensitive). Read-only. This is NOT an Authenticode check: the real files +# are signed by Microsoft (Windows Hardware Compatibility Publisher), not by NVIDIA. +# Returns Genuine (bool) and Found (text naming what was found, for the refusal message). +# ReadError is set (and Genuine false) when the file cannot be read at all, for example because another +# program holds it open exclusively. That is NOT the same as "no version information": the caller +# must report the read error and must not suggest reinstalling the driver. +Function Test-GenuineNvidiaDll { + param([Parameter(Mandatory)][string]$Path) + $company = '' + $product = '' + try { + # VersionInfo returns empty strings, without an error, for a file another program holds + # exclusively, so the file is opened first to turn that case into a read error. + $probe = [IO.File]::Open($Path, [IO.FileMode]::Open, [IO.FileAccess]::Read, [IO.FileShare]::ReadWrite -bor [IO.FileShare]::Delete) + $probe.Close() + $vi = (Get-Item -LiteralPath $Path -Force).VersionInfo + $company = [string]$vi.CompanyName + $product = [string]$vi.ProductName + } + catch { + # Use the .NET message, not the "Exception calling Open ..." wrapper that PowerShell adds. + $msg = $_.Exception.Message + if ($_.Exception.InnerException) { $msg = $_.Exception.InnerException.Message } + return [pscustomobject]@{ Genuine = $false; Found = ''; ReadError = $msg } + } + $found = if ($company -eq '' -and $product -eq '') { 'no version information' } + else { "CompanyName '$company', ProductName '$product'" } + return [pscustomobject]@{ Genuine = [bool]($company -match 'NVIDIA'); Found = $found; ReadError = $null } +} + +Function Get-Sha256 { + param([Parameter(Mandatory)][string]$Path) + return (Get-FileHash -LiteralPath $Path -Algorithm SHA256).Hash +} + +Function New-Target { + param([string]$Name, [string]$Dir, [string]$GenuineFile, [string]$BackupFile, [string]$Wrapper) + [pscustomobject]@{ + Name = $Name + Dir = $Dir + Original = (Join-Path $Dir $GenuineFile) + Backup = (Join-Path $Dir $BackupFile) + Temp = (Join-Path $Dir ((Split-Path -Leaf (Join-Path $Dir $GenuineFile)) + '.nvfbcwrp-tmp')) # wrapper copy staging name + Wrapper = $Wrapper + Action = $null # Install | ResumeInstall | AlreadyPatched + OriginalHash = $null + } +} + +# Decides what to do for one architecture. Read-only; throws to refuse the whole run. +Function Set-PatchPlan { + param($Target, [switch]$SkipGenuineCheck) + $name = $Target.Name + $wrapperHash = Get-Sha256 -Path $Target.Wrapper + $hasOriginal = Test-Path -LiteralPath $Target.Original -PathType Leaf + $hasBackup = Test-Path -LiteralPath $Target.Backup -PathType Leaf + + if (-not $hasOriginal -and -not $hasBackup) { + throw "[$name] Neither $($Target.Original) nor $($Target.Backup) exists. Nothing was changed." + } + if ($hasBackup -and (Get-Sha256 -Path $Target.Backup) -eq $wrapperHash) { + throw "[$name] The backup $($Target.Backup) is the wrapper, not the genuine DLL. The wrapper copy is the file to delete. If $($Target.Original) is the genuine NVIDIA DLL (File Properties > Details: Company 'NVIDIA Corporation', Product 'NVIDIA Frame Buffer Capture Library'), keep it and delete only the WRAPPER backup $($Target.Backup), then run again. If $($Target.Original) is missing or is also a wrapper, reinstall the NVIDIA driver, then run again. Nothing was changed." + } + + $originalHash = $null + if ($hasOriginal) { + try { $originalHash = Get-Sha256 -Path $Target.Original } + catch { + # Typically a sharing lock held by another program. Refuse before any change. + throw "[$name] could not read $($Target.Original): $($_.Exception.Message). Close programs that use NVIDIA components and run again. Nothing was changed." + } + } + + if ($hasOriginal -and $originalHash -eq $wrapperHash) { + if (-not $hasBackup) { + throw "[$name] $($Target.Original) is already the wrapper but $($Target.Backup) is missing, so the genuine DLL cannot be restored. Nothing was changed." + } + $Target.Action = 'AlreadyPatched' + } + elseif ($hasOriginal -and $hasBackup) { + throw "[$name] Stale backup: $($Target.Backup) exists and $($Target.Original) is not the wrapper. Check which case you have. Case (a): after a driver update, $($Target.Original) may be a NEW genuine NVIDIA DLL. First verify it: only if the NVIDIA driver was just installed or updated and NvFBC works, or the size and SHA-256 of $($Target.Original) match the driver package, MOVE the OLD BACKUP $($Target.Backup) to another folder (do not delete it), then run again. Case (b): if $($Target.Original) is an old or partial copy of the wrapper, delete ONLY $($Target.Original) and KEEP the backup, then run again. Nothing was changed." + } + elseif ($hasBackup) { + $Target.Action = 'ResumeInstall' + } + else { + # The file at the original path is about to be taken as the genuine DLL. Refuse unless it + # looks like one (an old wrapper build or a damaged file must not be moved and kept as genuine). + if (-not $SkipGenuineCheck) { + $genuine = Test-GenuineNvidiaDll -Path $Target.Original + if ($genuine.ReadError) { + throw "[$name] could not read version information of $($Target.Original): $($genuine.ReadError). Close programs that use NVIDIA components and run again. Nothing was changed." + } + if (-not $genuine.Genuine) { + throw "[$name] $($Target.Original) does not look like the genuine NVIDIA DLL. Found: $($genuine.Found). It may be an old wrapper build or a damaged file. Reinstall the NVIDIA driver. If you are sure the file is genuine, run again with -SkipGenuineCheck. Nothing was changed." + } + } + $Target.Action = 'Install' + $Target.OriginalHash = $originalHash + } +} + +# Returns the owner SID text of $Path. Get-Acl read works for non-owners (READ_CONTROL). +Function Get-OwnerSid { + param([Parameter(Mandatory)][string]$Path) + return (Get-Acl -LiteralPath $Path).GetOwner([System.Security.Principal.SecurityIdentifier]).Value +} + +# True when $Path already has ANY explicit ACE for Administrators (well-known SID S-1-5-32-544, +# so it works on localized Windows). Such an ACE must never be removed by the restore, because +# it was there before this script touched the file. An existing ACE is widened to FullControl by +# Unlock-DLL and is not narrowed again on restore (real genuine DLLs have none). +Function Test-ExplicitAdminAce { + param([Parameter(Mandatory)][string]$Path) + $adminsSid = 'S-1-5-32-544' + $rules = (Get-Acl -LiteralPath $Path).GetAccessRules($true, $false, [System.Security.Principal.SecurityIdentifier]) + return [bool]@($rules | Where-Object { $_.IdentityReference.Value -eq $adminsSid }).Count +} + +# Takes ownership for Administrators and grants it FullControl, using takeown.exe and +# icacls.exe. Both tools enable the privileges they need themselves, which Set-Acl does not +# (on a protected DACL such as the genuine TrustedInstaller-owned DLL, Set-Acl fails with +# "does not possess the 'SeSecurityPrivilege' privilege"). Only used after a Move-Item was +# refused with UnauthorizedAccess, and only on $Target.Original. The caller records the +# undo entry BEFORE calling this, so a half-done unlock is still rolled back. Function Unlock-DLL { - param ( - [string]$filePath + param([Parameter(Mandatory)][string]$Path) + # An Administrators ACE that already existed is widened to FullControl here (icacls /grant) and is + # not narrowed again by Restore-Owner (see Test-ExplicitAdminAce). + $takeown = "$env:SystemRoot\System32\takeown.exe" + $icacls = "$env:SystemRoot\System32\icacls.exe" + + $r = Invoke-Native -FilePath $takeown -Arguments @('/F', $Path, '/A') + if ($r.ExitCode -ne 0) { throw "takeown failed on $Path (exit $($r.ExitCode)): $($r.Output)" } + + $r = Invoke-Native -FilePath $icacls -Arguments @($Path, '/grant', '*S-1-5-32-544:F') + if ($r.ExitCode -ne 0) { throw "icacls /grant failed on $Path (exit $($r.ExitCode)): $($r.Output)" } + + Write-Host "OK: $Path is now owned by Administrators with FullControl" +} + +# Undoes Unlock-DLL with icacls.exe (no Set-Acl, no SDDL, no privilege P/Invoke). Arguments: +# OwnerSid the owner SID saved before the unlock (SID text, e.g. S-1-5-18). +# RemoveAdminGrant $true when this run added the Administrators grant (the file had no +# explicit Administrators ACE before). $false keeps any existing ACE. +# Returns one of: +# 'full' the grant was removed (if added) and the owner was set back. +# 'partial' the grant was removed, but the owner could not be set back. Typical on a real +# machine: the saved owner is NT SERVICE\TrustedInstaller, and an Administrator +# token is not allowed to assign it. The file stays owned by Administrators; the +# DACL is back to the original and the file contents are fine. A NOTE is printed. +# 'failed' the grant could not be removed, so the DACL is still changed (a red WARNING is +# printed). The caller treats this as a rollback failure. +Function Restore-Owner { + param( + [Parameter(Mandatory)][string]$Path, + [Parameter(Mandatory)][string]$OwnerSid, + [bool]$RemoveAdminGrant = $true + ) + $icacls = "$env:SystemRoot\System32\icacls.exe" + $adminsSid = 'S-1-5-32-544' + + if ($RemoveAdminGrant) { + $r = Invoke-Native -FilePath $icacls -Arguments @($Path, '/remove:g', "*$adminsSid") + if ($r.ExitCode -ne 0) { + $msg = "WARNING: could not remove the Administrators grant from $Path (exit $($r.ExitCode)): $($r.Output) Saved owner SID: $OwnerSid. Remove the Administrators entry by hand and restore the owner." + Write-Host $msg -ForegroundColor Red + if ($null -ne $script:AclNotes) { $script:AclNotes.Add($msg.Trim()) } + return 'failed' + } + } + + $r = Invoke-Native -FilePath $icacls -Arguments @($Path, '/setowner', "*$OwnerSid") + if ($r.ExitCode -ne 0) { + $note = "NOTE: the owner of $Path could not be set back to SID $OwnerSid (exit $($r.ExitCode)). The original DACL is restored and the file is still owned by Administrators. The file contents are not affected. $($r.Output)" + $note = $note.Trim() + Write-Host $note -ForegroundColor Yellow + if ($null -ne $script:AclNotes) { $script:AclNotes.Add($note) } + return 'partial' + } + + Write-Host "OK: restored original owner and DACL on $Path" + return 'full' +} + +# Reads only the PE headers of $Path (a few hundred bytes) and returns Machine and Magic. +# Magic 0x10B = PE32 (32-bit), 0x20B = PE32+ (64-bit). Throws if $Path is not a PE image. +Function Get-PeInfo { + param([Parameter(Mandatory)][string]$Path) + $fs = [System.IO.File]::OpenRead($Path) + try { + $dos = [byte[]]::new(64) + if ($fs.Read($dos, 0, 64) -ne 64 -or $dos[0] -ne 0x4D -or $dos[1] -ne 0x5A) { + throw "$Path is not a PE file (no MZ header)." + } + $lfanew = [BitConverter]::ToInt32($dos, 0x3C) # e_lfanew: offset of 'PE\0\0' + if ($lfanew -lt 64 -or $lfanew -gt 0x10000) { throw "$Path is not a PE file (bad e_lfanew)." } + $null = $fs.Seek($lfanew, [System.IO.SeekOrigin]::Begin) + # 'PE\0\0' (4 bytes) + COFF header (20 bytes) + Magic (2 bytes) = 26 bytes. + $hdr = [byte[]]::new(26) + if ($fs.Read($hdr, 0, 26) -ne 26 -or $hdr[0] -ne 0x50 -or $hdr[1] -ne 0x45 -or $hdr[2] -ne 0 -or $hdr[3] -ne 0) { + throw "$Path is not a PE file (no PE signature)." + } + return [pscustomobject]@{ + Machine = [BitConverter]::ToUInt16($hdr, 4) # IMAGE_FILE_HEADER.Machine + Magic = [BitConverter]::ToUInt16($hdr, 24) # IMAGE_OPTIONAL_HEADER.Magic + } + } + finally { $fs.Close() } +} + +# Pre-flight check of one wrapper. Lenient on purpose so ARM64 hosts are not refused: +# 32-bit wrapper = x86 PE32 (0x014C, 0x10B); 64-bit wrapper = PE32+ (0x20B) that is not x86. +Function Test-WrapperPe { + param($Target) + $file = Split-Path -Leaf $Target.Wrapper + try { $pe = Get-PeInfo -Path $Target.Wrapper } + catch { throw "$file is not a usable wrapper: $($_.Exception.Message) Nothing was changed." } + $found = 'Machine 0x{0:X4}, Magic 0x{1:X3}' -f $pe.Machine, $pe.Magic + if ($Target.Name -eq '32-bit') { + if ($pe.Machine -ne 0x014C -or $pe.Magic -ne 0x10B) { + throw "$file must be a 32-bit x86 PE image (Machine 0x014C, Magic 0x10B) for the 32-bit DLL; it is $found. Nothing was changed." + } + } + elseif ($pe.Magic -ne 0x20B -or $pe.Machine -eq 0x014C) { + throw "$file must be a 64-bit PE32+ image (Magic 0x20B) that is not x86, for example x64 0x8664 or ARM64 0xAA64; it is $found. Nothing was changed." + } +} + +# Moves the genuine DLL to its backup name. Never overwrites an existing backup. +# Known limit (F-7): an interrupt (Ctrl+C) after taking ownership in Unlock-DLL and before +# Restore-Owner has finished (the window is between the unlock and the end of the restore) can +# leave NvFBC64_.dll / NvFBC_.dll owned by Administrators, possibly with an Administrators +# FullControl entry. The DLL contents are intact. A re-run does not restore owner/ACL, because the +# move then succeeds without UnauthorizedAccess. Fix by hand if desired (see README.md). +# Order: move, verify the move, restore owner/ACL on the moved backup, then verify the hash, so the +# unrestored window is as short as possible. Rollback records are kept correct at each step. +Function Move-Genuine { + param($Target, $Undo) + $from = $Target.Original + $to = $Target.Backup + $aclEntry = $null + try { + Move-Item -LiteralPath $from -Destination $to # no -Force on purpose + } + catch [System.UnauthorizedAccessException] { + Write-Host "[$($Target.Name)] Access denied; taking ownership of $from ..." + # Everything needed to revert is recorded BEFORE the unlock. Rollback runs Restore-Owner on + # the original path after the file has been moved back, so a half-done unlock is undone too. + $ownerSid = Get-OwnerSid -Path $from + $hadAdminAce = Test-ExplicitAdminAce -Path $from + $aclEntry = [pscustomobject]@{ + Kind = 'Acl' + Path = $from + OwnerSid = $ownerSid + AddedAdminGrant = (-not $hadAdminAce) # never remove an Administrators ACE that was already there + Done = $false + } + $Undo.Add($aclEntry) + Unlock-DLL -Path $from + Move-Item -LiteralPath $from -Destination $to + } + # Recorded right after the move, so rollback moves the backup back even if a later step fails. + $Undo.Add([pscustomobject]@{ Kind = 'Move'; From = $from; To = $to }) + + if ((Test-Path -LiteralPath $from) -or -not (Test-Path -LiteralPath $to -PathType Leaf)) { + throw "Move verification failed: $from -> $to" + } + if ($aclEntry) { + # Owner/ACL go back on the moved BACKUP, not on the original path. Runs before the hash check + # so the window with the Administrators ownership is as short as possible. + $r = Restore-Owner -Path $to -OwnerSid $aclEntry.OwnerSid -RemoveAdminGrant $aclEntry.AddedAdminGrant + if ($r -eq 'failed') { throw "Could not remove the Administrators grant from the backup $to" } + if ($r -eq 'full') { $aclEntry.Done = $true } + else { $aclEntry.AddedAdminGrant = $false } # partial: the grant is already gone; rollback retries only the owner + } + if ((Get-Sha256 -Path $to) -ne $Target.OriginalHash) { + throw "Hash mismatch after moving the genuine DLL to $to" + } + Write-Host "[$($Target.Name)] OK: moved $from -> $to (SHA-256 verified)" +} + +# Installs the wrapper without ever leaving a partial file under the final name: +# 1. copy to .nvfbcwrp-tmp in the same folder (no -Force), +# 2. verify the SHA-256 of the temp copy, +# 3. Move-Item the temp copy to the final name (a rename, so it is all or nothing). +# An interrupted run leaves at most the temp file, which the next run removes. Target must not exist. +Function Install-Wrapper { + param($Target, $Undo) + $dest = $Target.Original + $tmp = $Target.Temp + if (Test-Path -LiteralPath $dest) { throw "$dest still exists; refusing to overwrite it." } + if (Test-Path -LiteralPath $tmp) { throw "$tmp still exists; refusing to overwrite it." } + # Recorded before the copy, so a partial temp file is removed on rollback. + $Undo.Add([pscustomobject]@{ Kind = 'Copy'; Path = $tmp }) + Copy-Item -LiteralPath $Target.Wrapper -Destination $tmp + if ((Get-Sha256 -Path $tmp) -ne (Get-Sha256 -Path $Target.Wrapper)) { + throw "Hash mismatch after copying the wrapper to $tmp" + } + # Recorded before the rename, so a rollback removes the final copy if the rename did happen. + $Undo.Add([pscustomobject]@{ Kind = 'Copy'; Path = $dest }) + Move-Item -LiteralPath $tmp -Destination $dest # no -Force: must not overwrite + if ((Get-Sha256 -Path $dest) -ne (Get-Sha256 -Path $Target.Wrapper)) { + throw "Hash mismatch after installing the wrapper as $dest" + } + Write-Host "[$($Target.Name)] OK: installed $(Split-Path -Leaf $Target.Wrapper) as $dest (SHA-256 verified)" +} + +# Removes leftover temp files of an interrupted earlier run. Only the exact temp name of this +# script next to the target is ever touched. Called after the plan is accepted. +Function Remove-LeftoverTemp { + param($Target) + if (Test-Path -LiteralPath $Target.Temp -PathType Leaf) { + Remove-Item -LiteralPath $Target.Temp -Force + Write-Host "[$($Target.Name)] Removed $($Target.Temp) left by an earlier interrupted run." + } +} + +# Reverts recorded steps in reverse order. Returns the number of steps that failed. +Function Undo-Changes { + param($Undo) + $failures = 0 + for ($i = $Undo.Count - 1; $i -ge 0; $i--) { + $u = $Undo[$i] + try { + switch ($u.Kind) { + 'Copy' { + if (Test-Path -LiteralPath $u.Path) { Remove-Item -LiteralPath $u.Path -Force } + if (Test-Path -LiteralPath $u.Path) { throw "$($u.Path) still exists" } + Write-Host "ROLLBACK OK: removed wrapper copy $($u.Path)" + } + 'Move' { + Move-Item -LiteralPath $u.To -Destination $u.From + if (-not (Test-Path -LiteralPath $u.From -PathType Leaf) -or (Test-Path -LiteralPath $u.To)) { + throw "could not move $($u.To) back to $($u.From)" + } + Write-Host "ROLLBACK OK: restored $($u.From)" + } + 'Acl' { + # Runs after the 'Move' undo, so the file is back at its original path. + # 'partial' (owner not set back) is only a note: the contents were put back. + if (-not $u.Done -and (Test-Path -LiteralPath $u.Path)) { + if ((Restore-Owner -Path $u.Path -OwnerSid $u.OwnerSid -RemoveAdminGrant $u.AddedAdminGrant) -eq 'failed') { + throw "owner/DACL of $($u.Path) not restored" + } + } + } + } + } + catch { + $failures++ + Write-Host "ROLLBACK FAILED ($($u.Kind)): $($_.Exception.Message)" -ForegroundColor Red + } + } + return $failures +} + +# Repeats the owner/ACL notes collected during the run, so they are not lost in the output. +Function Show-AclNotes { + if ($null -eq $script:AclNotes -or $script:AclNotes.Count -eq 0) { return } + Write-Host 'Owner/ACL summary:' -ForegroundColor Yellow + foreach ($n in $script:AclNotes) { Write-Host " $n" -ForegroundColor Yellow } +} + +Function Invoke-Patch { + $scriptDir = $PSScriptRoot + Write-Host 'Starting NVFBC DLL patch...' + $script:AclNotes = New-Object -TypeName System.Collections.Generic.List[string] + + $targets = @( + (New-Target -Name '64-bit' -Dir $System32Path -GenuineFile 'NvFBC64.dll' -BackupFile 'NvFBC64_.dll' -Wrapper (Join-Path $scriptDir 'nvfbcwrp64.dll')), + (New-Target -Name '32-bit' -Dir $SysWOW64Path -GenuineFile 'NvFBC.dll' -BackupFile 'NvFBC_.dll' -Wrapper (Join-Path $scriptDir 'nvfbcwrp32.dll')) ) - # Check if the file exists - if (-Not (Test-Path -Path $filePath)) { - Write-Error "The specified file does not exist." - exit + # Pre-flight: all checks run before anything is changed. + foreach ($t in $targets) { + if (-not (Test-Path -LiteralPath $t.Wrapper -PathType Leaf)) { + throw "$(Split-Path -Leaf $t.Wrapper) was not found in $scriptDir. Put nvfbcwrp64.dll and nvfbcwrp32.dll next to Patch.ps1. Nothing was changed." + } + if (-not (Test-Path -LiteralPath $t.Dir -PathType Container)) { + throw "Folder not found: $($t.Dir). Nothing was changed." + } + Test-WrapperPe -Target $t # throws (Nothing was changed.) if the wrapper is not the right PE kind + } + foreach ($t in $targets) { Set-PatchPlan -Target $t -SkipGenuineCheck:$SkipGenuineCheck } + # The plan was accepted. Leftover temp files of an interrupted run are removed now (not earlier, + # so a refused run changes nothing). + foreach ($t in $targets) { Remove-LeftoverTemp -Target $t } + + # Apply. Any failure rolls back everything done in this run, for both architectures. + $undo = New-Object -TypeName System.Collections.Generic.List[object] + try { + foreach ($t in $targets) { + switch ($t.Action) { + 'AlreadyPatched' { + Write-Host "[$($t.Name)] OK: already patched ($($t.Original) is the wrapper, $($t.Backup) kept). Skipped." + } + 'Install' { + Move-Genuine -Target $t -Undo $undo + Install-Wrapper -Target $t -Undo $undo + } + 'ResumeInstall' { + Write-Host "[$($t.Name)] Backup exists and $($t.Original) is missing; installing the wrapper only." + Install-Wrapper -Target $t -Undo $undo + } + } + } + } + catch { + Write-Host "ERROR: $($_.Exception.Message)" -ForegroundColor Red + Write-Host 'Patch install failed. Rolling back this run''s changes...' -ForegroundColor Red + $failed = Undo-Changes -Undo $undo + if ($failed -gt 0) { + Write-Host "ROLLBACK INCOMPLETE: $failed step(s) could not be reverted. Check the files named above and fix them by hand." -ForegroundColor Red + } + else { + Write-Host 'Rollback complete. System32 and SysWOW64 are back to their previous state.' + } + Show-AclNotes + return 1 } - Write-Information "Unlocking file: $filePath" + Show-AclNotes + Write-Host 'NVFBC patch complete.' + return 0 +} - # Get the file security object - $fileSecurity = Get-Acl -Path $filePath - - # Set the owner to "Administrators" - $administrators = [System.Security.Principal.NTAccount]"Administrators" - $fileSecurity.SetOwner($administrators) - - # Apply the new owner to the file - Set-Acl -Path $filePath -AclObject $fileSecurity - - # Define a new access rule for "Administrators" with full control - $administratorsRights = [System.Security.AccessControl.FileSystemRights]::FullControl - $accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($administrators, $administratorsRights, [System.Security.AccessControl.InheritanceFlags]::None, [System.Security.AccessControl.PropagationFlags]::None, [System.Security.AccessControl.AccessControlType]::Allow) - - # Add the new access rule to the file security object - $fileSecurity.AddAccessRule($accessRule) - - # Apply the updated security settings to the file - Set-Acl -Path $filePath -AclObject $fileSecurity - - Write-Information "Owner changed to 'Administrators' and full control permissions granted to 'Administrators' for file: $filePath" -} #end function Unlock-DLL -Write-Information "Staring NVFBC DLL patch..." - -$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path - -$system32Path = "$env:WINDIR\system32" -$sysWOW64Path = "$env:WINDIR\SysWOW64" - -$originalNvFBC64 = "$system32Path\NvFBC64.dll" -$originalNvFBC32 = "$sysWOW64Path\NvFBC.dll" - -$backupNvFBC64 = "$system32Path\NvFBC64_.dll" -$backupNvFBC32 = "$sysWOW64Path\NvFBC_.dll" - -$wrapperNvFBC64 = "$scriptDir\nvfbcwrp64.dll" -$wrapperNvFBC32 = "$scriptDir\nvfbcwrp32.dll" +# ---- main ---- +if (-not $Unattended -and -not (Test-Administrator)) { + exit (Start-ElevatedPatch) +} try { - # Step 1: Backup NvFBC64.dll - if (Test-Path $originalNvFBC64) { - Write-Information "Rename $originalNvFBC64 to $backupNvFBC64" - Unlock-DLL $originalNvFBC64 - Move-Item -Path $originalNvFBC64 -Destination $backupNvFBC64 -Force - Write-Information "✓ NvFBC64.dll backup completed" - } - else { - Write-Warning "NvFBC64.dll not found" - } - - # Step 2: Backup NvFBC.dll - if (Test-Path $originalNvFBC32) { - Write-Information "Rename $originalNvFBC32 to $backupNvFBC32" - Unlock-DLL $originalNvFBC32 - Move-Item -Path $originalNvFBC32 -Destination $backupNvFBC32 -Force - Write-Information "✓ NvFBC.dll backup completed" - } - else { - Write-Warning "NvFBC.dll not found" - } - - # Step 3: Copy 64-bit wrapper to system32 - if (Test-Path $wrapperNvFBC64) { - Write-Information "Copy $wrapperNvFBC64 to $originalNvFBC64" - Copy-Item -Path $wrapperNvFBC64 -Destination $originalNvFBC64 -Force - Write-Information "✓ 64-bit warpper copied" - } - else { - throw "nvfbcwrp64.dll not found" - } - - # Step 4: Copy 32-bit wrapper SysWOW64 - if (Test-Path $wrapperNvFBC32) { - Write-Information "Copy $wrapperNvFBC32 to $originalNvFBC32" - Copy-Item -Path $wrapperNvFBC32 -Destination $originalNvFBC32 -Force - Write-Information "✓ 32-bit wrapper copied" - } - else { - throw "nvfbcwrp32.dll not found" - } + $exitCode = Invoke-Patch } catch { - Write-Error "$($_.Exception.Message)" -ErrorAction Continue - Write-Error "Patch install failed!" -ErrorAction Continue - Write-Information "Rolling back changes..." - if ((Test-Path $backupNvFBC64) -and (-not (Test-Path $originalNvFBC64))) { - Move-Item -Path $backupNvFBC64 -Destination $originalNvFBC64 -Force -ErrorAction SilentlyContinue - } - if ((Test-Path $backupNvFBC32) -and (-not (Test-Path $originalNvFBC32))) { - Move-Item -Path $backupNvFBC32 -Destination $originalNvFBC32 -Force -ErrorAction SilentlyContinue - } - Read-Host "Press to stop." - exit 1 + Write-Host "ERROR: $($_.Exception.Message)" -ForegroundColor Red + $exitCode = 1 } -Write-Information "Press to stop." -pause +Wait-BeforeExit +exit $exitCode # End of script diff --git a/win/nvfbcwrp/README.md b/win/nvfbcwrp/README.md index 68639de..87abcb4 100644 --- a/win/nvfbcwrp/README.md +++ b/win/nvfbcwrp/README.md @@ -14,7 +14,7 @@ Wrapper for `NvFBC64.dll` library which injects private keys into `NvFBC_CreateE This procedure has to be repeated after any driver reinstall/update, so keep your copies of `nvfbcwrp64.dll` and `nvfbcwrp32.dll` files. -You can use the [PowerShell script](Patch.ps1) and [Patch Launcher](RunPatch.bat) to quickly apply patch. +You can use the [PowerShell script](Patch.ps1) and [Patch Launcher](RunPatch.bat) to quickly apply patch. Put `nvfbcwrp64.dll` and `nvfbcwrp32.dll` in the same folder as `Patch.ps1` first. The script asks for administrator rights itself, renames the genuine DLLs to `NvFBC64_.dll` and `NvFBC_.dll` (steps 2-4), and installs the wrappers (steps 5-6). It is safe to run again. It refuses, and changes nothing, when the file at the original path exists and is not the wrapper while a `NvFBC64_.dll` or `NvFBC_.dll` backup exists. If the original is missing and a genuine backup exists, it completes the installation. If the wrappers are already in place, it changes nothing, except that it removes a leftover `NvFBC64.dll.nvfbcwrp-tmp` or `NvFBC.dll.nvfbcwrp-tmp` file from an interrupted run. The refusal message says which case applies: (a) after a driver update the original may be a new genuine NVIDIA DLL; first verify it (File Properties > Details: Company `NVIDIA Corporation`, Product `NVIDIA Frame Buffer Capture Library`). Only if the driver was just installed or updated and NvFBC works, or the size and SHA-256 of the file match the driver package, move the old backup to another folder (do not delete it) and run again. (b) if the original is an old or partial copy of the wrapper, delete only that original file and keep the backup. If the backup itself is the wrapper, the message says that the wrapper copy is the file to delete (and that the original must be kept if it is genuine). Before any change, the script also checks that the file it would treat as genuine has Company `NVIDIA Corporation`; if it does not (for example an old wrapper build or a damaged file), it refuses, changes nothing, and asks you to reinstall the NVIDIA driver. If you are sure the file is genuine, run it again with `-SkipGenuineCheck`. If a file cannot be read at all (for example another program holds it open), the script refuses with a read error and asks you to close programs that use NVIDIA components and run again; it does not suggest reinstalling the driver. If anything fails, the script rolls back its changes. If the script was interrupted (for example with Ctrl+C) before the genuine DLL was moved, run it again. If it was interrupted after the move, run it again: it resumes and finishes the installation. If it was interrupted after the script took ownership of the genuine DLL and before the owner/ACL restore had finished, `NvFBC64_.dll` or `NvFBC_.dll` may remain owned by Administrators, possibly with an Administrators FullControl entry. The DLL itself is intact, and a re-run does not restore the owner or ACL. Fix it by hand if you want the original owner and ACL back. To restore manually, close all applications that use NvFBC, open an elevated (Administrator) prompt, delete `%WINDIR%\system32\NvFBC64.dll` and `%WINDIR%\SysWOW64\NvFBC.dll`, then rename `NvFBC64_.dll` back to `NvFBC64.dll` and `NvFBC_.dll` back to `NvFBC.dll`. ## Advanced Usage diff --git a/win/nvfbcwrp/RunPatch.bat b/win/nvfbcwrp/RunPatch.bat index bd31a99..f03b683 100644 --- a/win/nvfbcwrp/RunPatch.bat +++ b/win/nvfbcwrp/RunPatch.bat @@ -1,12 +1,18 @@ @echo off -%~d0 -cd "%~dp0" -if not exist Patch.ps1 ( - echo "Patch.ps1" is not found. - echo Press any key to exit - pause>nul - exit 1 -) else ( - start powershell.exe -ExecutionPolicy Bypass -File .\Patch.ps1 - exit +cd /d "%~dp0" +if not exist "%~dp0Patch.ps1" ( + echo "Patch.ps1" is not found next to this file. + pause + exit /b 1 ) +REM -ExecutionPolicy Bypass applies to this process only. Scripts downloaded as a zip +REM carry the Mark-of-the-Web, so the default policy would refuse to run them. +REM The policy setting itself is not changed. +powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%~dp0Patch.ps1" %* +set "RC=%ERRORLEVEL%" +if not "%RC%"=="0" ( + echo. + echo Patch failed with exit code %RC%. See the messages above. + pause +) +exit /b %RC%