问题描述
嗨!
$
我要在我的C#项目中添加一个基于C的dll,以便与传感器通信。
Hi!
I've to add a C based dll to my C# project in order to communicate with a sensor.
当发生以下错误时,我无法添加对dll的引用:"无法添加引用。请确保该文件是可访问的,并且它是有效的程序集或COM组件。"
I cant add a reference to the dll as the following error occurs: "The reference could not be added. Please make sure that the file is accesible, and that it is a valid assembly or COM component."
我尝试使用
TlbImp.exe xyz.dll
但是发生了另一个错误,声明xyz.dll不是有效的类型库。
But another error occurs, stating that xyz.dll is not a valid type library.
我也试过使用regsvr32,但无济于事。
I've also tried to use regsvr32 but to no avail.
另一种方法是使用以下行在代码中引用库:
Another approach was to reference the library in code using the following lines:
[DllImport("xyz.dll", CallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl)]
public static extern Boolean PresenceDetectionExample();
但这也不起作用。
我正在使用Visual Studio Enterprise 2017。
I'm using Visual Studio Enterprise 2017.
是有什么方法可以解决这个问题吗?
Is there any way how to fix that issue?
谢谢!
推荐答案
并关注是关于这个主题的好文章:
and following is a good article on this topic:
这里也是一个不错的选择发布如何执行此操作:
and here is also a good post on how to do this:
以及PInvoke用于在C#中调用未托管代码的另一个资源:
and one more resource for PInvoke to call the un-managed code in C#:
这篇关于将基于C的dll添加到C#项目中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!