Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual studio Copy Local on reference doesnt work

I have two Unmanaged C++ DLLs in a solution, called A and B, and A has a reference to B. I want to copy B.dll to the application directory for A. When I click "Copy Local" on the reference in A's "Framework and References" tab in its project properties, it looks like it is set to true, but clicking apply reverts the value back to false. Any idea whats up here?

like image 457
Jason Fry Avatar asked Nov 27 '12 17:11

Jason Fry


2 Answers

I know it's been a while but I just ran into this issue and found this connect page:

https://connect.microsoft.com/VisualStudio/feedback/details/766064/visual-studio-2012-copy-local-cannot-be-set-via-the-property-pages

Seems like it's a known issue. You can work around it by editing the project file.

like image 111
Hoop Somuah Avatar answered Oct 13 '22 10:10

Hoop Somuah


@HoopSomuah Even if you work around the connect bug and got the Copy Local setting to stick, Visual Studio still won't copy dependent libs or dlls into the target folder for A, no matter what flags you set on the reference to B in project A.

Unmanaged reference handling in Visual Studio 2015 is still screwed up and I'm sure 2017 is equally as bad.

@JasonFry As @HansPassant pointed out, you'll need to add a post-build event to project A to copy B.dll into the same folder as A.dll.

Visual Studio doesn't provide any easy way to obtain the path to B.dll from project A, so we can make project B write that path to a text file, and then have project A read the path from that text file and copy B.dll into the same folder as A.dll.

open the project properties for B and add this as a post-build event:

echo $(TargetPath)>$(SolutionDir)References.txt

in the project properties for A, add as as a post-build event:

for /f %%f in ($(SolutionDir)References.txt) do xcopy /y %%f $(TargetDir)

If you had a 3rd project C.dll that needs to be copied into A's output folder too, add a similar post-build event to the project properties for C (note >> instead of > so it appends to the text file instead of overwriting it):

echo $(TargetPath)>>$(SolutionDir)References.txt
like image 20
Ger O'Donnell Avatar answered Oct 13 '22 09:10

Ger O'Donnell