Calling a C# library from python

柔情痞子 提交于 2019-11-26 12:08:02

It is actually pretty easy. Just use NuGet to add the "UnmanagedExports" package to your .Net project. See https://sites.google.com/site/robertgiesecke/Home/uploads/unmanagedexports for details.

You can then export directly, without having to do a COM layer. Here is the sample C# code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using RGiesecke.DllExport;

class Test
{
    [DllExport("add", CallingConvention = CallingConvention.Cdecl)]
    public static int TestExport(int left, int right)
    {
        return left + right;
    }
}

You can then load the dll and call the exposed methods in Python (works for 2.7)

import ctypes
a = ctypes.cdll.LoadLibrary(source)
a.add(3, 5)

Since your post is tagged IronPython, if you want to use the sample C# the following should work.

import clr
clr.AddReference('assembly name here')
from DataViewerLibrary import PlotData 

p = PlotData()
p.Start()
NickSuperb

Python for .Net (pythonnet) may be a reasonable alternative to IronPython in your situation. https://github.com/pythonnet/pythonnet/blob/master/README.md

From the site:

Note that this package does not implement Python as a first-class CLR language - it does not produce managed code (IL) from Python code. Rather, it is an integration of the CPython engine with the .NET runtime. This approach allows you to use use CLR services and continue to use existing Python code and C-based extensions while maintaining native execution speeds for Python code.

Also

Python for .NET uses the PYTHONPATH (sys.path) to look for assemblies to load, in addition to the usual application base and the GAC. To ensure that you can implicitly import an assembly, put the directory containing the assembly in sys.path.

This package still requires that you have a local CPython runtime on your machine. See the full Readme for more info http://pythonnet.github.io/readme.html

This project has been developed for that exact purpose - use C# classes in regular Python

https://bitbucket.org/pydotnet/pydotnet/wiki/Home

All you need to do is to install either MSI or EGG into your CPython. PyDotnet is Python module, so the executable stays regular python.exe from your installation of Python or Anaconda. Supported both 32bit and 64bit.

Unlimited access to all C# classes, methods with output and ref parameters, generic classes and generic methods, extension methods, private members.

Overloaded assembly loader with customized mechanics for searching assemblies.

.NET runtime type information convertible to class object, which can be instantiated as any other class.

Special import mode designed especially for Python interactive shell, which allows you to discover available assemblies, namespaces, classes, methods, etc.

I'm waiting for feedback:)

I am not a .NET expert, but you code looks as if your method is exposed as a COM object. So you can try http://starship.python.net/crew/mhammond/win32/ package to access it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!