Python有类似于.NET C# PInvoke的东西吗?

3

CPython是否有类似于.net c# PInvoke的功能?

例如,我有一个some.dll或some.so文件,并希望在运行时注册其中的某些函数并开始在我的程序中使用它们。

在C#中,您可以这样做:

// Marshal.cs
using System;
using System.Runtime.InteropServices;

namespace CSharpIterators
{
    class MainClass
    {
        [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 (string[] args)
        {

            puts("Hello World!");
            _flushall();
        }
    }
}

Python有类似的功能吗?

提前致谢!

2个回答

6

您可以使用Python的ctypes模块从DLL中调用函数。


0

另一个选择是使用诸如swig之类的工具,以创建Python接口到C/C++代码: http://www.swig.org/

创建包装器的语法与C语言非常相似,只需添加一些预处理指令。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接