我需要做什么才能从 C# 应用程序引用 c++ dll
What do I need to do reference a c++ dll from a C# application?
C++ dll 使用 Win32 读取和写入数据到串行端口。我的 C# 应用程序中需要这些数据。这仅仅是像引用用 C# 编写的任何其他 dll 一样引用 dll 的情况,导入它然后调用其中的方法吗?还是我需要做一些不同的事情?
如果此 DLL 不是 COM 库,则需要使用 PInvoke
基本上,DLL 导出的每个函数都需要使用所需的语法进行定义。这是从wininet访问函数InternetGetConnectedState所需的声明的示例.dll
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState( out int Description, int ReservedValue ) ;
声明之后,你可以通过这种方式从 C# 代码调用函数
public static bool IsConnectedToInternet( )
{
try
{
int Desc;
return InternetGetConnectedState(out Desc, 0);
}
catch
{
return false;
}
}
当然,您的 DLL 应该在您的应用程序中可见(相同的文件夹或路径)
您要查找的搜索词是 PInvoke。
实质上,您需要在 C# 类中声明引用外部C++实现的方法。
像这样的东西(来自 MSDN 示例):
class PlatformInvokeTest
{
[DllImport("msvcrt.dll")]
public static extern int puts(string c);
[DllImport("msvcrt.dll")]
internal static extern int _flushall();
public static void Main()
{
puts("Test");
_flushall();
}
}
相关文章:
- 如何在 64 位 vb.net Windows 应用程序中引用 32 位 dll
- 在Linux/Ubuntu中编译openCV应用程序时,如何解决未定义的引用cv:..?
- 在Qt应用程序中包含Python.h会导致对Qt函数的未定义引用
- Android (ART) 崩溃并显示错误 JNI 在应用程序中检测到错误:jstring 是无效的本地引用
- 错误:尝试在全新系统安装中构建应用程序后未定义对'...'的引用
- 由非托管 EXE 引用时托管 DLL 的应用程序配置
- 在 qt 控制台应用程序中未定义对 'vtable for myObj' 的引用 - 信号和插槽
- 构建应用程序时未定义的引用错误;
- 如何在 silverlight 应用程序中引用 C++/CLI 项目
- 无法使用 GCC 构建 C 应用程序,因为未定义对'deflateEnd'的引用
- 使用 Clang、libc++ 和 c++11 制作 SFML2 应用程序.对 SFML 库的未定义引用
- 引用Windows应用商店应用程序项目中的C++WinRT组件
- 我需要做什么才能从 C# 应用程序引用 c++ dll
- 如何从主应用程序从 GetInstance 获取引用
- 编译c++应用程序时出现未定义的引用错误
- MDI Qt 应用程序中的链接器错误:对 qInitResources_mdi() 的未定义引用
- 如何为c++代码创建一个dll(在树形程序中使用大量指针),以便在ASP. js中引用它.Net Web应用程序
- 多线程应用程序中对象的引用计数
- 交叉编译opencv应用程序,未定义引用
- 引用在依赖应用程序中声明的库中的变量