【发布时间】:2021-03-01 23:18:45
【问题描述】:
我已经成功地用 C# 构建了一个应用程序来连接高速测量传感器。该应用程序使用由硬件供应商提供的用于连接外部硬件的第三方 DLL 文件。下面是一个示例代码 sn-p,说明如何接口 DLL 文件中的类;
[DllImport("LJV7_IF.dll")]
internal static extern int LJV7IF_Initialize();
[DllImport("LJV7_IF.dll")]
internal static extern int LJV7IF_Finalize();
[DllImport("LJV7_IF.dll")]
internal static extern uint LJV7IF_GetVersion();
[DllImport("LJV7_IF.dll")]
internal static extern int LJV7IF_UsbOpen(int lDeviceId);
[DllImport("LJV7_IF.dll")]
internal static extern int LJV7IF_EthernetOpen(int lDeviceId, ref LJV7IF_ETHERNET_CONFIG ethernetConfig);
重点放在“DLLImport”函数的使用上。为了访问 DLL 文件,我在解决方案路径中名为 Library 的文件夹中创建,然后添加了命令
copy $(SolutionDir)Library\LJV7_IF.dll $(TargetDir)LJV7_IF.dll
这样 DLL 可以在成功构建后复制到 bin 文件夹中。因此,该应用程序当前可以运行。
使用 Visual Studio 中的内置发布选项发布应用程序时
生成的安装程序已安装,但 DLL“LJV7_IF.dll”未复制到应用程序输出。运行已安装的应用程序时,找不到 DLL 文件。这是我收到的错误消息,
错误消息指定应用程序无法找到 DLL 文件。
问题:是否有替代方法来引用 DLL 文件,以便在创建可执行文件时正确定位 DLL?
【问题讨论】:
-
只需使用 Project > Add Existing Item 将其添加到项目中。确保其 Build Action 属性为“Content”(默认情况下),以便安装程序项目知道它也需要部署。如果您将其 Copy to Output Directory 属性设置为“Copy if newer”,那么您不再需要 postbuild 事件。或者通过单击“应用程序文件”按钮手动完成。
-
天啊!!非常感谢你@Hans Passant。你太棒了它完美地工作。问题解决了。请允许我在新线程中添加解决方案。
标签: c# visual-studio-2019 dllimport publishing setup-deployment