/// <summary> /// Checks if the environment you are running in now supports WSL. /// </summary> public static void AssertWslSupported() { var commonErrorMessage = "Windows Subsystems for Linux requires 64-bit system and latest version of Windows 10 or higher than Windows Server 1709."; var is64BitProcess = (IntPtr.Size == 8); var is64BitOperatingSystem = is64BitProcess || InternalCheckIsWow64(); if (!is64BitOperatingSystem || !is64BitProcess) { throw new PlatformNotSupportedException(commonErrorMessage); } var osvi = new NativeMethods.OSVERSIONINFOEXW(); osvi.dwOSVersionInfoSize = (uint)Marshal.SizeOf(osvi); if (!NativeMethods.GetVersionExW(ref osvi)) { throw new PlatformNotSupportedException(commonErrorMessage); } if (osvi.dwPlatformId != 2) { throw new PlatformNotSupportedException(commonErrorMessage); } if (osvi.dwMajorVersion < 10 || osvi.dwMinorVersion < 0 || osvi.dwBuildNumber < 16299) { throw new PlatformNotSupportedException(commonErrorMessage); } var systemDirectory = Path.Combine( Environment.GetEnvironmentVariable("WINDIR"), "system32"); if (!File.Exists(Path.Combine(systemDirectory, "wslapi.dll"))) { throw new NotSupportedException("This system does not have WSL enabled."); } if (!File.Exists(Path.Combine(systemDirectory, "wsl.exe"))) { throw new NotSupportedException("This system does not have wsl.exe CLI."); } }
/// <summary> /// Checks if a 64-bit process is running on a 64-bit system. /// </summary> /// <returns>Returns True or False depending on whether or not.</returns> private static bool InternalCheckIsWow64() { var osvi = new NativeMethods.OSVERSIONINFOEXW(); osvi.dwOSVersionInfoSize = (uint)Marshal.SizeOf(osvi); if (NativeMethods.GetVersionExW(ref osvi)) { if ((osvi.dwMajorVersion == 5 && osvi.dwMinorVersion >= 1) || osvi.dwMajorVersion >= 6) { if (NativeMethods.IsWow64Process(NativeMethods.GetCurrentProcess(), out bool retVal)) { return(retVal); } } } return(false); }