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 wildcards
  • ForceDirectories — create entire directory trees (better than CreateDir)
  • FileGetAttr — read file attributes (read-only, hidden, etc.)
  • Always call FindClose to release search handles

Running External Processes (TProcess)

  • TProcess from the Process unit launches external programs
  • poWaitOnExit — block until process finishes
  • poUsePipes — capture stdout/stderr via Output and Stderr streams
  • ExitCode — 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)
  • PChar maps to C's char*; Pointer maps to void*
  • 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

  • FinancePlatform unit encapsulates all platform-specific code
  • GetPennyWiseDataDir returns the correct path for any platform
  • Auto-start uses Registry on Windows, .desktop file on Linux
  • OpenURL launches the help page in the default browser