PlatformDetection and DynDll

Two small but widely used parts of MonoMod.Utils: PlatformDetection tells you which operating system, CPU architecture and .NET runtime you are running on, and DynDll loads native libraries and looks up their exports in a cross-platform way. Both are also what MonoMod itself uses internally to pick the right detour implementation.

PlatformDetection

MonoMod.Utils.PlatformDetection is a static class. Values are computed lazily on first access and cached.

Property Type Description
OS OSKind Operating system, including sub-types (Android, iOS, Wine).
Architecture ArchitectureKind Process architecture.
Runtime RuntimeKind Framework, CoreCLR, Mono, or Unknown.
Corelib CorelibKind Framework (mscorlib-based) or Core (System.Private.CoreLib-based).
RuntimeVersion Version Runtime version. The source warns that on .NET Framework, and on .NET Core 2.1 and earlier, this "will semi-frequently be nonsense" (.NET Core 2.x reports 4.6.x like Framework).

OSKind

OSKind is a bit-field: the low 5 bits identify the base OS and the remaining bits a sub-type.

Value Meaning
Unknown Detection failed.
Posix Generic POSIX.
Linux Linux (includes Posix).
Android Android (a sub-type of Linux).
OSX macOS (includes Posix).
IOS iOS (a sub-type of OSX).
BSD BSD (includes Posix).
Windows Windows.
Wine Windows API running under Wine (a sub-type of Windows).

Use the extension methods rather than ==, because sub-types include their parent bits:

if (PlatformDetection.OS.Is(OSKind.Linux)) { /* true on Linux and on Android */ }
OSKind kernel = PlatformDetection.OS.GetKernel();   // strips the sub-type: Android -> Linux
int sub = PlatformDetection.OS.GetSubtypeId();      // 0 for base kinds

How Wine Is Detected

When the OS is Windows, MonoMod checks, in order:

  1. The RunningOnWine switch, if set, decides.
  2. The XL_WINEONLINUX environment variable (used by the XIVLauncher/Dalamud loader): TRUE or FALSE, case-insensitive.
  3. Whether ntdll.dll exports a symbol named wineGetVersion (spelled exactly like that in the source).

If MonoMod misdetects Wine in your environment, set RunningOnWine explicitly; it is only read once, during the first OS detection.

Android is detected on Linux by the presence of /data and /system/build.prop; iOS by the /Applications, /System and /User directories without /Users.

ArchitectureKind

Value Meaning
Unknown
x86 32-bit x86
x86_64 (alias AMD64) 64-bit x86
Arm 32-bit ARM
Arm64 64-bit ARM
Bits64 Flag bit set on the 64-bit values

DynDll

MonoMod.Utils.DynDll loads native libraries and resolves exported symbols.

Method Description
IntPtr OpenLibrary(string? name) Loads a library and returns its handle; throws on failure. null returns the handle of the main program module.
bool TryOpenLibrary(string? name, out IntPtr libraryPtr) Same, returning false on failure.
void CloseLibrary(IntPtr lib) / bool TryCloseLibrary(IntPtr lib) Release a handle.
IntPtr GetExport(this IntPtr libraryPtr, string name) Finds an exported symbol; throws if missing.
bool TryGetExport(this IntPtr libraryPtr, string name, out IntPtr functionPtr) Same, returning false.
IntPtr libc = DynDll.OpenLibrary(PlatformDetection.OS.Is(OSKind.Windows) ? "msvcrt" : "c");
IntPtr rand = libc.GetExport("rand");

Backends and Name Resolution

  • In builds for .NET Core 3.0 and newer, DynDll uses System.Runtime.InteropServices.NativeLibrary, passing the calling assembly for its search rules.
  • In older builds it uses OS APIs directly: LoadLibrary/GetProcAddress on Windows, dlopen/dlsym on Linux and macOS (with a warning and a generic POSIX fallback on other systems).

On Linux and macOS the libdl backend tries several file names for a short name, adding the lib prefix and the platform suffix (.so on Linux, .dylib on macOS). On Linux, asking for c or libc also tries c.so.6 and glibc variants, which is why DynDll.OpenLibrary("c") works on glibc systems where libc.so is only a linker script.

See Also