diff --git a/src/tests/Interop/CMakeLists.txt b/src/tests/Interop/CMakeLists.txt index 4137b5ba16df62..55b4a35bd80e57 100644 --- a/src/tests/Interop/CMakeLists.txt +++ b/src/tests/Interop/CMakeLists.txt @@ -57,6 +57,7 @@ add_subdirectory(MarshalAPI/FunctionPointer) add_subdirectory(NativeLibrary/NativeLibraryToLoad) add_subdirectory(DllImportAttribute/DllImportPath) add_subdirectory(DllImportAttribute/ExactSpelling) +add_subdirectory(DllImportSearchPaths/NativeLibraryWithDependency) add_subdirectory(ICustomMarshaler/ConflictingNames) add_subdirectory(ICustomMarshaler/Primitives) add_subdirectory(LayoutClass) diff --git a/src/tests/Interop/DllImportSearchPaths/DllImportSearchPathsTest.cs b/src/tests/Interop/DllImportSearchPaths/DllImportSearchPathsTest.cs index 00c7b375a7ed77..a956143b424aca 100644 --- a/src/tests/Interop/DllImportSearchPaths/DllImportSearchPathsTest.cs +++ b/src/tests/Interop/DllImportSearchPaths/DllImportSearchPathsTest.cs @@ -65,6 +65,20 @@ public static void AssemblyDirectory_Fallback_Found() Environment.CurrentDirectory = currentDirectory; } } + + [ConditionalFact(nameof(CanLoadAssemblyInSubdirectory))] + [PlatformSpecific(TestPlatforms.Windows)] + public static void AssemblyDirectory_SearchFlags_WithDependency_Found() + { + // Library and its dependency should be found in the assembly directory. + var assembly = Assembly.LoadFile(Path.Combine(Subdirectory, $"{nameof(DllImportSearchPathsTest)}.dll")); + var type = assembly.GetType(nameof(NativeLibraryWithDependency)); + var method = type.GetMethod(nameof(NativeLibraryWithDependency.Sum)); + + int sum = (int)method.Invoke(null, new object[] { 1, 2 }); + Assert.Equal(3, sum); + Console.WriteLine("NativeLibraryWithDependency.Sum returned {0}", sum); + } } public class NativeLibraryPInvoke @@ -93,3 +107,18 @@ public static int Sum(int a, int b) [DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory | DllImportSearchPath.System32)] static extern int NativeSum(int arg1, int arg2); } + +public class NativeLibraryWithDependency +{ + public static int Sum(int a, int b) + { + return CallDependencySum(a, b); + } + + // For LoadLibrary on Windows, search flags, like that represented by System32, are incompatible with + // looking at a specific path (per AssemblyDirectory), so we specify both flags to validate that we do + // not incorrectly use both when looking in the assembly directory. + [DllImport(nameof(NativeLibraryWithDependency))] + [DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory | DllImportSearchPath.System32)] + static extern int CallDependencySum(int a, int b); +} \ No newline at end of file diff --git a/src/tests/Interop/DllImportSearchPaths/DllImportSearchPathsTest.csproj b/src/tests/Interop/DllImportSearchPaths/DllImportSearchPathsTest.csproj index b98a1734c250b1..0d05dc0dbc3e2a 100644 --- a/src/tests/Interop/DllImportSearchPaths/DllImportSearchPathsTest.csproj +++ b/src/tests/Interop/DllImportSearchPaths/DllImportSearchPathsTest.csproj @@ -9,6 +9,7 @@ + @@ -18,7 +19,9 @@ + + diff --git a/src/tests/Interop/DllImportSearchPaths/NativeLibraryWithDependency/CMakeLists.txt b/src/tests/Interop/DllImportSearchPaths/NativeLibraryWithDependency/CMakeLists.txt new file mode 100644 index 00000000000000..4c0d01e49efa45 --- /dev/null +++ b/src/tests/Interop/DllImportSearchPaths/NativeLibraryWithDependency/CMakeLists.txt @@ -0,0 +1,8 @@ +project(NativeLibraryWithDependency) +include("${CLR_INTEROP_TEST_ROOT}/Interop.cmake") + +add_library(Dependency SHARED Dependency.cpp) +target_link_libraries(Dependency PRIVATE ${LINK_LIBRARIES_ADDITIONAL}) + +add_library(NativeLibraryWithDependency SHARED NativeLibraryWithDependency.cpp) +target_link_libraries(NativeLibraryWithDependency PRIVATE Dependency ${LINK_LIBRARIES_ADDITIONAL}) diff --git a/src/tests/Interop/DllImportSearchPaths/NativeLibraryWithDependency/Dependency.cpp b/src/tests/Interop/DllImportSearchPaths/NativeLibraryWithDependency/Dependency.cpp new file mode 100644 index 00000000000000..df3376374bb7fa --- /dev/null +++ b/src/tests/Interop/DllImportSearchPaths/NativeLibraryWithDependency/Dependency.cpp @@ -0,0 +1,10 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#include + +extern "C" DLL_EXPORT int STDMETHODCALLTYPE Sum(int a, int b) +{ + return a + b; +} + diff --git a/src/tests/Interop/DllImportSearchPaths/NativeLibraryWithDependency/NativeLibraryWithDependency.cpp b/src/tests/Interop/DllImportSearchPaths/NativeLibraryWithDependency/NativeLibraryWithDependency.cpp new file mode 100644 index 00000000000000..983c945a7ebad9 --- /dev/null +++ b/src/tests/Interop/DllImportSearchPaths/NativeLibraryWithDependency/NativeLibraryWithDependency.cpp @@ -0,0 +1,12 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#include + +extern "C" int STDMETHODCALLTYPE Sum(int a, int b); + +extern "C" DLL_EXPORT int STDMETHODCALLTYPE CallDependencySum(int a, int b) +{ + return Sum(a, b); +} +