Python使用ctypes库调用外部DLL

上传人:m**** 文档编号:489308200 上传时间:2022-11-30 格式:DOC 页数:11 大小:106KB
返回 下载 相关 举报
Python使用ctypes库调用外部DLL_第1页
第1页 / 共11页
Python使用ctypes库调用外部DLL_第2页
第2页 / 共11页
Python使用ctypes库调用外部DLL_第3页
第3页 / 共11页
Python使用ctypes库调用外部DLL_第4页
第4页 / 共11页
Python使用ctypes库调用外部DLL_第5页
第5页 / 共11页
点击查看更多>>
资源描述

《Python使用ctypes库调用外部DLL》由会员分享,可在线阅读,更多相关《Python使用ctypes库调用外部DLL(11页珍藏版)》请在金锄头文库上搜索。

1、Python:使用ctypes 库调用外部DLL一、Python 之 ctypesctypes是Python的一个夕陪B库,提供和C语言兼容的数据类型,可以很方便地调用C DLL中的函数。在Python2.5官方安装包都带有ctypes 1.1版。ctypes的官方文档在这里。ctypes的使用非常简明,如调用cdecl方式的DLL只需这样:view sourcepri nt?1 from ctypes import *;2 h=CDLL(msvcrt.dll)3 h.pri ntf(a=%d,b=%d,a+b=%d,1,2,1+2);以上代码运行后输出a=1,b=2,a+b=3。二、加载库和

2、普通函数的调用官方API提供的库中有几个主要的函数:view sourceprint?O1 / 初始化02 int DCSPCLIENTDLLInitlnterface( const char *pCenterIP, const unsigned shortnUpLin kSvrPort,c onst un sig ned short n Dow nLin kSvrPort );0304 /释放资源05 int DCSPCLIENTDLL Fi niln terface( void );0607 /登录08 int DCSPCLIENTDLL Login( const unsigned int

3、 uiBranchPlatformID, const unsigned int nU serID, const char *pPassword );09 /注销10 int DCSPCLIENTDLL Logout( con st un sig ned int uiBra nchPlatformID, const un sig ned intnU serID,const char *pPassword );1111 /发车辆实时定位数据12 int DCSPCLIENTDLLSen dUPRealLocatio n( con st char * const pDeviceId, const c

4、harcDeviceColor,13 const unsigned short nMsgCode, const _stBPDynamicData * const pStGpsData );在Python中加载使用:view sourcepri nt?O1 from ctypes import *0203 #加载API库04 api = CDLL(DCSPClie ntDLL.dll);05 #初始化函数的参数类型06 api.I nit In terface.argtypes=c_char_p,c_ushort,c_ushort07 api.Logi n.argtypes=c_ uin t,c

5、_ uin t,c_char_p08 api.Logout.argtypes=c_ uin t,c_ uin t,c_char_p0910 #初始化并登录11 api.Initlnterface(u中心服务器地址”,u上行服务端端口 , u下行客户端端口 )12 api.Logi n( platformID,userID,password);13 #.其它操作14 api.Logout(platformlD,userlD,password); # 注销参数类型可以像上面的代码一样预先设定好,或者在调用函数时再把参数转成相应的c_类型。ctypes的类型对应如下:rtypej typeC typ

6、ePython typee_chiLrcki&rleil ekuint/lortgsk*Ttint/longtithertint/longe_i ntinimilei intintAongc_l on it1omc匚口息,le.int/lcng_iftt64 or lo&cInt/lcnge_gdaabltc _1 oil.do Tibi elose dlouhil*flotcbw NUL terminated)String or 1vckr_t * (NUL terrnJnotcd)ufKvde or ntwii *.皿咤唯卩飞啊二OC;S.SG:如此,完成了简单的第一步。C语言中的Stru

