Skip to content

Instantly share code, notes, and snippets.

@rajkosto
Last active April 23, 2025 02:10
Show Gist options
  • Save rajkosto/6bb60346d8a7f7f4e21566618e44020a to your computer and use it in GitHub Desktop.
Save rajkosto/6bb60346d8a7f7f4e21566618e44020a to your computer and use it in GitHub Desktop.
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <Shlobj.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
bool replaceAllInFile(const TCHAR* filePath)
{
FILE* file = _wfopen(filePath,TEXT("rb"));
if (!file)
{
puts("Error opening file for reading: ");
_putws(filePath);
puts("\n");
return false;
}
else
{
puts("Opened file for reading: ");
_putws(filePath);
puts("\n");
}
//Read file content into memory
fseek(file,0,SEEK_END);
size_t fileSize = ftell(file);
fseek(file,0,SEEK_SET);
//Ensure file size is valid before allocating buffer
if (fileSize <= 0)
{
printf("File is empty or error reading the file size.\n");
fclose(file);
return false;
}
char* buffer = (char*)calloc(fileSize+50,1); //+50 to prevent reading past end of buffer
if (!buffer)
{
printf("Memory allocation of in buffer of %zu bytes failed\n",fileSize+50);
fclose(file);
return false;
}
size_t bytesRead = fread(buffer,1,fileSize,file);
if (bytesRead != fileSize)
{
printf("Error reading the file. Bytes read: %zu, Expected: %zu\n",bytesRead,fileSize);
free(buffer);
fclose(file);
return false;
}
buffer[fileSize] = 0;
fclose(file);
file = NULL;
size_t outSize = fileSize;
char* outBuffer = (char*)malloc(outSize);
if (!outBuffer)
{
printf("Memory allocation of out buffer of %zu bytes failed\n",outSize);
free(buffer);
return false;
}
static const char search1[] = "\"Disable_FG_Override\":true";
static const char replace1[] = "\"Disable_FG_Override\":false";
static const char search2[] = "\"Disable_RR_Override\":true";
static const char replace2[] = "\"Disable_RR_Override\":false";
static const char search3[] = "\"Disable_SR_Override\":true";
static const char replace3[] = "\"Disable_SR_Override\":false";
static const char search4[] = "\"Disable_RR_Model_Override\":true";
static const char replace4[] = "\"Disable_RR_Model_Override\":false";
static const char search5[] = "\"Disable_SR_Model_Override\":true";
static const char replace5[] = "\"Disable_SR_Model_Override\":false";
static const char search6[] = "<Disable_FG_Override>1</Disable_FG_Override>";
static const char replace6[] = "<Disable_FG_Override>0</Disable_FG_Override>";
static const char search7[] = "<Disable_RR_Model_Override>1</Disable_RR_Model_Override>";
static const char replace7[] = "<Disable_RR_Model_Override>0</Disable_RR_Model_Override>";
static const char search8[] = "<Disable_RR_Override>1</Disable_RR_Override>";
static const char replace8[] = "<Disable_RR_Override>0</Disable_RR_Override>";
static const char search9[] = "<Disable_SR_Model_Override>1</Disable_SR_Model_Override>";
static const char replace9[] = "<Disable_SR_Model_Override>0</Disable_SR_Model_Override>";
static const char searchA[] = "<Disable_SR_Override>1</Disable_SR_Override>";
static const char replaceA[] = "<Disable_SR_Override>0</Disable_SR_Override>";
size_t outN = 0;
for (size_t i=0; i<fileSize; i++)
{
size_t searchLen = 1;
size_t replaceLen = 1;
const char* replaceSrc = &buffer[i];
if (!_strnicmp(replaceSrc,search1,sizeof(search1)-1))
{
searchLen = sizeof(search1)-1;
replaceSrc = replace1;
replaceLen = sizeof(replace1)-1;
}
else if (!_strnicmp(replaceSrc,search2,sizeof(search2)-1))
{
searchLen = sizeof(search2)-1;
replaceSrc = replace2;
replaceLen = sizeof(replace2)-1;
}
else if (!_strnicmp(replaceSrc,search3,sizeof(search3)-1))
{
searchLen = sizeof(search3)-1;
replaceSrc = replace3;
replaceLen = sizeof(replace3)-1;
}
else if (!_strnicmp(replaceSrc,search4,sizeof(search4)-1))
{
searchLen = sizeof(search4)-1;
replaceSrc = replace4;
replaceLen = sizeof(replace4)-1;
}
else if (!_strnicmp(replaceSrc,search5,sizeof(search5)-1))
{
searchLen = sizeof(search5)-1;
replaceSrc = replace5;
replaceLen = sizeof(replace5)-1;
}
else if (!_strnicmp(replaceSrc,search6,sizeof(search6)-1))
{
searchLen = sizeof(search6)-1;
replaceSrc = replace6;
replaceLen = sizeof(replace6)-1;
}
else if (!_strnicmp(replaceSrc,search7,sizeof(search7)-1))
{
searchLen = sizeof(search7)-1;
replaceSrc = replace7;
replaceLen = sizeof(replace7)-1;
}
else if (!_strnicmp(replaceSrc,search8,sizeof(search8)-1))
{
searchLen = sizeof(search8)-1;
replaceSrc = replace8;
replaceLen = sizeof(replace8)-1;
}
else if (!_strnicmp(replaceSrc,search9,sizeof(search9)-1))
{
searchLen = sizeof(search9)-1;
replaceSrc = replace9;
replaceLen = sizeof(replace9)-1;
}
else if (!_strnicmp(replaceSrc,searchA,sizeof(searchA)-1))
{
searchLen = sizeof(searchA)-1;
replaceSrc = replaceA;
replaceLen = sizeof(replaceA)-1;
}
if (replaceLen != searchLen)
{
outSize += replaceLen-searchLen;
outBuffer = (char*)realloc(outBuffer,outSize);
if (!outBuffer)
{
printf("Memory allocation of new out buffer of %zu bytes failed\n",outSize);
free(buffer);
return false;
}
}
memcpy(&outBuffer[outN],replaceSrc,replaceLen);
outN += replaceLen;
i += searchLen-1;
}
free(buffer);
buffer = NULL;
//Write the new buffer back to the file
file = _wfopen(filePath,TEXT("wb"));
if (!file)
{
puts("Error opening file for writing: ");
_putws(filePath);
puts("\n");
free(outBuffer);
return false;
}
size_t bytesWritten = fwrite(outBuffer,1,outSize,file);
free(outBuffer);
fclose(file);
if (bytesWritten != outSize)
{
printf("Error writing to file. Bytes written: %zu, Expected: %zu\n",bytesWritten,outSize);
return false;
}
printf("File updated successfully. Old size: %zu, new size: %zu\n",fileSize,outSize);
return true;
}
bool setFileReadOnly(const TCHAR* filePath, bool shouldBeReadOnly)
{
DWORD oldAttrs = GetFileAttributes(filePath);
if (oldAttrs == INVALID_FILE_ATTRIBUTES)
{
puts("Error getting file attributes for : ");
_putws(filePath);
puts("\n");
return false;
}
DWORD newAttrs = oldAttrs;
if (shouldBeReadOnly)
newAttrs |= FILE_ATTRIBUTE_READONLY;
else
newAttrs &= ~FILE_ATTRIBUTE_READONLY;
if (newAttrs == oldAttrs)
return true;
if (!SetFileAttributes(filePath,newAttrs))
{
puts("Error setting file attributes for : ");
_putws(filePath);
puts("\n");
return false;
}
return true;
}
bool restartService(const char* serviceName)
{
//Prepare command to stop and start the service
char command[256] = {};
snprintf(command, sizeof(command), "net stop %s && net start %s",serviceName,serviceName);
printf("Attempting to restart service: %s\n", serviceName);
//Execute the command
int result = system(command);
if (result == 0)
{
printf("Service %s restarted successfully.\n", serviceName);
return true;
}
else
{
printf("Failed to restart service %s. Command result: %d\n", serviceName, result);
return false;
}
}
int main()
{
TCHAR localAppDataPath[1024] = {};
if (!SUCCEEDED(SHGetFolderPathW(NULL,CSIDL_LOCAL_APPDATA,NULL,0,localAppDataPath)))
{
puts("Error getting local app data folder path\n");
return 2;
}
static const TCHAR TargetDbFileSuffix[] = TEXT("/NVIDIA Corporation/NVIDIA app/NvBackend/ApplicationOntology/data/fingerprint.db");
{
TCHAR fingerprintDbPath[1024];
wcscpy(fingerprintDbPath,localAppDataPath);
wcscpy(&fingerprintDbPath[wcslen(fingerprintDbPath)],TargetDbFileSuffix);
if (!setFileReadOnly(fingerprintDbPath,false))
goto onError;
if (!replaceAllInFile(fingerprintDbPath))
goto onError;
if (!setFileReadOnly(fingerprintDbPath,true))
goto onError;
}
static const TCHAR TargetJsonFileSuffix[] = TEXT("/NVIDIA Corporation/NVIDIA app/NvBackend/ApplicationStorage.json");
{
TCHAR jsonPath[1024];
wcscpy(jsonPath,localAppDataPath);
wcscpy(&jsonPath[wcslen(jsonPath)],TargetJsonFileSuffix);
if (!setFileReadOnly(jsonPath,false))
goto onError;
if (!replaceAllInFile(jsonPath))
goto onError;
}
//Services to restart
static const char SERVICE_1[] = "NvContainerLocalSystem";
static const char SERVICE_2[] = "NVDisplay.ContainerLocalSystem";
printf("Restarting NVIDIA services...\n");
restartService(SERVICE_1);
restartService(SERVICE_2);
system("pause");
return 0;
onError:
system("pause");
return 1;
}
@rajkosto
Copy link
Author

