如何检测Delphi应用是否通过Steam Proton在Linux系统中运行?
Great question! When your Windows-targeted app runs under Steam Proton on Linux, Proton's compatibility layer tricks the app into thinking it's still running natively on Windows—that's exactly why TOSVersion.Name keeps returning Windows instead of the actual Linux host.
Here are two reliable, easy-to-implement methods to detect if your app is running under Proton:
Method 1: Check Proton-Specific Environment Variables
Steam Proton sets several unique environment variables when launching Windows apps. You can check for these using Delphi's GetEnvironmentVariable function. If any of them exist, your app is running under Proton.
Delphi Code Example:
function IsRunningUnderProton: Boolean; var EnvBuffer: array[0..255] of Char; begin Result := False; // Check for common Proton/Steam Compat environment variables if GetEnvironmentVariable('STEAM_COMPAT_CLIENT_INSTALL_PATH', EnvBuffer, Length(EnvBuffer)) > 0 then begin Result := True; Exit; end; if GetEnvironmentVariable('STEAM_COMPAT_DATA_PATH', EnvBuffer, Length(EnvBuffer)) > 0 then begin Result := True; Exit; end; if GetEnvironmentVariable('PROTON_VERSION', EnvBuffer, Length(EnvBuffer)) > 0 then begin Result := True; Exit; end; end;
Method 2: Check for Linux-System Files
Proton lets Windows apps access some Linux system files. You can try reading /proc/version—a file that exists on all Linux systems and contains details about the Linux kernel. If you can read this file and it mentions "Linux", you're running under Proton.
Delphi Code Example:
function IsRunningUnderProton: Boolean; var ProcFile: TFileStream; ContentBuffer: array[0..1023] of Char; BytesRead: Integer; begin Result := False; try // Attempt to open the Linux proc version file ProcFile := TFileStream.Create('/proc/version', fmOpenRead or fmShareDenyNone); try BytesRead := ProcFile.Read(ContentBuffer, SizeOf(ContentBuffer) - 1); if BytesRead > 0 then begin ContentBuffer[BytesRead] := #0; // Verify the file contains Linux kernel info if Pos('Linux', ContentBuffer) > 0 then Result := True; end; finally ProcFile.Free; end; except // If we can't open the file, we're likely on native Windows Result := False; end; end;
Best Practice
For maximum accuracy, combine both methods: first check for the environment variables (fast and reliable), then fall back to the file check as a backup. This covers edge cases where some environment variables might not be set in rare Proton configurations.
Remember that TOSVersion will still report the Windows version that Proton is emulating (e.g., Windows 10), which can still be useful for your app's Windows-specific logic. The above functions just let you detect when that Windows environment is running on top of Linux via Proton.
内容的提问来源于stack exchange,提问作者Shaun Roselt

