15

How can we export C# methods?

I have a dll and I want to use its methods in the Python language with the ctypes module. Because I need to use the ctypes module, I need to export the C# methods for them to be visible in Python.

So, how can I export the C# methods (like they do in C++)?

denfromufa
  • 5,610
  • 13
  • 81
  • 138
aF.
  • 64,980
  • 43
  • 135
  • 198
  • python.net allows bi-directional interop between Python and .NET using Python C-API and .NET Pinvoke and Unmanaged Exports, mentioned below. So all the hard pieces are written for you! http://pythonnet.github.io/ – denfromufa Sep 08 '16 at 04:29

4 Answers4

22

Contrary to popular belief, this is possible.
See here.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 1
    I used the dllexp.exe to see the exported methods from the C# dll that the template creates and it didn't happear the method.. Is there a way for the C# dll methods to be visible externally in a program like dllexp.exe? – aF. Jan 18 '10 at 11:51
1

With the normal Python implementation ("CPython"), you can't, at least not directly.

You could write native C wrappers around our C# methods using C++/CLI, and call these wrappers from Python.

Or, you could try IronPython. This lets you run Python code and call code in any .Net language, including C#.

oefe
  • 19,298
  • 7
  • 47
  • 66
  • 1
    I want to use Python itself and not an extra implementation of it like IronPython. – aF. Jan 17 '10 at 19:05
1

(This may no longer be relevant since SLaks has found that ingenious link, but I'll leave an edited version for reference...)

The "normal" way of exposing .NET/C# objects to unmanaged code (like Python) is to create a COM-callable wrapper for the C# DLL (.NET assembly), and call that using Python's COM/OLE support. To create the COM-callable wrapper, use the tlbexp and/or regasm command-line utilities.

Obviously, however, this does not provide the C/DLL-style API that SLaks' link does.

itowlson
  • 73,686
  • 17
  • 161
  • 157
1

That's not possible. If you need DLL exports you'll need to use the C++/CLI language. For example:

public ref class Class1 {
public:
  static int add(int a, int b) {
      return a + b;
  }
};

extern "C" __declspec(dllexport) 
int add(int a, int b) {
  return Class1::add(a, b);
}

The class can be written in C# as well. The C++/CLI compiler emits a special thunk for the export that ensures that the CLR is loaded and execution switches to managed mode. This is not exactly fast.

Writing [ComVisible(true)] code in C# is another possibility.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 1
    Actually, it is possible. See my answer. – SLaks Jan 17 '10 at 19:13
  • 1
    Meh, neither the article nor the downloadable source code explains how it works. I'll wait for confirmation from the OP. – Hans Passant Jan 17 '10 at 21:19
  • I had to build a wrapper c++ dll (of the c# dll) to properly use the ctypes module! – aF. Jul 28 '11 at 08:50
  • And there we have it. – Hans Passant Dec 03 '13 at 17:09
  • @HansPassant you seem to downplay Unmanaged Exports in all reverse pinvoke threads on SO. But does C++/CLI have a "downloadable source code"? When was the last time C++ features were added to C++/CLI? UE is licensed MIT and ILSPY is pretty good at providing C# code for it. – denfromufa Sep 08 '16 at 04:32
  • I certainly do, it is the worst possible solution. It is not like it can't work, it is because nobody can ever figure out what to do what the code fails. Not the user of the code either, there is no reasonable diagnostic. Bam, dead. Why the author doesn't open-source his code so it can be improved is very mysterious as well. Well, he doesn't have to, just like he doesn't have to provide support. – Hans Passant Sep 08 '16 at 04:45
  • @HansPassant the unmanaged exports code is now actively maintained on github: https://github.com/3F/DllExport – denfromufa Dec 21 '18 at 22:26