C ++ dll使用Win32从串行端口读取数据并将数据写入串行端口。我的C#应用​​程序中需要这些数据。是否只是像我用C#编写的其他任何dll一样引用dll,导入该dll并在其中调用方法的情况?还是我需要做些不同的事情?

最佳答案

如果此DLL不是COM库,则需要使用PInvoke

基本上,DLL导出的每个函数都需要使用所需的语法进行定义。
这是从wininet.dll访问功能InternetGetConnectedState所需的声明的示例

[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应该在您的应用程序中可见(相同的文件夹或路径)

关于c# - 我需要做什么来从C#应用程序引用C++ dll?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10132043/

10-11 22:50
查看更多