rajkosto commented Feb 1, 2025

Statically linked compiled version available at: https://files.sshnuke.net/SearchReplaceNvidiaApp.exe it's even set up to run as administrator which is required for the service restarts so all you have to do is download and double click it to unlock Nvidia App DLSS override capability on all game profiles

@rajkosto
Copy link
Author

rajkosto commented Feb 8, 2025

I've updated this code to also search/replace the override disables in the ApplicationOntology fingerprint.db which is the source of the starting values that the json file gets populated with when a new game gets detected, and then it sets the fingerprint.db to READ ONLY (setting the actual ApplicationStorage.json to READ ONLY causes NVContainer to crash occasionally so it's not a viable option)

So make sure to REDOWNLOAD AND RE-RUN THE EXE if you already have it.

@AsCo1d
Copy link

AsCo1d commented Feb 10, 2025

Thanks a lot for the tool! It does the job perfectly.
Interesting, for how much it is viable in the long term, after app / driver updates, adding new games, etc.

@Agiltech-com-au
Copy link

I've updated this code to also search/replace the override disables in the ApplicationOntology fingerprint.db which is the source of the starting values that the json file gets populated with when a new game gets detected, and then it sets the fingerprint.db to READ ONLY (setting the actual ApplicationStorage.json to READ ONLY causes NVContainer to crash occasionally so it's not a viable option)

