1

I have external dll which I want to include to project. I cannot add this dll as standard library (it is c++ dll).

I am using DllImport for external dll.

[DllImport("MyExternal.dll")]
public static extern UInt32 Authent([Out] UInt32[] LibRandNum);

This is working if I include dll in main project:

 "publishOptions": {
    "include": [
      "MyExternal.dll"
    ]
  }

But it is not working in class libaray (same publish option in class library project.json file).

If I add dll to "C:\windows\system32" than it is working. But I do not want to assume that dll exist, I want to add dll from project library.

edit 1:

I found "How to use native dependencies", but still not working. I don't know how to do it with nuget package, but I would like to avoid nuget if possible.

edit 2:

Something like this, but in Core class library.

edit 3

I have tried:

"copyToOutput": {
  "include": [ "MyExternal.dll" ]
}

Still without success.

edit 4

I created minimal solution (.NET core - visual studio 2015). You can get here : http://www.filedropper.com/myapp_1

There is dll file in class library (SGLW64.dll)

If you add this dll to C:\Windows\System32 than it is working.

Community
  • 1
  • 1
Raskolnikov
  • 3,791
  • 9
  • 43
  • 88
  • Is the external dll copied local to where the dll/exe is? – TheLethalCoder Dec 05 '16 at 09:47
  • I thought it was where dll/exe is. But now I cannot find it. So I don't know it is. – Raskolnikov Dec 05 '16 at 10:13
  • If its not alongside the dll/exe it won't be able to find it when you import it so won't work – TheLethalCoder Dec 05 '16 at 12:06
  • Create a package which contains this native dll and make it a dependency of the package using it. Make sure that the package containing native dll has the right structure. I wrote a blog post on this a while ago: https://blog.3d-logic.com/2015/11/10/using-native-libraries-in-asp-net-5/. While I wrote during the days of dnx most of it is still applicable (except for project references) – Pawel Dec 05 '16 at 22:58

1 Answers1

2

Unfortunately, copyToOutput is not transitive in project.json. See the GitHub Issue for more details. It's likely this will be fixed with the transition back to MSBuild, but for now we have to manage these dependencies manually.

Since we cannot transitively bring in copyToOutput'ed resources from the ClassLibrary to MyApp, we must tell MyApp where to find the .dll and where to put it. As we want to distribute the .dll with the ClassLibrary, this means that MyApp will have to pick it up using a relative path. Here, we can take advantage of the mappings object in copyToOutput, which gives us a ton of control over where we get files from and where we put them. If you try to use include instead of mappings, you'll end up getting the whole folder structure your .dll is in.

Add this to your buildOptions and publishOptions in MyApp's project.json:

"copyToOutput": {
   "mappings" : {
      "SGLW64.dll": "../ClassLibrary/SGLW64.dll"
   }
}

While it's not great that MyApp has to "know" about the internal implementation details of ClassLibrary by manually copying its .dll, this is a compromise we need to make until we move back to MSBuild.

Ryan Erdmann
  • 1,786
  • 10
  • 11