37

how can I load a c# dll in python?

Do I have to put some extra code in the c# files? (like export in c++ files)

I don't want to use IronPython. I want to import a module to Python!

denfromufa
  • 5,610
  • 13
  • 81
  • 138
aF.
  • 64,980
  • 43
  • 135
  • 198
  • You might want to add a bit of clarification. Are we talking about *CPython* or *IronPython*? – Jason Baker Jan 16 '10 at 15:40
  • I want to import a module in Python (not IronPython then..) – aF. Jan 16 '10 at 16:11
  • You're going to struggle. A C# DLL is written in CIL and is not python, nor is it readable by python. You are going to either go full out .NET and use IronPython or suffer the pain of using COM. Isn't there some way you could use a native python module? – Callum Rogers Jan 16 '10 at 16:15
  • 3
    With the help of stack overflow I've done it. I've turned my C# dll into a COM InterOp DLL and then use it in a C++ DLL. After that I used ctypes to call the C++ DLL functions :) – aF. Jan 19 '10 at 22:28

5 Answers5

36

The package Python for.NET and the Python Implementation IronPython now work the same way.

Example for a C# DLL MyDll.dll:

import clr
clr.AddReference('MyDll')
from MyNamespace import MyClass
my_instance = MyClass()

See this post for more details.

Manuel
  • 534
  • 3
  • 9
Vincent
  • 12,919
  • 1
  • 42
  • 64
9

This is to answer the second part of your Question Try making the DLL COM visible.

by using the

[ComVisible(true)]

Ok IronPython is a .net implemenatation of the Python language The technology is going to use the DLR of the .net 4.0 when it arrives so IronPython will have more Dynamism (is that a word). (In english if you're a Python guru, you'll feel more at home when you use IronPython)

So you may well choose IronPython, if you do that you can skip the COM visible part. Since both (C# , Iron Python) are under .Net

http://ironpython.net/

Manuel
  • 534
  • 3
  • 9
Vivek Bernard
  • 2,063
  • 3
  • 26
  • 43
  • 1
    Now that I've saw Ironpython I understood what it was. I don't want that. I want to import a module in python :P – aF. Jan 16 '10 at 16:10
  • You mean Import (port) C# code to Python or Import a C# dll to Python – Vivek Bernard Jan 16 '10 at 16:21
  • Import and use functions from a C# Dll in Python – aF. Jan 16 '10 at 16:26
  • I guess this solution is not available before .net 4.0? I tried with .net 2.0 but there is no option ComVisible – Georg W. Apr 05 '18 at 09:56
  • 1
    You've selected the (perfectly good) answer using Iron Python as the correct answer but also said you don't want that. I have the same requirement as you so your correct answer is not helpful. – Ant Jan 14 '22 at 13:12
6

If you do not want to use solutions like Python .NET or IronPython it is possible to implement a C++/CLI wrapper and use Pythons ctypes in order to load it. For example:

The C++/CLI library CallCSharp:

extern "C" {
    __declspec(dllexport) void foo()
    {
        // here you could use managed and unmanaged code
        Console.WriteLine("Hello, C# world...");
    }

The Python script:

from ctypes import cdll
lib = cdll.LoadLibrary("./CallCSharp.dll")
lib.foo()

More examples for the use of ctypes can be found here.

I strongly recomment reading this blog: http://pragmateek.com/if-your-plumbing-doesnt-work-youre-just-not-using-enough-pipes/#more-1745

It also handles the issue that arises when the C++/CLI wrapper calls code that is in another assembly (you'd get something like a WindowsError: [Error -532462766] Windows Error 0xE0434352 from your Python script then).

Matt
  • 25,467
  • 18
  • 120
  • 187
anhoppe
  • 4,287
  • 3
  • 46
  • 58
5

Python for .NET works well if you don't want to use IronPython.

user1251007
  • 15,891
  • 14
  • 50
  • 76
Warren
  • 59
  • 1
  • 2
0

If you want more explanation , go through with the youtube video (https://www.youtube.com/watch?v=lnsTJRY1jPU) where he has explained nicely.

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/34522231) – Alez Jun 13 '23 at 10:52