Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cleanest Method for copying Native DLLs in a .NET Project

I have a C# GUI application that references a Managed C++ project, which requires 7 native C++ DLLs. I'm looking for the cleanest method for copying these 7 DLLs to the final project output.

What works
Add all DLLs to the C# applications, specifying:

Build Action == "Content"
Copy To Output Directory == Copy Always"

This will make the base folder of the project a mess of DLLs in some cases, all of which are requirements of referenced projects, and not that project itself.

What does not work

  • Adding these DLLs to a folder named "Required DLLs" with the above settings. It copies it to a folder with the same name in the output, causing them to be in an incorrect location. I can't see a way to specify the output directory.
  • Embedded Resources: In C# P/Invoke, you can add DLLs you're referencing as embedded resources, and the DLLs are embedded inside your final library. I don't see this possibility in Managed C++, and I'm not even sure if it works with reference chains.
  • Adding the DLLs as content within the Managed C++ project. The files do not get copied to the output directory.

What is the best solution in this case? I'd prefer the Managed C++ project to be able to handle it's own DLL requirements if possible, and preferably in a way that won't prevent the project from being used across multiple applications.

As far as having a clean project goes, is it better to insert all my code files within subfolders in the project, and have the DLLs at the root to make the first solution work?

Solution:
Using the post-build suggestion from Joseph, the following command does the trick to use a "Required DLLs" folder.

xcopy "$(ProjectDir)Required DLLs*.*" "$(TargetDir)" /Q /Y

/Q hides the individual files from the output, and /Y suppresses overwrite prompts.

like image 851
Will Eddins Avatar asked Oct 05 '09 16:10

Will Eddins


1 Answers

You can use a post-build event to copy the contents of a directory (e.g., your "Required DLLs" directory) into the project's output directory.

like image 85
Joseph Avatar answered Nov 16 '22 20:11

Joseph