Listing of Source DPI-aware/DPI-aware.bat
@echo off

setlocal

echo.
echo -----------------------------------------------
echo --- Java 8 as DPI-aware for Windows 8 to 10 ---
echo -----------------------------------------------
echo.
echo (C) Nexum International SARL, 2022.
echo All rights reserved.
echo.

::
:: Check parameters.
::

set "OP=%1"
set "SCOPE=%2"
set "DIR=%~dpnx3"

if "%OP%"   =="" goto :syntax
if "%SCOPE%"=="" goto :syntax
if "%DIR%"  =="" goto :syntax

:: Make OP and WHO to upper case.
for /f "usebackq delims=" %%I in (`powershell "\"%OP%\".toUpper()"`   ) do set "OP=%%~I"
for /f "usebackq delims=" %%I in (`powershell "\"%SCOPE%\".toUpper()"`) do set "SCOPE=%%~I"

:: Mark "syntax" exit to return error code 5.
set PARAMERR=true

:: Check OP for "ADD" or "DELETE".
if "%OP%"=="ADD"    goto :op_OK
if "%OP%"=="DELETE" goto :op_OK
echo Invalid OP parameter: "%OP%".
echo.
goto :syntax
:op_OK

:: Check SCOPE for "USER" or "ALLUSERS" ("ALL_USERS" and "ALL-USERS" also accepted).
set "AGAIN="
if "%SCOPE%"=="USER"      set "KEY=HKCU" & goto :scope_OK
if "%SCOPE%"=="BOTH"      set "KEY=HKCU" & set "AGAIN=true" & goto :scope_OK
if "%SCOPE%"=="ALLUSERS"  set "KEY=HKLM" & goto :scope_OK
echo Invalid SCOPE parameter: "%SCOPE%".
echo.
goto :syntax
:scope_OK

:again_dir
:: Check for valid DIR pointing to a JRE or JDK, version 1.8.
if not exist "%DIR%" echo Directory "%DIR%" cannot be found. & echo. & goto :syntax
if not exist "%DIR%\bin\java.exe" echo Directory "%DIR%" does not seem to be a valid JRE or JDK directory, cannot find bin\java.exe. & echo. & goto :syntax

:: Check if JRE or JDK. 
set "T=JRE"
set "S=JRE"
if exist "%DIR%\jre\bin\java.exe" set "T=JDK"
if exist "%DIR%\lib\tools.jar"    set "S=JDK"

:: Check Java version for 1.8.0. 
for /f tokens^=2-5^ delims^=.-_^" %%j in ('"%DIR%\bin\java.exe" -fullversion 2^>^&1') do set "JFVER=%%j.%%k.%%l update %%m" & set "JVER=%%j.%%k.%%l"

echo ^ - Operation     = %OP%
echo ^ - Scope         = %SCOPE%
echo ^ - %S% directory = %DIR%
echo ^ - Java version  = %JFVER%
echo.

:: echo on
if not "%JVER%"=="1.8.0" echo Java version must be 1.8.0. Java versions 10 or better are DPI-aware. & goto :error

:: Check for Administrator/elevated privileges.
net session 2>&1 1>nul
if not errorlevel 1 set "ADMIN=true" & goto ok_admin 
if "%KEY%"=="HKLM" echo You do not have Administrator/elevated privileges for ALLUSERS scope. & goto :error
set "AGAIN="
echo You do not have Administrator/elevated privileges for BOTH (ALLUSERS) scope.
echo Changing scope to USER instead.
:ok_admin

echo Executing commands:

:again
:: Perform the operation for "directory\bin" (common to JRE and JDK).
call :process "%DIR%\bin\java.exe"            & if errorlevel 1 goto :error
call :process "%DIR%\bin\javaw.exe"           & if errorlevel 1 goto :error
call :process "%DIR%\bin\javaws.exe"          & if errorlevel 1 goto :error

:: Only if this is JRE, JDK does not have this file in it's "bin" directory.
if "%T%"=="JRE" call :process "%DIR%\bin\jp2launcher.exe" & if errorlevel 1 goto :error

