简体   繁体   English

几个C#项目如何在没有繁琐的手动复制的情况下使用非托管DLL?

[英]How can several C# Projects use an unmanaged DLL, without tedious manual copying?

We wrote an unmanaged DLL with a simple C-style API last month. 上个月,我们使用简单的C风格API编写了一个非托管DLL。 It works and all is fine with it. 它有效,一切都很好。

Today, we're writing a demo app using this DLL. 今天,我们正在使用此DLL编写演示应用程序。 This is part of a whole new Solution in Visual Studio 2013. It compiles and runs fine, until it tries to use the DLL's API, where we get DllNotFoundException. 这是Visual Studio 2013中全新解决方案的一部分。它可以编译并运行良好,直到尝试使用DLL的API为止(我们在其中获得DllNotFoundException)。

Same thing happened last week with a test console app. 上周,测试控制台应用程序也发生了同样的事情。 The cheap-quick workaround was to copy the DLL from the workspace it was made in, and plop a copy into bin/x64/Debug/ in the project's folder in the new workspace. 廉价的快速解决方法是从创建它的工作空间中复制DLL,然后将副本放入新工作空间中项目文件夹中的bin / x64 / Debug /中。 The problem went away. 问题消失了。

Sure, we could do the same today for the new demo app, and get on with life, but this seems goofy. 当然,我们今天可以为新的演示应用程序做同样的事情,然后继续生活,但这似乎很愚蠢。 We have several test and demo apps, and apps to be given to customers, numbering fifteen or so. 我们有几个测试和演示应用程序,以及将要提供给客户的应用程序,大约有15个。 Surely we are not supposed to manually copy the DLL, after every change to it, into fifteen different bin/x64/Debug/ for fifteen different projects. 当然,我们不应该在每次更改DLL之后将DLL手动复制到15个不同项目的15个不同的bin / x64 / Debug /中。 And more, since we'll have Release versions too. 还有更多,因为我们还将提供Release版本。

Despite reading many blogs, articles, MSDN pages about unmanaged DLLs and Solutions and Projects and right-click to "Add References..." and all that, I've not seen any clear explanation how one should deal with multiple projects using one DLL. 尽管阅读了许多博客,文章,有关非托管DLL,解决方案和项目的MSDN页面,并右键单击“添加引用...”等等,但我还没有看到任何明确的解释来说明如何使用一个DLL处理多个项目。 。 Seems like there should be some way we could copy the DLL to one place, and tell all the projects just once to go look there. 似乎应该有某种方式可以将DLL复制到一个地方,并告诉所有项目一次去那里。

Relevant Side Question: We've tried the "Add References..." things, but it didn't like our DLL. 相关方面的问题:我们已经尝试了“添加引用...”的事情,但是它不喜欢我们的DLL。 Is this not applicable to unmanaged DLLs written in C? 这不适用于用C编写的非托管DLL吗? We don't see a clear statement whether or not we should be using Add Reference. 对于是否应该使用“添加引用”,我们没有明确的声明。

For your program to run it needs all its dependencies to be available, so the dll has to be included in the set of files you ship. 为了使程序运行,它需要所有依赖项都可用,因此dll必须包含在您附带的文件集中。 For managed assemblies the build process copies these files automatically, but for unmanaged dlls you need to tell the build system what to do. 对于托管程序集,生成过程会自动复制这些文件,但是对于非托管dll,则需要告诉生成系统该做什么。

The usual way is to either 通常的方法是

  • edit the post-build step (in your app's project properties) and add a copy/xcopy/robocppy command that copies the file to the output folder. 编辑构建后步骤(在应用程序的项目属性中),然后添加copy / xcopy / robocppy命令,将文件复制到输出文件夹。 You can embed macros in the filenames to locate the folders so that the project remains relocatable. 您可以将宏嵌入文件名中以找到文件夹,以便项目可重定位。

  • add the file to the project and get its properties in the solution explorer. 将文件添加到项目中,并在解决方案资源管理器中获取其属性。 Then set it to be "copied if newer". 然后将其设置为“如果更新则复制”。

By doing this the latest version of the dll is copied every time you do a build, and you can forget about it. 这样,每次执行构建时都会复制dll的最新版本,您可以将其忘记。

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

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