1

I have an Asp.Net project (Vb.Net) that references a managed dll (library written in C#). That library project has several unmanaged dependencies dlls in a lib folder (copied into bin/Release/lib folder during build). The library is not a part of the main solution.

My library uses [DllImport] that references an unmanaged dll. But to let the unmanaged dll be found, I call SetDllDirectory():

        string path = // don't know how to generate the path
        SetDllDirectory(path);

I am struggling with generating the path to the unmanaged dll and its dependencies. I can start with my main project bin folder or something. But what should I do next? E.g. is there a way to copy the unmanaged dlls from the library's bin/Release/lib folder to my main project's bin folder? Or some other solution?

David Shochet
  • 5,035
  • 11
  • 57
  • 105
  • 1
    How about adding `xcopy "$(ProjectDir)lib\*.*" "$(OutDir)..\" /y /e` to the post build events? – Asti Jul 18 '18 at 03:25
  • @Asti Thank you for your suggestion. May I ask how I can set OutDir to point to my main project's bin folder? – David Shochet Jul 18 '18 at 15:39
  • It's a macro which is automatically set by the build path. – Asti Jul 18 '18 at 17:57
  • @Asti I used xcopy "$(ProjectDir)lib\*.*" "$(TargetDir)" /y , it seems to be what I need. If you would like to make your suggestion an answer, I would mark it as such. Thanks! I will not even need to use SetDllDirectory(), as I am making my library a NuGet package, so upon installation, all dlls will be copied right to the bin folder. – David Shochet Jul 19 '18 at 12:34
  • Hey, glad it worked out. I've added it as proper answer in case it might be useful to someone in the future. – Asti Jul 20 '18 at 18:01

1 Answers1

0

There are two ways you can go about this:

  1. Add the Dlls to the VS project as a file, then set Build Action to None and Copy to output directory as Copy. This should ensure that any external dependencies of the referenced library are copied.

  2. Add a command like xcopy "$(ProjectDir)lib\*.*" "$(OutDir)\" /y /e or xcopy "$(ProjectDir)lib\*.*" "$(TargetDir)" /y to the Build Events section in Project properties. This should copy the \lib directory from the project root to the output.

Asti
  • 12,447
  • 29
  • 38