Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to call a dynamic library

Hugues Stefanski edited this pageJun 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 thectypes module.

Supporting Code

For this tutorial, I will be using a very simple C# library for adding and subtracting numbers, which was pulled from thistutorial 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

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceCalcTestNS{publicclasscalculate{publicintAdd(inta,intb){returna+b;}publicintSub(inta,intb){returna-b;}}}

There are 4 things to note here:

  1. The namespace:CalcTestNS
  2. The class name:calculate
  3. The method names:AddSub
  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 usingCalcTest.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 modifyingsys.path.
assembly_path=r"C:\Users\Administrator\Desktop\test\CalcTest\CalcTest\bin\Debug"importsyssys.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.
importclrclr.AddReference("CalcTest")
  1. Use the namespace as a module to import classes and other DLL goodies.
fromCalcTestNSimportcalculate
  1. Use the imported goodies.
ct=calculate()print(ct.Add(1,1))params= [1,2]print(ct.Sub(*params))

Complete Python Code

importclrimportsysassembly_path=r"C:\Users\Administrator\Desktop\test\CalcTest\CalcTest\bin\Debug"sys.path.append(assembly_path)clr.AddReference("CalcTest")fromCalcTestNSimportcalculatect=calculate()print(ct.Add(1,1))params= [1,2]print(ct.Sub(*params))
Clone this wiki locally

[8]ページ先頭

©2009-2025 Movatter.jp