简体   繁体   English

C#/ C ++在同一解决方案中 - DllImport没有找到DLL

[英]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. 我有一个C ++ DLL项目和使用它的C#项目的解决方案。 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. 问题是c ++项目的构建路径位于解决方案文件夹中,而c#位于项目的bin文件夹中(每个嵌套有debug / release),因此DllImport找不到它们。
Is there a standard way to fix this? 有没有一种标准的方法来解决这个问题?

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. 您应该这样做的方法是将两个项目的构建路径设置为相同的“bin”目录...最好是解决方案,而不是项目。 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. 另一种技术是使用C ++应用程序的后构建步骤,将DLL复制到C#项目的文件夹。 That way you don't actually change any paths. 这样你实际上不会改变任何路径。 You just copy over a DLL. 您只需复制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. 这里要小心,因为当你清理C ++文件的项目时,实际上你可能仍然在C#projec'ts bin目录中有副本,让你不知道为什么事情没有按预期发生。

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. 或者,您可以将C ++ DLL部署到系统路径(也作为构建后步骤的一部分),但是您将遇到与上述相同的问题。

For debugging, I'd recommend these in the order presented. 对于调试,我建议按顺序推荐这些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM