Baruch
Baruch

Reputation: 21538

C# / C++ in same solution - DllImport not finding DLL

I have a solution with a C++ dll project and a C# project that uses it. The problem is that the build path of the c++ project is in the solution folder and the c# is in the project's bin folder (each nested with debug/release), so the DllImport doesn't find them.
Is there a standard way to fix this?

Upvotes: 8

Views: 1301

Answers (1)

Mark A. Donohoe
Mark A. Donohoe

Reputation: 30458

The way you are supposed to do this is to set the build path for both projects to the same 'bin' directory... preferrably one for the solution, not a project. Then just make all projects build to that one folder. You can change that from the Project settings.

Another technique is to use a post-build step for the C++ app that copies the DLL to the C# project's folder. That way you don't actually change any paths. You just copy over a DLL. Be careful here though because when you clean the C++ file's project, you may actually still have the copy in the C# projec'ts bin directory leaving you scratching your head as to why things aren't happening as expected.

Alternately, you can deploy the C++ DLL to a system path (also as part of a post-build step) but you'll have the same issues as stated above.

For debugging, I'd recommend these in the order presented.

Upvotes: 6

Related Questions