Chapter 37 Key Takeaways
Environment and Arguments
GetEnvironmentVariable('NAME')reads environment variables (returns '' if not set)ParamStr(0)= executable path;ParamStr(1..ParamCount)= user arguments- Application data directories differ by platform:
- Windows:
%APPDATA%\AppName\ - Linux:
$HOME/.config/appname/ - macOS:
~/Library/Application Support/AppName/
File System Operations
FindFirst/FindNext/FindClose— search for files with wildcardsForceDirectories— create entire directory trees (better thanCreateDir)FileGetAttr— read file attributes (read-only, hidden, etc.)- Always call
FindCloseto release search handles
Running External Processes (TProcess)
TProcessfrom theProcessunit launches external programspoWaitOnExit— block until process finishespoUsePipes— capture stdout/stderr viaOutputandStderrstreamsExitCode— check the process's return code- Cross-platform URL opening:
start(Windows),xdg-open(Linux),open(macOS)
Conditional Compilation
| Directive | True When |
|---|---|
{$IFDEF WINDOWS} |
Compiling for Windows |
{$IFDEF LINUX} |
Compiling for Linux |
{$IFDEF DARWIN} |
Compiling for macOS |
{$IFDEF UNIX} |
Any Unix-like OS |
{$IFDEF CPU64} |
64-bit target |
- Evaluated at compile time, not runtime
- Code in false blocks is completely excluded from the executable
- For large differences, use separate platform-specific units
Calling C Libraries
function name(params): RetType; cdecl; external 'libname';cdecl= C calling convention (mandatory for C interop)PCharmaps to C'schar*;Pointermaps tovoid*- Static linking:
external 'libname'— library required at compile time - Dynamic loading:
LoadLibrary+GetProcAddress— library loaded at runtime
Platform-Specific Features
| Feature | Windows | Linux |
|---|---|---|
| Auto-start | Registry Run key |
~/.config/autostart/*.desktop |
| System info | WMI / GetSystemInfo | /proc/cpuinfo, /proc/meminfo |
| Signals | N/A (use SetConsoleCtrlHandler) | fpSignal(SIGTERM, @handler) |
| System tray | Shell_NotifyIcon API | TTrayIcon (Lazarus) |
PennyWise OS Integration
FinancePlatformunit encapsulates all platform-specific codeGetPennyWiseDataDirreturns the correct path for any platform- Auto-start uses Registry on Windows, .desktop file on Linux
OpenURLlaunches the help page in the default browser