Lee White
Lee White

Reputation: 3719

How to correctly reference a dll in Visual Studio 2010?

I have a solution that contains a C++ DLL project, and a C# project that will use this DLL (by using PInvoke).

The dll is being built to the x64/Release folder in my solution folder, which makes sense, because that way the C# project doesn't have to poke into the DLL project's folders.

I wonder what would be the correct way to reference it now though. Right now, the DLL project is a dependency of the C# project. My intuition told me that that should have been enough, but the C# project says it cannot find the DLL.

Should I just add the .dll file as a reference too? I thought this might work now but break things in the long run when project settings might get changed around.

Upvotes: 7

Views: 6560

Answers (3)

Spook
Spook

Reputation: 25927

Usually in such situations I configure my solution such that all projects compile to $(SolutionDir)bin\$(Configuration). The binaries are then located the same way as in production mode and DLLs may be easily used.

As for keeping the latest version of DLL, remember, that you may set a dependency, such that if anything changes in DLL, it will be rebuilt prior to building your assembly / application.

Yet another way is to use build events (prebuild and postbuild) to copy your DLL into appropriate folder.

Upvotes: 1

Thorsten Dittmar
Thorsten Dittmar

Reputation: 56697

You can not add an unmanaged DLL as reference in a managed project. The DLL needs to reside either in the executable's folder or in any other folder of the system's PATH.

The best way is to use a "Post Build Event" to have VS copy the DLL into the folder where it is needed every time it is rebuilt.

Upvotes: 1

Ken Kin
Ken Kin

Reputation: 4693

I've answered a similar question before. So I don't repeat the text here.

You might want to take a look of:

Wish that helps.

Upvotes: 2

Related Questions