python调用.net动态库
# python调用.net动态库
### pythonnet简介
-----------------------------
- pythonnet是cpython的扩展
- pythonnet提供了cpython和.net程序集之间交互的桥梁
- pythonnet开源在github上
### pythonnet安装
-----------------------------
- 通过`pip install pythonnet`安装
### pythonnet的使用帮助
-----------------------------
- pythonnet的使用帮助,请参见github.
### pythonnet中的坑
-----------------------------
- cpython是分32和64位的,对应的pythonnet也是分的,版本要对应好
- pythonnet最核心的就是python.Runtime.dll动态库,这个库是c#编写的实现了两种语言的交互
### ref类型的参数如何返回
-----------------------------
- 返回值的第一个元素是c#的返回值
- 返回值的第二个元素就是ref的值了,ref String[] 对应的返回值第二个元素就是元组tuple
### 如何加载动态库
-----------------------------
```
# clr是公共运行时环境,该模块是与c#交互的核心
import clr
import sys
# 导入clr时这个模块最好也一起导入,这样可用使用AddReference()方法
import System
# input()
from System import Array
from System import String
# 打印当前.net运行时的版本
print(System.Environment.Version)
# 打印当前的环境变量
print('---------------------')
for p in sys.path:
print(p)
print('---------------------')
# 加载动态,不要带动态库的后缀名,如果d=None,表示动态库没有找到
d = clr.FindAssembly('YctxKj.Card.Interpreter')
print(d)
# from后面跟的是动态库的名字,不是命名空间
from YctxKj.Card.Interpreter import *
d = clr.FindAssembly('YctxKj.Card.CardReader')
print(d)
from YctxKj.Card import CardReader
# 打印程序集,如果动态库加载成功,程序集里就会含有动态库的程序集
lt = clr.ListAssemblies(False)
for i in range(lt.Length):
print('%d = %s' % (i,lt[i]))
ret = 0
# 程序调用业务代码
_reader = CardReader()
_Interpreter = ScriptInterpreter()
_Interpreter._reader = _reader
_Interpreter.Init();
ret = _Interpreter.SetKeyMode(305)
print('ret=%d' % ret)
arCarder = ''
# input() 可以程序暂停,然后使用vs附加进程python.exe调试
# input()
ret = _reader.ListReader(arCarder);
print('ret = %d' % ret[0])
print('ref param string[] length = %d' % ret[1].Length)
if ret[1].Length > 0:
items = ret[1]
for t in items:
print(t)
else:
print('don\'t find cardreader dev')
exit()
# 链接读卡器
strReader = items[1]
strATR = ''
nRet = _reader.Connect(strReader, strATR)
if nRet[0] != 0:
print('connect card fail, nRet=%d' % nRet[0])
_reader.Beep()
_reader.Beep()
exit()
print('crd connect successed,atr=%s' % nRet[1])
_reader.Beep()
# 断开读卡器
_reader.Disconnect()
```
### 参考文章
-----------------------------
- [python与c#的交互模块pythonnet](https://www.cnblogs.com/tester-zhenghan/p/5406521.html)
- [pythonnet主页](https://github.com/pythonnet/pythonnet)
python调用.net动态库的更多相关文章
- python 调用 C 动态库
首先是 C 的头文件和源文件, #ifndef POINT_H #define POINT_H struct point { int x; int y; }; void point_print(str ...
- 使用ctypes在Python中调用C++动态库
使用ctypes在Python中调用C++动态库 入门操作 使用ctypes库可以直接调用C语言编写的动态库,而如果是调用C++编写的动态库,需要使用extern关键字对动态库的函数进行声明: #in ...
- c#调用c++动态库的一些理解
调用c++动态库一般我们这样写 [DllImport("UCamer.dll", CallingConvention = CallingConvention.Winapi)] ...
- C#调用C++动态库(dll)
在实际软件开发过程中,由于公司使用了多种语言开发,在C#中可能需要实现某个功能,而该功能可能用其他语言已经实现了,那么我们可以调用其他语言写好的模块吗?还有就是,由于C#开发好的项目,我们可以利用re ...
- C#总结(四)调用C++动态库
由于公司很多底层的SDK,都是C++开发,上层的应用软件却是C# Winform程序.在实际工作的过程中,就经常碰到了C# 程序调用C++ 动态库的问题.最近一直在和C++ 打交道,C# 怎么调用C+ ...
- Java调用dll动态库
最近项目里使用java调用dll动态库,因此研究了一下这方面的东西. 使用的工具包如下 <dependency> <groupId>net.java.dev.jna</g ...
- 【C#】 使用Gsof.Native 动态调用 C动态库
[C#] 使用Gsof.Native 动态调用 C动态库 一.背景 使用C# 开发客户端时候,我们经常会调用一些标准的动态库或是C的类库.虽然C# 提供的PInvoke的方式,但因为使用的场景的多变, ...
- Delphi XE7调用C++动态库出现乱码问题回顾
事情源于有个客户需使用我们C++的中间件动态库来跟设备连接通讯,但是传入以及传出的字符串指针格式都不正确(出现乱码或是被截断),估计是字符编码的问题导致.以下是解决问题的过程: 我们C++中间件动态库 ...
- C# 调用C++动态库注意事项
C# 调用C++动态库注意事项 最近项目上需要在C#中调用C++,期间遇到不少坑,总结如下: 1.in const char* 对应C#中string 或 IntPtr 2.out const ...
随机推荐
- TensorFlow 基本概念
一.概述 使用图(graph)来表示计算任务 在会话(Session)的上下文(context)中执行图(graph) 使用tensor表示数据 通过 变量(Variable)维护状态 使用 feed ...
- android 4.4 支持透明状态栏和透明导航栏
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceSt ...
- FreeSWITCH在会议室中持续播放音频文件
最近遇到一个客户需求,希望在会议室建立起来后,自动播放一段指定的声音. 已知会议室命令,假设建立起一个会议室号码3000,很容易实现以下功能: 一.播放一个声音文件一次 conference 3000 ...
- appium 重新启动apk
在旧版本的appium,重新启动apk,调用startActivity方法可以随意启动一个app,并传入一个package name 和启动activity name的名称.语句如下: driver. ...
- unity, Animator.ResetTrigger
解: 正确的写法应该是:Animator.SetTrigger("unfoldTrigger")Animator.ResetTrigger("unfoldTrigger& ...
- Spring+Mybatis整合过程中找不到.properties文件
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' ...
- UIButton 标题靠右
_classBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _classBtn.frame = CGRectMake( kDeviceWid ...
- 腾讯云服务器 - 安装redis3.2.9以及集群
redis大家都知道,服务器上必不可少的,那么在生产环境下安装的步骤和虚拟机里也是差不多的 官网上最新稳定版是3.2.9,而4.0的更新比较大,但是比几个还是beta版嘛 下载并且上传压缩包至云服务器 ...
- Mac OSX 快捷键&命令行
一.Mac OSX 快捷键 ctrl+shift 快速放大dock的图标会暂时放大,而如果你开启了dock放大Command+Op ...
- ios页面间跳转方式总结
转自:http://www.cnblogs.com/anywherego/p/3542202.html 下面以OldViewController(oldC)的按钮btn点击后跳转到NewViewCon ...