Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interop, Native DLL and Visual Studio build process

Confusing title, confusing question, but I hope I can get your attention. To the point:

I have a VS solution with three projects.

  1. Project1: A Native DLL interop wrapper, which imports Native.dll.
  2. Project2: A WinForms project that make calls to the wrapper contained by Project1.
  3. Project3: A MSTest project that make calls to the wrapper contained by Project1 Project2.

I have included Native.dll in the Project1, and configured it to be copied to the output folder.

This setup will result in the DllImport failing, saying that the target dll couldn't be found. The reason is simple. I'm either running Project 2 or 3, and in each case:

  • Project2: When built, it copies the Project1.dll generated by the build process but DOES NOT copy Native.dll. When the code is run, the folder is $ProjectDir\bin\Debug (or release) and the Native.dll isn't available.

  • Project3: It's a MSTest project; When asked to run all tests, it compiles the application and copies the output exe and dll to the output folder. However it does not copies the Native.dll.

In both examples, the DLL is copied to the debug folder of the Project1, which is useless.

Is there a simple way to correct this project? By simple, I mean I'd like to avoid using Post Build to copy or other manual process. I'd also like to try avoiding using a absolute path to copy the .dll.

I'm using Visual Studio 2010.

Thanks!

UPDATE: The scenario above was stating that Project3 referenced Project1, which is not the case. Project3 references Project2, which References Project1. In this case, the Native.dll is copied to Project2 output folder, but not to the Project3 output folder. I've also added a Project4, of class library type, added a reference to Project2 and it also doesn't copies.

In other words, if a project P[A] has a item X with "copy to output" rule and a P[B] references it, P[B] also copies X. However, if a P[C] references P[B], it disregards the copy rule.

like image 565
Bruno Brant Avatar asked Mar 27 '12 23:03

Bruno Brant


1 Answers

Add a reference to 'Native.dll' in your project. In the file's property window, set the 'Build Action' to 'None' and the 'Copy to Output Directory' to 'Always', or 'Copy if Newer'

like image 175
Ritch Melton Avatar answered Oct 17 '22 07:10

Ritch Melton