# 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动态库的更多相关文章

  1. python 调用 C 动态库

    首先是 C 的头文件和源文件, #ifndef POINT_H #define POINT_H struct point { int x; int y; }; void point_print(str ...

  2. 使用ctypes在Python中调用C++动态库

    使用ctypes在Python中调用C++动态库 入门操作 使用ctypes库可以直接调用C语言编写的动态库,而如果是调用C++编写的动态库,需要使用extern关键字对动态库的函数进行声明: #in ...

  3. c#调用c++动态库的一些理解

    调用c++动态库一般我们这样写   [DllImport("UCamer.dll", CallingConvention = CallingConvention.Winapi)] ...

  4. C#调用C++动态库(dll)

    在实际软件开发过程中,由于公司使用了多种语言开发,在C#中可能需要实现某个功能,而该功能可能用其他语言已经实现了,那么我们可以调用其他语言写好的模块吗?还有就是,由于C#开发好的项目,我们可以利用re ...

  5. C#总结(四)调用C++动态库

    由于公司很多底层的SDK,都是C++开发,上层的应用软件却是C# Winform程序.在实际工作的过程中,就经常碰到了C# 程序调用C++ 动态库的问题.最近一直在和C++ 打交道,C# 怎么调用C+ ...

  6. Java调用dll动态库

    最近项目里使用java调用dll动态库,因此研究了一下这方面的东西. 使用的工具包如下 <dependency> <groupId>net.java.dev.jna</g ...

  7. 【C#】 使用Gsof.Native 动态调用 C动态库

    [C#] 使用Gsof.Native 动态调用 C动态库 一.背景 使用C# 开发客户端时候,我们经常会调用一些标准的动态库或是C的类库.虽然C# 提供的PInvoke的方式,但因为使用的场景的多变, ...

  8. Delphi XE7调用C++动态库出现乱码问题回顾

    事情源于有个客户需使用我们C++的中间件动态库来跟设备连接通讯,但是传入以及传出的字符串指针格式都不正确(出现乱码或是被截断),估计是字符编码的问题导致.以下是解决问题的过程: 我们C++中间件动态库 ...

  9. C# 调用C++动态库注意事项

    C# 调用C++动态库注意事项 最近项目上需要在C#中调用C++,期间遇到不少坑,总结如下: 1.in const char*   对应C#中string 或  IntPtr 2.out const ...

随机推荐

  1. B. Eight Point Sets

    B. Eight Point Sets http://codeforces.com/contest/334/problem/B   time limit per test 1 second memor ...

  2. Vue.js 添加组件

    <!DOCTYPE HTML> <html> <head> <title>vue.js hello world</title> <sc ...

  3. 【MySQL】MySQL的索引

    索引是存放在模式中的一个数据库对象,虽然索引总是从属于数据表,但它也和数据表一样属于数据库对象.创建索引的唯一作用就是加速对表的查询,索引通过使用快速路径访问方法来快速定位数据,从而减少了磁盘的I/O ...

  4. Linux安装ElasticSearch-2.2.0-分词器插件(Mmseg)

    1.在gitpub上搜索elasticsearch-analysis,能够看到所有elasticsearch的分词器: 2.安装Mmseg分词器:https://github.com/medcl/el ...

  5. 安卓Textview的getLineCount返回0

    ViewTreeObserver observer = tv.getViewTreeObserver(); // textAbstract为TextView控件 observer.addOnGloba ...

  6. 正则和xpath在网页中匹配字段的效率比较

    1. 测试页面是  https://www.hao123.com/,这个是百度的导航 2. 为了避免网络请求带来的差异,我们把网页下载下来,命名为html,不粘贴其代码. 3.测试办法: 我们在页面中 ...

  7. Oracle 11g安装,新建表空间和用户

    windows版: 官网下载,两个压缩包,总共2G,解压到同一个文件夹下,setup.exe双击执行,首次安装的话,一直下一步就可以了,最后一步可能时间比较长,等着就好了,一般性能的机器大概要半小时左 ...

  8. 微信小程序 weui 使用方法

      https://github.com/Tencent/weui-wxss/ 下载地址用于小程序的https://github.com/Tencent/weui   下载地址用于H5    运用示例 ...

  9. PHP读取超大日志文件

    打开一个17G的日志文件,都不吃力,除了占cpu之外,内存占用不多,如果直接fopen根本打不开 注:它是逐行读取的 foreach( glob( ngx_log. "/*.log" ...

  10. RecyclerView中实现headerView,footerView功能

    之前用com.bartoszlipinski.recyclerviewheader.RecyclerViewHeader 不过局限性有点大. (com.bartoszlipinski.recycler ...