-
Notifications
You must be signed in to change notification settings - Fork 747
How to call a dynamic library
This tutorial will cover the absolute essentials for how you can use a dynamic library with Python for .Net.
The current documentation covers how to import a DLL, but it doesn't cover exactly how to call the functions inside that DLL. Likewise, none of the tutorials online really cover how to do this either, they just expect you to know how to use the library once you have it loaded into your Python interpreter. This tutorial aims to bridge the gap for Python developers who need that first little bit of how-to in order to get started, and provides sample code that can be compiled into a DLL and tested against directly to gain hands-on knowledge.
In the .Net ecosystem, a DLL is known as a managed assembly. You can have unmanaged assemblies, which is the more traditional standalone DLLs that you get from compiling C/C++ code, but when you compile a .Net project, such as one written in C#, you'll get a managed assembly. This means that the DLL requires the .Net framework to be installed on the system that will be using the DLL.
Python for .Net is concerned exclusively with managed assemblies. You can integrate with unmanaged assemblies via the ctypes module.
For this tutorial, I will be using a very simple C# library for adding and subtracting numbers, which was pulled from this tutorial and modified slightly to emphasize what information goes where. If you want to build the DLL to try things hands-on, you can do so using the steps in that tutorial.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CalcTestNS
{
public class calculate
{
public int Add(int a, int b)
{
return a + b;
}
public int Sub(int a, int b)
{
return a - b;
}
}
}
There are 4 things to note here:
- The namespace:
CalcTestNS
- The class name:
calculate
- The method names:
Add
Sub
- The name of the DLL. This has nothing to do with the code, and is 100% based on how you compile things. You can rename the DLL that you get to whatever you want, but you'll need to know the filename so you can actually import it. In this case, I'm using
CalcTest.dll
.
There's nothing strange going on here, but you'll need to know all 4 pieces of information to actually be able to use the DLL.
With all of that out of the way, down to the real reason you're here: How the heck do I make an actual call into the DLL?
This is a 4 step process:
- Make sure that the directory containing the DLL is in your Python path. The easiest way to do this is by modifying
sys.path
.
assembly_path = r"C:\Users\Administrator\Desktop\test\CalcTest\CalcTest\bin\Debug"
import sys
sys.path.append(assembly_path)
- Import the assembly. Note that you don't have to append
.dll
to the end of the assembly name, Python for .Net does that for you automatically.
import clr
clr.AddReference("CalcTest")
- Use the namespace as a module to import classes and other DLL goodies.
from CalcTestNS import calculate
- Use the imported goodies.
ct = calculate()
print(ct.Add(1,1))
params = [1,2]
print(ct.Sub(*params))
import clr
import sys
assembly_path = r"C:\Users\Administrator\Desktop\test\CalcTest\CalcTest\bin\Debug"
sys.path.append(assembly_path)
clr.AddReference("CalcTest")
from CalcTestNS import calculate
ct = calculate()
print(ct.Add(1,1))
params = [1,2]
print(ct.Sub(*params))