So make sure to REDOWNLOAD AND RE-RUN THE EXE if you already have it.

WOW, awesome thank you so much!

@xumix
Copy link

xumix commented Feb 17, 2025

Or use this AI-rewriten Powershell script:

#Requires -RunAsAdministrator
<#
.SYNOPSIS
   PowerShell script to modify NVIDIA-related config files and restart services.

.DESCRIPTION
   1) Disables certain overrides in:
         - fingerprint.db
         - ApplicationStorage.json
   2) Toggles file read-only flags.
   3) Restarts specified NVIDIA services.

.NOTES
   Save this script as "UpdateNVIDIAOverrides.ps1" and run it in a PowerShell console.
#>

# Configuration - Paths
$NVIDIA_PATHS = @{
    FingerprintDb = "NVIDIA Corporation\NVIDIA app\NvBackend\ApplicationOntology\data\fingerprint.db"
    AppStorage = "NVIDIA Corporation\NVIDIA app\NvBackend\ApplicationStorage.json"
}

# Configuration - Services
$NVIDIA_SERVICES = @(
    'NvContainerLocalSystem',
    'NVDisplay.ContainerLocalSystem'
)

# Configuration - Replacements
$REPLACEMENTS = @(
    @{
        Search = '"Disable_FG_Override":true'
        Replace = '"Disable_FG_Override":false'
    },
    @{
        Search = '"Disable_RR_Override":true'
        Replace = '"Disable_RR_Override":false'
    },
    @{
        Search = '"Disable_SR_Override":true'
        Replace = '"Disable_SR_Override":false'
    },
    @{
        Search = '"Disable_RR_Model_Override":true'
        Replace = '"Disable_RR_Model_Override":false'
    },
    @{
        Search = '"Disable_SR_Model_Override":true'
        Replace = '"Disable_SR_Model_Override":false'
    },
    @{
        Search = '<Disable_FG_Override>1</Disable_FG_Override>'
        Replace = '<Disable_FG_Override>0</Disable_FG_Override>'
    },
    @{
        Search = '<Disable_RR_Model_Override>1</Disable_RR_Model_Override>'
        Replace = '<Disable_RR_Model_Override>0</Disable_RR_Model_Override>'
    },
    @{
        Search = '<Disable_RR_Override>1</Disable_RR_Override>'
        Replace = '<Disable_RR_Override>0</Disable_RR_Override>'
    },
    @{
        Search = '<Disable_SR_Model_Override>1</Disable_SR_Model_Override>'
        Replace = '<Disable_SR_Model_Override>0</Disable_SR_Model_Override>'
    },
    @{
        Search = '<Disable_SR_Override>1</Disable_SR_Override>'
        Replace = '<Disable_SR_Override>0</Disable_SR_Override>'
    }
)

