将基于C的dll添加到C#项目中 [英] Adding C based dll to C# project

查看:78
本文介绍了将基于C的dll添加到C#项目中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!
$


我要在我的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?

谢谢!

推荐答案

您可以查看以下文档,其中包含有关h的示例要调用C中的dll用于C#代码:

You can take a look at the following document which contains examples about how to call dll made in C to be used in C# code :

从C#调用C库DLL

并关注是关于这个主题的好文章:

and following is a good article on this topic:

如何在C中创建DLL库,然后将其与C#一起使用

这里也是一个不错的选择发布如何执行此操作:

and here is also a good post on how to do this:

https://stackoverflow.com/a/315064/ 1875256

以及PInvoke用于在C#中调用未托管代码的另一个资源:

and one more resource for PInvoke to call the un-managed code in C#:

https://www.pinvoke.net/index.aspx

希望它有所帮助。


这篇关于将基于C的dll添加到C#项目中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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