|
12 | 12 | // See the License for the specific language governing permissions and
|
13 | 13 | // limitations under the License.
|
14 | 14 |
|
15 |
| -#if NET47 |
16 | 15 | using System;
|
17 |
| -using System.IO; |
18 |
| -using System.Runtime.InteropServices; |
19 |
| -#endif |
20 | 16 |
|
21 | 17 | namespace Yubico.PlatformInterop
|
22 | 18 | {
|
| 19 | + /// <summary> |
| 20 | + /// Handles the loading and management of native libraries required by the Yubico SDK. |
| 21 | + /// </summary> |
| 22 | + /// <remarks> |
| 23 | + /// This class provides cross-platform and cross-framework support for loading native libraries. |
| 24 | + /// The implementation differs based on the target framework: |
| 25 | + /// |
| 26 | + /// For .NET Framework 4.7: |
| 27 | + /// - Native libraries must be placed in architecture-specific subdirectories (x86/x64) |
| 28 | + /// - Library loading is handled explicitly at runtime based on process architecture |
| 29 | + /// - Requires proper cleanup through the Cleanup method |
| 30 | + /// |
| 31 | + /// For Modern .NET: |
| 32 | + /// - Native libraries are handled by the runtime's built-in library loading mechanism |
| 33 | + /// - Architecture-specific loading is managed automatically |
| 34 | + /// - No explicit cleanup is required |
| 35 | + /// </remarks> |
23 | 36 | internal static partial class Libraries
|
24 | 37 | {
|
25 | 38 | #if NET47
|
26 |
| - internal const string NativeShims = "Yubico.NativeS hims.dll"; |
27 |
| - private static bool _isNativeShimsIsLoaded; |
28 |
| - |
29 | 39 | /// <summary>
|
30 |
| - /// This method needs to run for .NET47 to determine to use either AppDirectory/x86/Yubico.NativeShims.dll or AppDirectory/x64/Yubico.NativeShims.dll |
| 40 | + /// The filename of the native shims library for .NET Framework 4.7. |
31 | 41 | /// </summary>
|
32 |
| - /// <exception cref="DllNotFoundException"></exception> |
33 |
| - internal static void EnsureNativeShimsLoaded() |
34 |
| - { |
35 |
| - if (_isNativeShimsIsLoaded) |
36 |
| - { |
37 |
| - return; |
38 |
| - } |
39 |
| - |
40 |
| - IntPtr moduleHandle = LoadLibrary(NativeShimsPath); |
41 |
| - if (moduleHandle == IntPtr.Zero) |
42 |
| - { |
43 |
| - throw new DllNotFoundException($"Failed to load native library: {NativeShimsPath}. Error: {Marshal.GetLastWin32Error()}"); |
44 |
| - } |
| 42 | + /// <remarks> |
| 43 | + /// For .NET Framework 4.7, the DLL must be placed in an architecture-specific subdirectory: |
| 44 | + /// - x86/Yubico.NativeShims.dll for 32-bit processes |
| 45 | + /// - x64/Yubico.NativeShims.dll for 64-bit processes |
| 46 | + /// The correct version is loaded at runtime based on the process architecture. |
| 47 | + /// </remarks> |
| 48 | + internal const string NativeShims = "Yubico.NativeShims.dll"; |
45 | 49 |
|
46 |
| - _isNativeShimsIsLoaded = true; |
47 |
| - } |
48 |
| - |
49 |
| - private static string NativeShimsPath => Path.Combine( |
50 |
| - AppDomain.CurrentDomain.BaseDirectory, |
51 |
| - Environment.Is64BitProcess ? "x64" : "x86", |
52 |
| - NativeShims); |
53 |
| - |
54 |
| - [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] |
55 |
| - [DefaultDllImportSearchPaths(DllImportSearchPath.SafeDirectories)] |
56 |
| - private static extern IntPtr LoadLibrary(string lpFileName); |
| 50 | + /// <summary> |
| 51 | + /// Ensures the native library is properly loaded for .NET Framework 4.7. |
| 52 | + /// </summary> |
| 53 | + /// <exception cref="DllNotFoundException"> |
| 54 | + /// Thrown when the native library cannot be loaded. This could be due to: |
| 55 | + /// - Missing DLL file in the architecture-specific directory (x86/x64) |
| 56 | + /// - Incorrect architecture (x86/x64 mismatch) |
| 57 | + /// - Missing dependencies |
| 58 | + /// - Insufficient permissions |
| 59 | + /// </exception> |
| 60 | + /// <remarks> |
| 61 | + /// This method must be called before any P/Invoke calls are made. |
| 62 | + /// The implementation details are handled in Libraries.Net47.cs. |
| 63 | + /// </remarks> |
| 64 | + public static void EnsureInitialized() => Net47Implementation.Initialize(); |
57 | 65 | #else
|
| 66 | + /// <summary> |
| 67 | + /// The filename of the native shims library for modern .NET versions. |
| 68 | + /// </summary> |
| 69 | + /// <remarks> |
| 70 | + /// For modern .NET, the runtime automatically handles library loading and architecture selection. |
| 71 | + /// The DLL extension is omitted as it's platform-specific and managed by the runtime. |
| 72 | + /// The library should be properly packaged with the correct runtimes/* folder structure in the NuGet package. |
| 73 | + /// </remarks> |
58 | 74 | internal const string NativeShims = "Yubico.NativeShims";
|
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// No-op implementation for modern .NET versions. |
| 78 | + /// </summary> |
| 79 | + /// <remarks> |
| 80 | + /// Library loading is handled automatically by the runtime. |
| 81 | + /// This method exists only for API compatibility with .NET Framework 4.7 code. |
| 82 | + /// </remarks> |
| 83 | + public static void EnsureInitialized() { } |
59 | 84 | #endif
|
60 | 85 | }
|
61 | 86 | }
|
0 commit comments