-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fc42c06
commit 6559a35
Showing
3 changed files
with
120 additions
and
35 deletions.
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
Yubico.Core/src/Yubico/PlatformInterop/Libraries.Net47.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// Copyright 2024 Yubico AB | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). | ||
// You may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#if NET47 | ||
using System; | ||
using System.IO; | ||
|
||
namespace Yubico.PlatformInterop | ||
{ | ||
/// <summary> | ||
/// .NET Framework 4.7 specific implementation for native library management. | ||
/// </summary> | ||
internal static partial class Libraries | ||
{ | ||
/// <summary> | ||
/// Encapsulates the .NET Framework 4.7 specific implementation details for native library management. | ||
/// This nested class handles the dynamic loading of architecture-specific (x86/x64) native libraries. | ||
/// </summary> | ||
private static class Net47Implementation | ||
{ | ||
// Handle to the loaded native library | ||
private static UnmanagedDynamicLibrary? _nativeShims; | ||
|
||
/// <summary> | ||
/// Gets the full path to the architecture-specific native library. | ||
/// </summary> | ||
/// <remarks> | ||
/// The path is constructed based on: | ||
/// - The application's base directory | ||
/// - The current process architecture (x86/x64) | ||
/// - The native library filename | ||
/// </remarks> | ||
private static string NativeShimsPath => | ||
Path.Combine( | ||
AppDomain.CurrentDomain.BaseDirectory, | ||
Environment.Is64BitProcess | ||
? "x64" | ||
: "x86", | ||
NativeShims); | ||
|
||
/// <summary> | ||
/// Initializes the native library for the current architecture. | ||
/// </summary> | ||
/// <remarks> | ||
/// This method is called by the public EnsureInitialized method. | ||
/// It ensures the appropriate version (x86/x64) of the native library is loaded. | ||
/// </remarks> | ||
internal static void Initialize() | ||
{ | ||
try | ||
{ | ||
EnsureNativeShimsLoaded(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
throw new DllNotFoundException( | ||
$"Failed to load native library from {NativeShimsPath}. " + | ||
$"Ensure the correct {(Environment.Is64BitProcess ? "x64" : "x86")} version is present.", | ||
ex); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Loads the native library if it hasn't been loaded already. | ||
/// </summary> | ||
/// <exception cref="Exception"> | ||
/// A variety of exceptions can be thrown during library loading, including but not limited to: | ||
/// - FileNotFoundException: If the DLL file is not found | ||
/// - BadImageFormatException: If the DLL is not compatible with the current architecture | ||
/// - Other exceptions based on the specific error condition encountered during loading | ||
/// </exception> | ||
private static void EnsureNativeShimsLoaded() | ||
{ | ||
if (_nativeShims != null) | ||
{ | ||
return; | ||
} | ||
|
||
_nativeShims = UnmanagedDynamicLibrary.Open(NativeShimsPath); | ||
} | ||
|
||
/// <summary> | ||
/// Releases resources associated with the native library. | ||
/// </summary> | ||
/// <remarks> | ||
/// This method safely disposes of the native library handle and resets the internal state. | ||
/// It can be called multiple times safely. | ||
/// </remarks> | ||
internal static void Cleanup() | ||
{ | ||
if (_nativeShims is null) | ||
{ | ||
return; | ||
} | ||
|
||
_nativeShims.Dispose(); | ||
_nativeShims = null; | ||
} | ||
} | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters