-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added detailed error for LoadLib exceptions + Added support for LoadL…
…ibraryEx
- Loading branch information
1 parent
0d1f691
commit e8c49a8
Showing
9 changed files
with
112 additions
and
24 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -1,8 +1,10 @@ | ||
// Licensed under the MIT license: https://opensource.org/licenses/MIT | ||
// Licensed under the MIT license: https://opensource.org/licenses/MIT | ||
|
||
using Whisper.net.Internals.Native.LibraryLoader; | ||
|
||
namespace Whisper.net.Native.LibraryLoader; | ||
|
||
internal interface ILibraryLoader | ||
{ | ||
IntPtr OpenLibrary(string filename, int flags); | ||
} | ||
LoadResult OpenLibrary(string filename); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Licensed under the MIT license: https://opensource.org/licenses/MIT | ||
|
||
namespace Whisper.net.Internals.Native.LibraryLoader; | ||
|
||
internal class LoadResult | ||
{ | ||
private LoadResult(bool isSuccess, string? errorMessage) | ||
{ | ||
IsSuccess = isSuccess; | ||
ErrorMessage = errorMessage; | ||
} | ||
|
||
public static LoadResult Success { get; } = new(true, null); | ||
|
||
public static LoadResult Failure(string errorMessage) | ||
{ | ||
return new(false, errorMessage); | ||
} | ||
|
||
public bool IsSuccess { get; } | ||
public string? ErrorMessage { get; } | ||
} |
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
30 changes: 27 additions & 3 deletions
30
Whisper.net/Internals/Native/LibraryLoader/WindowsLibraryLoader.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 |
---|---|---|
@@ -1,16 +1,40 @@ | ||
// Licensed under the MIT license: https://opensource.org/licenses/MIT | ||
// Licensed under the MIT license: https://opensource.org/licenses/MIT | ||
|
||
using System.ComponentModel; | ||
using System.Runtime.InteropServices; | ||
using Whisper.net.Internals.Native.LibraryLoader; | ||
|
||
namespace Whisper.net.Native.LibraryLoader; | ||
|
||
internal class WindowsLibraryLoader : ILibraryLoader | ||
{ | ||
public IntPtr OpenLibrary(string filename, int flags) | ||
private const uint LOAD_LIBRARY_SEARCH_DEFAULT_DIRS = 0x00001000; | ||
public LoadResult OpenLibrary(string filename) | ||
{ | ||
return LoadLibrary(filename); | ||
IntPtr loadedLib; | ||
|
||
try | ||
{ | ||
loadedLib = LoadLibraryEx(filename, IntPtr.Zero, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS); | ||
} | ||
catch (DllNotFoundException) | ||
{ | ||
loadedLib = LoadLibrary(filename); | ||
} | ||
|
||
if (loadedLib == IntPtr.Zero) | ||
{ | ||
var errorCode = Marshal.GetLastWin32Error(); | ||
var errorMessage = new Win32Exception(errorCode).Message; | ||
return LoadResult.Failure(errorMessage); | ||
} | ||
|
||
return LoadResult.Success; | ||
} | ||
|
||
[DllImport("kernel32", SetLastError = true, CharSet = CharSet.Auto)] | ||
private static extern IntPtr LoadLibrary([MarshalAs(UnmanagedType.LPTStr)] string lpFileName); | ||
|
||
[DllImport("kernel32", SetLastError = true, CharSet = CharSet.Auto)] | ||
private static extern IntPtr LoadLibraryEx([MarshalAs(UnmanagedType.LPTStr)] string lpFileName, IntPtr hFile, uint dwFlags); | ||
} |
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