I have VS2010 solution with 3 projects:
- A native (C++) DLL
- A managed (C#) DLL that uses the native DLL
- A managed (C#) console app that uses the managed DLL
I can set the managed DLL as a Reference to the console app, but I cannot set the native DLL as such a Reference.
To run the console app from within VS2010 without a crash, I must copy the native DLL to the .exe folder.
I have 2 questions:
- How can I add the native DLL to the DLL search path, so that I don’t have to do a manual copy?
- How can I do this so that each configuration (Debug/Release) takes the correct DLL version?
More Detailed Solution – due to Tilak’s answer:
Here are more newbie friendly details based Tilak’s reply below:
- Add the native DLL as a file (Add existing file…) to the managed console project.
- In the project properties pane, set “Build Action” to “Content” and “Copy to Output Directory” to “Copy if newer” or “Copy always”.
- Close the solution or VS2010.
- Open the managed project
.csprojfile in a text editor and find the<ItemGroup>which includes the native DLL name (this was created when we added the file in step 1). We’ll make several small changes there:- For the Debug build, instead of
<ItemGroup>use<ItemGroup Condition="'$(DefineConstants)' == 'DEBUG;TRACE' ">and enter the path to the Debug DLL. - Duplicate the entry for the Release build, and instead of
<ItemGroup>use<ItemGroup Condition="'$(DefineConstants)' != 'DEBUG;TRACE' ">and enter the path to the Release DLL.
- For the Debug build, instead of
- Save the file and reopen VS2010. The dependency should now be in place.
You can with copy the files in post build event,
or
Add files as resources.
You can use
$(ConfigurationName)in post build event to find out Debug/Release mode. And accordingly you can refer the libraries.If you are using resource approach, you have to modify the project file to copy based on Debug/Release mode. See details here