function Write-Log {
    param(
        [string]$Message,
        [ValidateSet('Info', 'Error')]
        [string]$Level = 'Info'
    )
    
    $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
    if ($Level -eq 'Error') {
        Write-Host "[$timestamp] ERROR: $Message" -ForegroundColor Red
    } else {
        Write-Host "[$timestamp] INFO: $Message"
    }
}

function Set-FileReadOnly {
    param(
        [Parameter(Mandatory)]
        [string]$FilePath,
        [bool]$ReadOnly
    )

    try {
        $attributes = (Get-Item $FilePath).Attributes
        if ($ReadOnly) {
            $newAttributes = $attributes -bor [System.IO.FileAttributes]::ReadOnly
        } else {
            $newAttributes = $attributes -band -bnot [System.IO.FileAttributes]::ReadOnly
        }
        Set-ItemProperty -Path $FilePath -Name Attributes -Value $newAttributes
        return $true
    } catch {
        Write-Log "Failed to set file attributes for: $FilePath - $_" -Level Error
        return $false
    }
}

function Update-FileContent {
    param(
        [Parameter(Mandatory)]
        [string]$FilePath
    )

    try {
        $content = Get-Content -Path $FilePath -Raw
        $originalSize = (Get-Item $FilePath).Length
        
        foreach ($replacement in $REPLACEMENTS) {
            $content = $content.Replace($replacement.Search, $replacement.Replace)
        }

        Set-Content -Path $FilePath -Value $content -NoNewline
        $newSize = (Get-Item $FilePath).Length
        
        Write-Log "File updated successfully. Old size: $originalSize, new size: $newSize"
        return $true
    } catch {
        Write-Log "Error processing file: $FilePath - $_" -Level Error
        return $false
    }
}

function Restart-NvidiaServices {
    foreach ($service in $NVIDIA_SERVICES) {
        Write-Log "Attempting to restart service: $service"
        try {
            Stop-Service -Name $service -Force
            Start-Service -Name $service
            Write-Log "Service $service restarted successfully"
        } catch {
            Write-Log "Failed to restart service: $service, $_" -Level Error
        }
    }
}

function Main {
    try {
        $localAppData = [Environment]::GetFolderPath('LocalApplicationData')
        
        # Build full paths
        $fingerprintDbPath = Join-Path $localAppData $NVIDIA_PATHS.FingerprintDb
        $jsonPath = Join-Path $localAppData $NVIDIA_PATHS.AppStorage

        # Process fingerprint.db
        if (Test-Path $fingerprintDbPath) {
            if (-not (Set-FileReadOnly -FilePath $fingerprintDbPath -ReadOnly $false)) { return 1 }
            if (-not (Update-FileContent -FilePath $fingerprintDbPath)) { return 1 }
            if (-not (Set-FileReadOnly -FilePath $fingerprintDbPath -ReadOnly $true)) { return 1 }
        } else {
            Write-Log "Fingerprint DB not found at: $fingerprintDbPath" -Level Error
        }

        # Process ApplicationStorage.json
        if (Test-Path $jsonPath) {
            if (-not (Set-FileReadOnly -FilePath $jsonPath -ReadOnly $false)) { return 1 }
            if (-not (Update-FileContent -FilePath $jsonPath)) { return 1 }
        } else {
            Write-Log "JSON file not found at: $jsonPath" -Level Error
        }

        # Restart services
        Restart-NvidiaServices
        
        Write-Log "All operations completed successfully"
        return 0
    } catch {
        Write-Log "An unexpected error occurred: $_" -Level Error
        return 1
    } finally {
        Write-Host "`nPress any key to continue..."
        $null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')
    }
}

