c++和c#库和类

C++ and C# libraries and Classes

本文关键字:c++      更新时间:2023-10-16

我可以使用c++库和类来构建c#语言的程序吗?

您可以使用P/Invoke从托管代码调用非托管代码。下面是调用非托管puts函数的示例:

using System;
using System.Runtime.InteropServices;
class Program
{
    [DllImport("msvcrt.dll")]
    public static extern int puts([MarshalAs(UnmanagedType.LPStr)] string m);
    [DllImport("msvcrt.dll")]
    internal static extern int _flushall();
    public static void Main() 
    {
        puts("Hello World!");
        _flushall();
    }
}
这里的思想包括声明一个托管包装器,该包装器将与您想要调用的非托管方法的签名相匹配。请注意,该方法是如何用extern关键字标记的,并使用DllImport属性进行装饰,以指示它在哪里实现的。

你当然可以!

如果你有一个预先存在的库,@Darin引用的机制可以自动完成。与www.swig.org

相关文章:
  • 没有找到相关文章