python与C++交互
python和C++能进行有效的交互,c++调用Python的一些小用法
写了一个python脚本导入发生异常,可能是编码问题(如存在中文),
Python默认的是ASCII
可加上:
#!/usr/bin/python
# -*- coding: utf-8 -*-
参见:https://www.python.org/dev/peps/pep-0263/
定义类C数据结构:
class Point(Structure):
_pack_ = 1
_fields_ = [('x', c_uint8),
('y', c_uint8),
]
定义点数组初始化:
pointList = [Point(0,0),
Point(1,1),
]
访问:pointList[i].x, pointList[i].y
也可强制转换为指针访问
pPoint = cast(addressof(point),POINTER(Point))
指针有一个属性contents为实例对象
pPoint.contents.x, pPoint.contents.y
想要定义数组:
class Array(Structure):
_pack_ = 1
_fields_ = [('iArray', c_uint8 * 5),
('bArray', c_ubyte * 5),
]
定义缓冲区:
buf = c_buffer("Hello",520)
buf = create_string_buffer("Hello", 10)
第二个是新一点的,旧的也可以用
ctypes的数据类型:https://docs.python.org/2/library/ctypes.html#ctypes.c_ubyte
addressof(obj): 获取ctypes类型地址
byref(obj, offset):
返回ctypes类型轻量级指针
对应以下C代码:(((char *)&obj) + offset)
sizeof: 计算变量和类型的字节数
调用c库的一些函数:
libc = cdll.msvcrt
fopen = libc.fopen
fwrite = libc.fwrite
fclose = libc.fclose
fseek = libc.fseek
fread = libc.fread
ftell = libc.ftell
memset = libc.memset
memcpy = libc.memcpy
可获取函数地址,调用与C一样,注意这些函数操作需要地址
可以用addressof获取ctypes类型的地址
接收C++传来的参数:
如果传过来的是Unicode字符需要转换为python字符串,否则会出现意想不到的错误
虽然用打印和写入文件的方式写入param内容是正确的,但是实际调用libc库时仍会
找不到路径
str(unicode(param))
使用python时如果更加了解其内在机制,会对使用该语言更有帮助
下面上代码:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os, sys
from ctypes import * class CPoint(Structure):
_pack_ = 1
_fields_ = [('x', c_uint8),
('y', c_uint8),
] class CArray(Structure):
_pack_ = 1
_fields_ = [('iArray', c_uint8 * 5),
('bArray', c_char * 5),
] def EditBuf(buf, len):
tmp = buf
i = 0
val = c_char('')
while i < len:
memmove(addressof(tmp)+i,addressof(val),sizeof(val))#addressof(tmp)+i相当于指针移位
i += 1 libc = cdll.msvcrt
fopen = libc.fopen
fwrite = libc.fwrite
fclose = libc.fclose
fseek = libc.fseek
fread = libc.fread
ftell = libc.ftell
memset = libc.memset
memcpy = libc.memcpy if __name__ == '__main__':
point = CPoint(5,6)
pPoint = cast(addressof(point),POINTER(CPoint))
print "point.x = %d , point.y = %d" %(point.x, point.y)
print "pPoint.x = %d , pPoint.y = %d" %(pPoint.contents.x, pPoint.contents.y)
print point, pPoint.contents#这里两个地址值不一样说明系统为pPoint新申请了一个Point保存复制的值 pointList = [CPoint(0,1),
CPoint(2,3)
]
print pointList[0].x, pointList[0].y#数组 iArr = (c_uint8 * 5)(5,6,7,8)
bArr = (c_char * 5)('a','b','c')
print bArr[:]
cArray = CArray(iArr, bArr[:])#c_uint8和c_char赋初始值的方式不同,具体还没研究
print cArray.iArray[:]
print cArray.bArray[:] buf = c_buffer("",8)#类似c_char*8
print buf.value
EditBuf(buf,8)#修改buf值,对内存修改
print buf.value
memmove(addressof(buf),byref(c_char('a')),sizeof(c_char))
print buf.value #C库函数调用
fp =fopen("D:\\pythonCallClib.txt","wb+")
fwrite(buf,1,sizeof(buf),fp)#
fseek(fp,3,0)
fwrite(addressof(c_char('')),1,1,fp)
fclose(fp) #sizeof
print "c_uint8 = %d bytes, c_int = %d bytes, buf = %d bytes" %(sizeof(c_uint8), sizeof(c_int), sizeof(buf)) #cast 可以将buf强制转换成我们自定义的类型
buf = create_string_buffer("abc",8)
p = cast(addressof(buf),POINTER(CPoint))
print p.contents.x, p.contents.y
python与C++交互的更多相关文章
- Python和Excel交互
Python和Excel交互 使用的python包为XlsxWriter 下载的链接 https://pypi.python.org/pypi/XlsxWriter 初级的例子: def write_ ...
- 【转】Python之系统交互(subprocess)
[转]Python之系统交互(subprocess) 本节内容 os与commands模块 subprocess模块 subprocess.Popen类 总结 我们几乎可以在任何操作系统上通过命令行指 ...
- Python与Mysql交互
#转载请联系 在写内容之前,先放一张图,bling- 这张图算是比较详细的表达出了web开发都需要什么.用户访问网页,就是访问服务器的网页文件.这些网页文件由前端工程师编写的.服务器通常用nginx/ ...
- Python实现用户交互,显示省市县三级联动的选择
题目:Python实现用户交互,显示省市县三级联动的选择 定义的字典为: dic = { "江西": { "萍乡": ["安源", &quo ...
- 二十、Python与Mysql交互
先安装一个python与MySQL交互的包:MySQL-python $ gunzip MySQL-python-1.2.2.tar.gz $ tar -xvf MySQL-python-1.2.2. ...
- Python之系统交互(subprocess)
本节内容 os与commands模块 subprocess模块 subprocess.Popen类 总结 我们几乎可以在任何操作系统上通过命令行指令与操作系统进行交互,比如Linux平台下的shell ...
- Python的用户交互程序及格式化输出
1. 用户输入 在Python 3 中,用户输入用input()函数即可实现用户交互程序. 例如,我们根据程序提示输入用户名和密码,并且打印输入的信息. 2. 字符串格式化输出 例如,我们根据程序提 ...
- Python与RabbitMQ交互
RabbitMQ 消息队列 成熟的中间件RabbitMQ.ZeroMQ.ActiveMQ等等 RabbitMQ使用erlang语言开发,使用RabbitMQ前要安装erlang语言 RabbitMQ允 ...
- python与mysql交互中的各种坑
开始学python 交互MySQLdb,踩了很多坑 第一个 %d format: a number is required, not str 参照以下博客: https://blog.csdn.net ...
随机推荐
- Url通配符映射
原文:http://www.cnblogs.com/liukemng/p/3726897.ht 1.URL路径映射 1.1.对一个action配置多个URL映射: 我们把上一篇中的HelloWorld ...
- Linux opencv安装与编译
参考http://blog.csdn.net/solomon1558/article/details/51967280 1安装cmake以及依赖库 $ sudo apt-get install cma ...
- 为什么使用spring
现在基本的项目都会用到spring框架,那么我们为什么要使用spring呢?下面为大家总结一下,希望大家指正. spring是一个轻量级的容器框架,其核心是IOC(控制反转也叫依赖注入)和AOP(面向 ...
- Linux Crontab 安装使用详细说明
crontab命 令常见于Unix和Linux的操作系统之中,用于设置周期性被执行的指令.该命令从标准输入设备读取指令,并将其存放于“crontab”文件中,以供 之后读取和执行.通常,crontab ...
- Windows Phone 四、控件模版
控件模版的概念 Windows Phone中每一个控件都有一个默认的模版,用于描述控件的内部组成结构和外观样式 相对于原本的样式外观操作,自定义模版的可自定义性更强 最基本的重写控件模版 <Gr ...
- swift基础:第二部分:函数和闭包
今天本来想利用上午的时间本来打算将swift基础部分学习完的,不巧的是,后台来和我讨论用户评价的接口,讨论过后,商讨出一种可行的方案,十几分钟时间过去了,我拿到将接口介入到已经完成的页面中,完美,终于 ...
- linux 连接到阿里云服务器
当Windows拥有xshell软件可以连接到你的远程服务器时,Linux其实自己带有的ssh就可以连接: 具体命令是: ssh root@60.2.5.201.81然后输入你服务器的密码:××××× ...
- LDA( Latent Dirichlet Allocation)主题模型 学习报告
1 问题描述 LDA由Blei, David M..Ng, Andrew Y..Jordan于2003年提出,是一种主题模型,它可以将文档集中每篇文档的主题以概率分布的形式给出,从而通过分析一 ...
- 浅述WinForm多线程编程与Control.Invoke的应用
VS2008.C#3.0在WinForm开发中,我们通常不希望当窗体上点了某个按钮执行某个业务的时候,窗体就被卡死了,直到该业务执行完毕后才缓过来.一个最直接的方法便是使用多线程.多线程编程的方式在W ...
- .Net Core[译文]
新文档 /* GitHub stylesheet for MarkdownPad (http://markdownpad.com) */ /* Author: Nicolas Hery - http: ...