7、ct数据结构在发送实时定位数据的函数 Sen dUPRealLocation中有一个参数是结构体类型_stBPDynamicData python 中没有 struct 这种数据结构,ctypes 很周全,对 C 的 struct 和 union 这二种数据类型都提供很好的支持。stBPDynamicData结构的定义如下: view sourceprint?O1 /车辆动态数据结构体 02 struct _stBPDy namicData03 04/加密状态05un sig ned char en crypt;06/ GPS时间07_StructTime gpsTime;08/经度09un

8、sig ned int Ion gitude;10 /纬度11 un sig ned int latitude;12 II GPS 速度13 un sig ned short un GpsSpeed;14 II行驶记录仪速度15 un sig ned short un TachographSpeed;16 II车辆当前总里程数17 un sig ned int uiMileageTotal;18 II角度19 un sig ned shortan gle;20 II车辆状态21 un sig ned shortstate;22 II报警状态23 un sig ned shortalarm;24

9、 ;ctypes.Structure,其中还用到一个在python中,需要定义一个与这兼容的类,继承于_StructTime结构,这里一并贴出代码: view sourcepri nt?01 class _StructTime(Structure):02_fields_ =(day,c_ubyte).03(m on th,c_ubyte),04(year,c_ushort),05(hour,c_ubyte),06(mi nu te,c_ubyte),07(sec on d,c_ubyte);08def _str_(self):09return3:4:5.format(self.year,sel

10、f.m on th,self.day,self.hour,self. minu te,self.sec on d);1011 class_stBPDy namicData(Structure):12_fields_ =(e ncrypt,c_ubyte),13(gpsTime,_StructTime),14(l on gitude,c_ uin t),15(latitude,c_ui nt),16(un GpsSpeed,c_ushort),17(un TachographSpeed,c_ushort),18(uiMileageTotal,c_ui nt),19(a ngle,c_ushort

11、),20(state,c_ushort),21(alarm,c_ushort);22def _str_(self):0-1-223return u(0,1),6,sp:2,a ngle:3,st:4,al:5.format(self.lo ngitude.24selfatitude,self.u nGpsSpeed,25self.a ngle ,self.state,self.alarm,self.gpsTime );2627 class gpsData(Structure):28_fields_ =(strDevicelD,c_char_p),29(cDeviceColor,c_char),

12、30(nM sgCode,c_ushort),31(stBPD,_stBPDy namicData);32def _str_(self):33return u0,1.format(self.strDevicelD,self.stBPD );gpsData是我自己加的一个类,用于记录每辆车的信息。现在就可以使用 SendUPRealLocation函数发送车辆实时数据了:view sourcepri nt?1 tm=_StructTime();2 tm.year=2010;tm. mon th=4;tm.day=3;tm.hour=11;tm. minu te=2;tm.sec on d=11;

13、3 bpd=_stBPDy namicData();4 bpd.gpsTime=tm;bpd.lo ngitude=1234567;bpd.latitude=246898;#.其它参数5 data=gpsData();6 data.strDevicelD=u桂 Coo007;data.stBPD=bpd;7 #调用API发送数据8 api.Se ndUPRealLocati on( data.strDeviceID, data.cDeviceColor ,9 data. nM sgCode, addressof( data.stBPD );注意 Sen dUPRealLocati on 第 三个参数是_stBPDy namicData *指针类型,所以要用ctypes.addressof()取参数的地址。四、回调函数写到这里就忍不住唠叨几句,这个系统的协议设计的太有“个性”了。这个系统的功能说起来也不复杂,就是要GPS运营商把指定的车辆位置信息发送到中心平台,同时中心平台可以向各GPS终端发送一些数据和指令,比如传送文字信息到终端,或者要求终端拍张照 片反馈到中心。这个协议流程是这样,运营商端主动连接到中心服务器,然后此连接只用于传输向中心平台主动发送的数据。登录成功了之后呢,中心平台再向运营商的IP建立一个连接,用

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 医学/心理学 > 基础医学

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号