private static OSVersion GetOSVersion() { if (Environment.OSVersion.Platform is not PlatformID.Win32NT) { // Not running on Win7+. return(OSVersion.NotWindows); } try { // REVIEW: Should we just use the lazy handle instead of disposing? using var bcryptLibHandle = SafeLibraryHandle.Open(UnsafeNativeMethods.BCRYPT_LIB); if (bcryptLibHandle.DoesProcExist("BCryptKeyDerivation")) { // We're running on Win8+. return(OSVersion.Win8OrLater); } else { // We're running on Win7+. return(OSVersion.Win7OrLater); } } catch { // Not running on Win7+. return(OSVersion.NotWindows); } }
private static SafeLibraryHandle GetBcryptLibHandle() { try { return(SafeLibraryHandle.Open("bcrypt.dll")); } catch { // If we're not on an OS with BCRYPT.DLL, just bail. return(null); } }
/* * HELPER FUNCTIONS */ private static SafeLibraryHandle GetLibHandle(string libraryName, ref SafeLibraryHandle?safeLibraryHandle) { if (safeLibraryHandle is null) { var newHandle = SafeLibraryHandle.Open(libraryName); if (Interlocked.CompareExchange(ref safeLibraryHandle, newHandle, null) is not null) { newHandle.Dispose(); } } return(safeLibraryHandle); }
private static void InitHttpApi(ushort majorVersion, ushort minorVersion) { version.HttpApiMajorVersion = majorVersion; version.HttpApiMinorVersion = minorVersion; var statusCode = HttpInitialize(version, (uint)(HTTP_FLAGS.HTTP_INITIALIZE_SERVER | HTTP_FLAGS.HTTP_INITIALIZE_CONFIG), null); supported = statusCode == UnsafeNclNativeMethods.ErrorCodes.ERROR_SUCCESS; if (supported) { HttpApiModule = SafeLibraryHandle.Open(HTTPAPI); HttpSetRequestProperty = HttpApiModule.GetProcAddress <HttpSetRequestPropertyInvoker>("HttpSetRequestProperty", throwIfNotFound: false); SupportsReset = HttpSetRequestProperty != null; SupportsTrailers = IsFeatureSupported(HTTP_FEATURE_ID.HttpFeatureResponseTrailers); SupportsDelegation = IsFeatureSupported(HTTP_FEATURE_ID.HttpFeatureDelegateEx); } }
private static OSVersion GetOSVersion() { const string BCRYPT_LIB = "bcrypt.dll"; SafeLibraryHandle bcryptLibHandle = null; try { bcryptLibHandle = SafeLibraryHandle.Open(BCRYPT_LIB); } catch { // we'll handle the exceptional case later } if (bcryptLibHandle != null) { using (bcryptLibHandle) { if (bcryptLibHandle.DoesProcExist("BCryptKeyDerivation")) { // We're running on Win8+. return(OSVersion.Win8OrLater); } if (bcryptLibHandle.DoesProcExist("BCryptDeriveKeyPBKDF2")) { // We're running on Win7+. return(OSVersion.Win7OrLater); } // We're running on Vista+. return(OSVersion.WinVistaOrLater); } } else { // Not running on Win7+. return(OSVersion.NotWindows); } }
private static Lazy <SafeLibraryHandle> GetLazyLibraryHandle(string libraryName) { // We don't need to worry about race conditions: SafeLibraryHandle will clean up after itself return(new Lazy <SafeLibraryHandle>(() => SafeLibraryHandle.Open(libraryName), LazyThreadSafetyMode.PublicationOnly)); }