views:

209

answers:

2

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.

A: 
  1. 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.
  2. Route all the projects in the solution to a single directory (managed and unmanaged).
Shay Erlichmen
Static libs are alright with me, but I've never had good luck setting up projects for them in Managed C++. Do you have advice or a resource that might help me out with setting that up?
Will Eddins
@Guard You can't do static managed c++ lib, I thought that you had problems with libs that the managed c++ needed (those can be static).
Shay Erlichmen
+3  A: 

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.

Joseph
I didn't even think of a post-build event for whatever reason. This works quite well for me, and I've posted the exact post-build I used in the question. Thanks.
Will Eddins