# Execute the script
exit (Main)

@FIocker
Copy link

FIocker commented Feb 17, 2025

And we're sure this is no rootkit?

@lzlrd
Copy link

lzlrd commented Feb 17, 2025

@xumix, your script should start with #Requires -RunAsAdministrator not # Requires -RunAsAdministrator.

@xumix
Copy link

xumix commented Feb 17, 2025

@xumix, your script should start with #Requires -RunAsAdministrator not # Requires -RunAsAdministrator.

thanks, I'm not very keen on PS, just checked overall logic correctness

@lzlrd
Copy link

lzlrd commented Feb 17, 2025

thanks, I'm not very keen on PS, just checked overall logic correctness

Nws, thanks for updating it so quickly.

@PatrickJnr
Copy link

And we're sure this is no rootkit?

I'm sure it's clean; you can always read and compile the code, even using ChatGPT to explain the code.

@Baije7Sensai
Copy link

Umm, guys, How tf** i mean, how do you all know how to use it?
Did I miss something?
I never used a .cpp file before, how am I supposed to run it?

@rajkosto
Copy link
Author

Umm, guys, How tf** i mean, how do you all know how to use it? Did I miss something? I never used a .cpp file before, how am I supposed to run it?

You just run the exe I linked in the first comment.

@JunaidDawood
Copy link

@rajkosto, do I need to re-run this every time there is a driver update or if I add new games/scan for them via the NVIDIA app?

@rajkosto
Copy link
Author

rajkosto commented Mar 20, 2025

Because the ontology defines the "whitelist"/"blacklist", and the ontology is made read only, no you do not.
however it being read only also means that driver/nvidia app updates wont update it unless you make it non-read only yourself, and then when it updates you will have to run this exe again

@cunningjames
Copy link

cunningjames commented Mar 23, 2025

I've been having an issue where the changes introduced by this script are overwritten within a day or so of running it. There's no update to the driver or the Nvidia app (at least not that I can see), it just ... reverts the changes and any games that I'd previously set to use DLSS 4 can no longer be set to use that model. Rerunning the script appears to fix this, but only until it reverts again. No idea what's up with that or if anyone else is experiencing this?

@eepiam
Copy link

eepiam commented Mar 25, 2025

Because the ontology defines the "whitelist"/"blacklist", and the ontology is made read only, no you do not. however it being read only also means that driver/nvidia app updates wont update it unless you make it non-read only yourself, and then when it updates you will have to run this exe again

How do you put it back to not “read-only” status?

@rajkosto
Copy link
Author

Right click->Properties uncheck Read Only, click OK
or just delete the file and i think NVIDIA App will redownload latest version anyway

@Bioswamp
Copy link

exe not worked with latest NVAPP 11.0.3.218 ((

@digitalrelicFM
Copy link

Right click->Properties uncheck Read Only, click OK or just delete the file and i think NVIDIA App will redownload latest version anyway

Right click->Properties of what exactly? The NVIDIA app itself?

@rajkosto
Copy link
Author

rajkosto commented Apr 1, 2025

fingerprint.db in %LOCALAPPDATA%/NVIDIA Corporation/NVIDIA app/NvBackend/ApplicationOntology/data/
for me, everything survived all nvidia app and driver updates since i released the exe.
just driver updates reset profile settings so you have to apply them again
but thats unrelated to what this exe does (unlocks nvidia app dlss settings for all games in list) which still remains

@DeadPixels1
Copy link

DeadPixels1 commented Apr 9, 2025

This was working for me, but it seems NVIDIA app has updated itself recently and now there isn't even a fingerprint.db file in %LOCALAPPDATA%/NVIDIA Corporation/NVIDIA app/NvBackend/ApplicationOntology/data/.

@rajkosto
Copy link
Author

rajkosto commented Apr 9, 2025

it will be there if you let nvidia app run for some time

@BZachHuff
Copy link

Should this allow me to add starcitizen to the app for config?

@Disegnator
Copy link

Hello, how can I stop a script that is launched via an .exe file?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment