Pythonnet:Python调用动态链接库(DLL)

教程: Python如何调用动态链接库


目的


本教程涵盖Python调用.Net动态链接库的核心要点。

当前文档涵盖了如何导入DLL,但是并没有准确涵盖如何调用DLL内部函数。
同样的, 其他文档没有真正的涵盖如何这样做,它们只是建议你在自己的Python编译器中配置一次该库。本教程旨在为那些需要入门的Python开发人员架起一座桥梁,并提供一些示例代码,这些代码可以编译成DLL并直接进行测试以获得实际知识。

背景信息


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.

DLL计算源码
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;
        }
    }
}

在此需要注意4点:

  1. 命名空间: CalcTestNS
  2. 类名: calculate
  3. 方法名: Add Sub
  4. 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?

该流程包含4步:

  1. 确保Python路径所在目录下包含DLL,最简单的方式是修改 sys.path.
assembly_path = r"C:\Users\Administrator\Desktop\test\CalcTest\CalcTest\bin\Debug"

import sys
sys.path.append(assembly_path)
  1. 导入assembly. 注意不要在assembly名的末尾加尾缀 .dll, PythonNet会自动加上尾缀。
import clr
clr.AddReference("CalcTest")
  1. 命名空间作为模块名导入类和其他DLL的功能.
from CalcTestNS import calculate
  1. 尽情享用导入的功能。
ct = calculate()
print(ct.Add(1,1))

params = [1,2]
print(ct.Sub(*params))
Python源码
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))

源文件:How to call a dynamic library

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值