开发者

Does Python have something similar to .net c# PInvoke?

Do CPython has something similar to .n开发者_如何学运维et c# PInvoke?

For example I have some.dll or some.so and would like to register some functions from it on runtime and start using those in my program.

In C# you can do:

// 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();
        }
    }
}

Do python has similar functionality?

Thanks in advance!


You can call functions from DLLs using Python's ctypes module.


Another option is to use tools like swig in order to create a Python interface to C/C++ code: http://www.swig.org/

The syntax for creating the wrappers is very similar to C plus some preprocessing directives.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