:: Perform the operation for "directory\jre\bin", only for JDK.
if "%T%"=="JRE" goto :jre
call :process "%DIR%\jre\bin\java.exe"        & if errorlevel 1 goto :error
call :process "%DIR%\jre\bin\javaw.exe"       & if errorlevel 1 goto :error
call :process "%DIR%\jre\bin\javaws.exe"      & if errorlevel 1 goto :error
call :process "%DIR%\jre\bin\jp2launcher.exe" & if errorlevel 1 goto :error
:jre

:: Again? Do it again, now for all users.
if "%AGAIN%"=="true" set "AGAIN=" & set "KEY=HKLM" & goto :again

:: Successful with this directory, check if there are more directories.
shift
if "%3"=="" goto :done
set "DIR=%~dpnx3" 
if "%SCOPE%"=="USER" set "KEY=HKCU" & goto :again_dir
if "%SCOPE%"=="BOTH" set "KEY=HKCU" & set "AGAIN=true" & goto :again_dir
set "KEY=HKLM"
goto :again_dir 


::
:: --- Success ---
::
:done
echo.
echo ---------------
echo --- SUCCESS ---
echo ---------------
echo.
endlocal
exit /b 0


::
:: --- Processes a single file ---
::
:process
setlocal

:: Check that the file exists.
if not exist %1 endlocal & echo. & echo File %1 not found. & exit /b 1
  
:: Check ADD or DELETE.
if "%OP%"=="DELETE" goto :delete

:: Build ADD command.
set CMD=REG ADD "%KEY%\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /f /v %1 /t REG_SZ /d "~ HIGHDPIAWARE"
goto :execute

:delete
:: Check if registry key is present, if not, ignore and return OK. 
REG QUERY "%KEY%\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /v %1 >nul 2>nul
if errorlevel 1 endlocal & echo ^ - DPI-aware settings for %1 does not exist. & exit /b 0

:: Build DELETE command.
set CMD=REG DELETE "%KEY%\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /f /v %1

:: Execute the command.
:execute
echo ^ - %CMD%
%CMD%
if errorlevel 1 endlocal & echo. & echo Command failed. & exit /b 1
endlocal
exit /b 0


::
:: --- Syntax ---
::
:syntax
echo ------------------------------------------------------------------------
echo.
echo The DPI-aware program will ADD or REMOVE DPI-aware settings for the
echo current USER or ALLUSERS (you must have Administrators privileges to do
echo so). It will do this for the programs "java.exe", "javaw.exe", i.e. Java
echo executable without Command Prompt window and "javaws.exe", i.e.
echo Java Web Start.
echo.
echo Syntax: DPI-aware.bat OPERATION SCOPE DIRECTORY_1 ... DIRECTORY_N
echo.
echo Where:
echo.
echo ^  - OPERATION is "ADD" or "DELETE".
echo.
echo ^  - SCOPE is "USER", "ALLUSERS" or "BOTH".
echo ^    For "ALLUSERS", the command must run with Administrators/elevated
echo ^    privileges. For "BOTH", the command will first perform the operation
echo ^    with the scope "USER", and if Administrators/elevated privileges are 
echo ^    present, the scope "ALLUSERS" will also be performed. 
echo.
echo ^  - DIRECTORY specifies the directory of your JDK or JRE version 1.8.0,
echo ^    put double-quotes around the path if it contains spaces. 
echo.
echo Return code:
echo ^    0  (zero) for success,
echo ^    1  for failure,
echo ^    5  for parameter error; and
echo ^    9  for syntax error.
echo.
echo Example for 64-bit Java JDK 1.8.0 update 231:
echo ^    DPI-aware.bat ADD ALLUSERS "%%ProgramFiles%%\Java\jdk1.8.0_231"
echo.
echo Example for 32-bit Java JRE 1.8.0 update 231:
echo ^    DPI-aware.bat ADD ALLUSERS "%%ProgramFiles(x86)%%\Java\jre1.8.0_231"
echo.
if "%PARAMERR%"=="true" & endlocal & exit /b 5
endlocal
exit /b 9


::
:: --- Error ---
::
:error
echo.
echo ===============
echo === FAILURE ===
echo ===============
echo.
endlocal
exit /b 1