SoFunction
Updated on 2024-11-16

The simplest way to call C# dll library with Python

1.Why cross-platform programming? Dual-platform programming or multi-platform programming is just a means to provide better development of more compatible solutions, programming to serve the product and the customer, but also to adapt to local conditions.

First, install the library clr , which I have installed here, and check the corresponding root directory for the folder pythonnet.

2. First of all, pay special attention to the version of Python you install and use is corresponding to x64 or x32, python can only call the corresponding 64-bit or 32-bit dll, you can right-click on the properties of the startup project, modify and regenerate.

Special note: the startup project is not the same as your target library, the output type of the former is windows application or console, the output type of the latter is class library, this is the type of dll generated.

3. Again, right-click on the properties of the startup project and select the dll you want under bin\Debug.

in the reference to the dynamic link library and puts the dll file in the same directory as the .py file. The third line of code is a declaration to call all classes, properties, methods, etc. under its space.

import clr # add C# suppor
('In3Controllers')

from In3Controllers import *

5. This Testor is the code I wrote in C#, generated by the dynamic link library inside a function. Here has completed all the steps to refer to the C# dll library.

if __name__ == '__main__':

  smctestor = Testor(setting, param)            # place here so it can only init once

This is the whole content of this article.