1

We have the following project setup:

  • A native C++ project containing some custom native code Example.Native, producing a dll with the name example_native.dll
  • A wrapper project, defined in Swig, which wraps Example.Native, called Example.Native.Wrapper. It wraps the example_native.dll functions using typical P/Invoke calls and the [DllImport(...)] attribute
  • A normal .NET (5.0) project Example.Managed, which references Example.Native.Wrapper. There are no direct references to the native project itself
  • An executable project Example.Runner which references Example.Managed and indirectly uses the native dlls without any issues. When running the project and inspecting the loaded modules I can see the example_native.dll is loaded

We want to write integration tests for the Example.Managed project which includes the calls to Example.Native, as the integration is relatively complex and isolated unit tests would not give us enough confidence. We use MSTest for testing

However, when we run the integration tests, we always get the exception System.DllNotFoundException: Unable to load DLL 'example_native' or one of its dependencies: The specified module could not be found. (0x8007007E)

I already figured out the following:

  • During test execution, the native dlls don't get loaded. They do not appear in the Modules list.
  • The file exists in the working dir. Running Assert.That(File.Exists("example_native.dll")) is true. I also check the bin directory, and it exists there as well
  • I found a lot of similar questions, but most of them relate to older versions of visual studio, and their solutions don't seem to work
    • .testsettings file which references the dlls. appears to not be supported anymore in VS2022, as it crashes when trying to edit the file. Doing so in VS2019 worked, but the tests still failed
    • .runsetting file with the content below, did also not work, but honestly I'm not sure here if I included it correctly. I extended the project file with <RunSettingsFilePath>...</RunSettingsFilePath>
    <MSTest>
        <DeploymentEnabled>False</DeploymentEnabled>
        <AssemblyResolution>
          <Directory path="path\to\dlls" includeSubDirectories="true"/>
        </AssemblyResolution>
    </MSTest>

Is there any way I can get these dlls to load and my tests to run?

crazy_crank
  • 659
  • 4
  • 17
  • Does your native code load correctly when run outside the test framework? Have you checked 32/64 bit is correct? Is there any dependencies for the native library that needs installing (like vcredist)? – JonasH Sep 26 '22 at 06:52
  • 1
    0x8007007E can also be caused by indirect missing dlls (dependencies), for example MSVC RT debug or release dlls, etc. Use Process Monitor to detect what are the paths searched for and/or missing dependencies (filter by file acess & the process(es) involved): https://learn.microsoft.com/en-us/sysinternals/downloads/procmon – Simon Mourier Sep 26 '22 at 07:35
  • @JonasH Yes, when running the code outside of the tests everything is loaded correctly. All assemblies are 64 bit. Since the native dlls behave exactly as expected when running from our example application I expect that everything should be installed correctly – crazy_crank Sep 27 '22 at 07:54

0 Answers0