互操作,本机DLL和Visual Studio生成过程 [英] Interop, Native DLL and Visual Studio build process

查看:153
本文介绍了互操作,本机DLL和Visual Studio生成过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

易混题,混乱的问题,但我希望我能得到你的注意。到了这一点:

Confusing title, confusing question, but I hope I can get your attention. To the point:

我有三个项目与解决方案。

I have a VS solution with three projects.


  1. PROJECT1 :本机DLL互操作的包装,其中进口 Native.dll 的。

  2. Project2的:一个WinForms项目,拨打电话通过PROJECT1包含在包装

  3. 项目3 :一个MSTest的项目,拨打电话通过 PROJECT1 Project2的

  1. Project1: A Native DLL interop wrapper, which imports Native.dll.
  2. Project2: A WinForms project that make calls to the wrapper contained by Project1.
  3. Project3: A MSTest project that make calls to the wrapper contained by Project1 Project2.

我已经包括含包装。 Native.dll在 PROJECT1 ,并将其配置为被复制到输出文件夹。

I have included Native.dll in the Project1, and configured it to be copied to the output folder.

这设置会导致失败的DllImport,说目标DLL不能被发现。原因很简单。我要么运行Project 2或3,并在每种情况下:

This setup will result in the DllImport failing, saying that the target dll couldn't be found. The reason is simple. I'm either running Project 2 or 3, and in each case:


  • Project2的:在建,它复制PROJECT1。在构建过程中生成的DLL,但不会复制Native.dll。当运行代码,文件夹是$ ProjectDir\bin\Debug(或释放)和Native.dll不可用。

  • Project2: When built, it copies the Project1.dll generated by the build process but DOES NOT copy Native.dll. When the code is run, the folder is $ProjectDir\bin\Debug (or release) and the Native.dll isn't available.

项目3:这是一个MSTest的项目;当记者问到运行所有测试,编译应用程序并拷贝输出EXE和DLL到输出文件夹。然而,它没有拷贝Native.dll。

Project3: It's a MSTest project; When asked to run all tests, it compiles the application and copies the output exe and dll to the output folder. However it does not copies the Native.dll.

在这两个例子中,该DLL被复制到PROJECT1,这是无用的debug文件夹。

In both examples, the DLL is copied to the debug folder of the Project1, which is useless.

有没有一种简单的方法来纠正这个项目?通过简单的,我的意思是我想避免使用后生成复制或其他手动过程。我也想尝试使用绝对路径来复制该.dll避免。

Is there a simple way to correct this project? By simple, I mean I'd like to avoid using Post Build to copy or other manual process. I'd also like to try avoiding using a absolute path to copy the .dll.

我在使用Visual Studio 2010。

I'm using Visual Studio 2010.

感谢

更新:场景上面,说明项目3引用PROJECT1,这是情况并非如此。项目3 Project2的引用,它引用PROJECT1。在这种情况下,Native.dll被复制到Project2的输出文件夹,而不是到项目3输出文件夹。我还添加了一个Project4,类库类型,加入到Project2中的引用,它也不会复制。

UPDATE: The scenario above was stating that Project3 referenced Project1, which is not the case. Project3 references Project2, which References Project1. In this case, the Native.dll is copied to Project2 output folder, but not to the Project3 output folder. I've also added a Project4, of class library type, added a reference to Project2 and it also doesn't copies.

换句话说,如果一个项目P [A]的项X具有复制到输出的规则和P [B]引用它,P [b]还复制X.但是,如果在P [C]引用P [b],它无视复制规则。

推荐答案

在您的项目添加到Native.dll的参考。在文件的属性窗口中,将生成操作为无和复制到输出目录为始终或复制如果较新的

Add a reference to 'Native.dll' in your project. In the file's property window, set the 'Build Action' to 'None' and the 'Copy to Output Directory' to 'Always', or 'Copy if Newer'

这篇关于互操作,本机DLL和Visual Studio生成过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