Skip to content

Commit 1418694

Browse files
committed
Allow LoadLibraryEx to search for librdkafka.dll using the default Windows search paths if the dll can't be found in the hardcoded set of search paths
1 parent cefe511 commit 1418694

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/Confluent.Kafka/Impl/LibRdKafka.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,9 @@ public static bool Initialize(string userSpecifiedPath)
615615
#if NET462
616616
private static void LoadNetFrameworkDelegates(string userSpecifiedPath)
617617
{
618-
string path = userSpecifiedPath;
618+
var path = userSpecifiedPath;
619+
var loadLibraryFlags = WindowsNative.LoadLibraryFlags.LOAD_WITH_ALTERED_SEARCH_PATH;
620+
619621
if (path == null)
620622
{
621623
// in net45, librdkafka.dll is not in the process directory, we have to load it manually
@@ -652,9 +654,17 @@ private static void LoadNetFrameworkDelegates(string userSpecifiedPath)
652654
{
653655
path = Path.Combine(baseDirectory, "librdkafka.dll");
654656
}
657+
658+
if (!File.Exists(path))
659+
{
660+
// Allow LoadLibrary to search for the dll using the default Windows search path as defined here:
661+
// https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-search-order#search-order-for-unpackaged-apps
662+
path = "librdkafka.dll";
663+
loadLibraryFlags = 0;
664+
}
655665
}
656666

657-
if (WindowsNative.LoadLibraryEx(path, IntPtr.Zero, WindowsNative.LoadLibraryFlags.LOAD_WITH_ALTERED_SEARCH_PATH) == IntPtr.Zero)
667+
if (WindowsNative.LoadLibraryEx(path, IntPtr.Zero, loadLibraryFlags) == IntPtr.Zero)
658668
{
659669
// catch the last win32 error by default and keep the associated default message
660670
var win32Exception = new Win32Exception();

0 commit comments

Comments
 (0)