Skip to content

How to call a dynamic library

Hugues Stefanski edited this page Jun 2, 2020 · 3 revisions

Tutorial: How To Call A Dynamic Library

Purpose

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.

Background Information

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.

Supporting Code

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.

Calculation DLL Code

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:

  1. The namespace: CalcTestNS
  2. The class name: calculate
  3. The method names: Add Sub
  4. 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.

The Good Stuff

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:

  1. 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)
  1. 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")
  1. Use the namespace as a module to import classes and other DLL goodies.
from CalcTestNS import calculate
  1. Use the imported goodies.
ct = calculate()
print(ct.Add(1,1))

params = [1,2]
print(ct.Sub(*params))

Complete Python Code

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))