0

I created a netcoreapp2.1 console application and wanted to use a native dll.

Solution structure:

-test
  --dependecy1.dll (needed by some.dll)
  --depedency2.dll (needed by some.dll)
  --Program.cs
  --some.dll
  --test.csproj (netcoreapp2.1 console application)

To have the dlls copied in debug, I marked them with:

<None Update="some.dll">
  <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="dependecy1.dll">
  <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="dependecy2.dll">
  <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>

I wrote a static method and used DllImport("some.dll"):

[DllImport("some.dll")]
public static extern ulong do_dll_stuff(int arg1);

Then I called this method in the main method, and it worked!

Now I want to move this code in a class library (netstandard2.0). I created a class library project, moved the files into the new project, moved the code into the NativeMethods.cs and referenced it in my application above, nothing fancy. I also made sure the dlls are copied into the debug folder, in the same way as seen above.

Solution structure:

-test
  --Program.cs
  --test.csproj (netcoreapp2.1 console application)
-native
  --dependecy1.dll (needed by some.dll)
  --depedency2.dll (needed by some.dll)
  --native.csproj (netstandard2.0 class library, referenced by test.csproj)
  --NativeMethods.cs
  --some.dll

If I now run* my application, I get: Unhandled Exception: System.DllNotFoundException: Unable to load DLL 'some.dll' or one of its dependencies. I checked and all 3 dlls are inside test\bin\Debug\netcoreapp2.1

What am I missing?

edit 1: * I use the run button in my IDE. As far as I see, this calls dotnet test/bin/Debug/netcoreapp2.1/test.dll, which was the standard configuration after I created a new console application. The build process doesn't produce an exe when debugging, and the test.dll can be used with dotnet, as an exe would be used. https://stackoverflow.com/a/44074296/8862152

edit 2: I uploaded some files in a github repository. Take note, these are not the real dlls (not allowed to upload those), just text files. But it's good enough to build the sln and see the folder structure. https://github.com/alexandrosntak/cant-locate-dll-example

bla
  • 301
  • 2
  • 11
  • why you not reference those dlls by add reference method? what is purpose of going [Dllimport] ? – Vishal Pawar May 11 '20 at 10:34
  • 1
    @VishalPawar It's a native DLL, not a .NET Assembly – canton7 May 11 '20 at 10:38
  • I don't see this in your question -- where are the dlls in relation to the "application above"? The dlls need to be in the same folder as your .exe. – canton7 May 11 '20 at 10:39
  • @canton7 They can be found under `test\bin\Debug\netcoreapp2.1`, where the test.dll also is. – bla May 11 '20 at 10:41
  • And where is your exe? – canton7 May 11 '20 at 10:42
  • There is no exe, only that test.dll. But I can use it as an exe for debugging purposes. See here: https://stackoverflow.com/questions/44074121/build-net-core-console-application-to-output-an-exe :) – bla May 11 '20 at 10:46
  • 1
    Right, there's a bunch of information that's missing from your question, then. Please [edit] your question to explain exactly how you're doing "if I now run my application". Is that from the exe produced by "test.csproj", or something else? If so, where is the exe, on disk, in relation to the dlls? Explain that. – canton7 May 11 '20 at 10:48
  • Right, and are there copies of the native dlls in `test\bin\Debug`? – canton7 May 11 '20 at 11:29
  • I'm sure you mean the same, but just in case: All the dlls are not in `test\bin\Debug`, but in `test\bin\Debug\netcoreapp2.1`. They are all in the same location as the test.dll. – bla May 11 '20 at 11:36
  • Yep, my mistake – canton7 May 11 '20 at 11:39
  • Does your test application have the same bitness as your native DLLs? – canton7 May 11 '20 at 11:48
  • Both are 32-bit and I'm using the appropriate dotnet sdk. – bla May 11 '20 at 12:04

0 Answers0