-
-
Notifications
You must be signed in to change notification settings - Fork 99
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
Sandro Hanea
committed
Feb 17, 2025
1 parent
535bd75
commit c059c3a
Showing
3 changed files
with
47 additions
and
7 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
45 changes: 45 additions & 0 deletions
45
tools/WhisperNetDependencyChecker/DependencyWalker/NativeLibraryChecker.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,45 @@ | ||
namespace WhisperNetDependencyChecker.DependencyWalker; | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
public static class NativeLibraryChecker | ||
{ | ||
// Windows API (GetModuleHandle) | ||
[DllImport("kernel32.dll", SetLastError = true)] | ||
private static extern IntPtr GetModuleHandle(string lpModuleName); | ||
|
||
// Linux/macOS API (dlopen with RTLD_NOLOAD) | ||
private const int RTLD_NOLOAD = 0x4; // Flag to check if the library is already loaded | ||
|
||
[DllImport("libdl.so.2", EntryPoint = "dlopen", SetLastError = true)] | ||
private static extern IntPtr dlopen_linux(string filename, int flags); | ||
|
||
[DllImport("libSystem.dylib", EntryPoint = "dlopen", SetLastError = true)] | ||
private static extern IntPtr dlopen_macos(string filename, int flags); | ||
|
||
public static IntPtr GetLibraryHandle(string libraryName) | ||
{ | ||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
{ | ||
return GetModuleHandle(libraryName); | ||
} | ||
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) | ||
{ | ||
return dlopen_linux(libraryName, RTLD_NOLOAD); | ||
} | ||
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) | ||
{ | ||
return dlopen_macos(libraryName, RTLD_NOLOAD); | ||
} | ||
else | ||
{ | ||
throw new PlatformNotSupportedException("Unsupported operating system."); | ||
} | ||
} | ||
|
||
public static bool IsLibraryLoaded(string libraryName) | ||
{ | ||
return GetLibraryHandle(libraryName) != IntPtr.Zero; | ||
} | ||
} |