Skip to content

Exception when loading native dlls from the application path when using AssemblyDirectory and System32 as the DllImportSearchPath #111825

@ThomasGoulet73

Description

@ThomasGoulet73

Description

There seems to be an issue when loading a native dll in the application path that depends on a native dll also in the application path when using DllImportSearchPath.AssemblyDirectory | DllImportSearchPath.System32 as the search path. When both of those scenario occurs, it'll throw an exception that the dll could not be loaded. This issue is not reproducible when using only DllImportSearchPath.AssemblyDirectory or if a native dll does not have a dependency on another native dll in the application path. This also doesn't seem to affect native dlls shipped in a runtime pack because the .Net host seems to include a custom search path for the runtime packs.

This is a bug I found while investigating a problem when running WPF unit tests, see dotnet/wpf#10304 (comment) for more info. I'm not sure if it's the expected behavior, a bug or if I simply misunderstood the bug in WPF.

/cc @dipeshmsft

Reproduction Steps

  1. Clone https://github.com/ThomasGoulet73/DllSearchPathRepro
  2. Build TestLibrary1.
  3. Build TestLibrary2.
  4. Run ConsoleApp. It should exit without issue.
  5. Open Program.cs in ConsoleApp.
  6. Comment the line in Main with Test1WithoutSystem32
  7. Uncomment the line in Main with Test1WithSystem32
  8. Run ConsoleApp. It should exit with an exception that TestLibrary1.dll could not be loaded.

Expected behavior

Methods Test1WithoutSystem32 and Test1WithSystem32 should both succeed.

Actual behavior

Methods Test1WithoutSystem32 succeeds and Test1WithSystem32 fails.

Regression?

I tested using .Net 6 through .Net 9 and they all have the same behavior.

Known Workarounds

Change the DllImportSearchPath.

Configuration

.Net 9.0
Windows 11 x64
Don't know if it is specific to my configuration.

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Interop-coreclrin-prThere is an active PR which will close this issue when it is merged

    Type

    No type

    Projects

    Status

    No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions