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 a开发者_开发百科s 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.
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.
- You can work with static libs instead of dynamic, it will make your dlls bigger but single dll instead of multiple is just time saver and not only in your aspect.
- Route all the projects in the solution to a single directory (managed and unmanaged).
精彩评论