Python调用DLL中的函数

最近研究了一下用Python调用dll,Python果然很神奇,代码不多既实现了从窗口创建到调用dll中函数的功能,程序也不复杂,最后打包成exe可直接执行的程序,测试过程中先用Python3.5,但用PyInstaller打包后的程序在xp系统上不能运行,后来用2.7,打包后的exe在xp或win7、win10上均可运行了,本人用的win10,程序安装如下:

1、https://www.python.org/-》download-》windows-》latest python 2 release-Python2.7.15
2、pip install pywin32
3、pip install PyInstaller

 

附源码如下:

#coding:utf-8
import tkinter
import Tkinter
import tkinter.messagebox
import tkinter as tk
from tkinter import ttk
from Tkinter import *
import ctypes

import sys      #保证能正常把二代证输出的gb2312码能够转成utf-8
reload(sys)
sys.setdefaultencoding('gb2312')

dll= ctypes.windll.LoadLibrary('IdCard_GWI_V001.dll')

def openPort(port):
    sBuf = 'A'
    pStr = ctypes.c_char_p()
    pStr.value = sBuf
    iRet=dll.InitComm(int(port), pStr, 9600)
    if iRet==0:
        msgInfo.insert(INSERT,'打开端口成功\n')
        msgInfo.insert(END,'')
    else:
        msgInfo.insert(INSERT,'打开端口失败\n')
        msgInfo.insert(END,'')
def closePort():
    iRet=dll.CloseComm()
    msgInfo.insert(INSERT,'端口已经关闭\n')
    msgInfo.insert(END,'')
def Authenticate():
    iRet=dll.Authenticate()
    if iRet==0:
        msgInfo.insert(INSERT,'卡认证成功\n')
        msgInfo.insert(END,'')
    else:
        msgInfo.insert(INSERT,'卡认证失败\n')
        msgInfo.insert(END,'')
def ReadBaseMsg():
    sDllPath='.\\'
    pDllPath=ctypes.c_char_p()
    pDllPath.value=sDllPath

    sPhotoPath='.\\'
    pPhotoPath=ctypes.c_char_p()
    pPhotoPath.value=sPhotoPath
    sMsg="\0"*1024
    pMsg=ctypes.c_char_p()
    pMsg.value=sMsg

    intLen = ctypes.c_int(0)
    #tkinter.messagebox.showinfo('二代证测试',sDllPath+sPhotoPath+sMsg)
    iRet=dll.ReadBaseMsg(pDllPath,sPhotoPath,pMsg,ctypes.byref(intLen))
    if iRet==0:
        msgInfo.insert(INSERT,'读卡成功\n')
        msg=ctypes.string_at(sMsg)
        msg1 = msg.encode('utf-8')
        msgInfo.insert(INSERT,msg1+'\n')
        msgInfo.insert(END,'')
    else:
        msgInfo.insert(INSERT,'读卡失败\n')
        msgInfo.insert(END,'')
def cardMessage(port):
    iPortNo=int(port)

    sBuf = 'A'
    extendPort = ctypes.c_char_p()
    extendPort.value = sBuf

    iBaudRate=9600
    iTimeOut=20

    hotohead='c:\\temp\\head.bmp'
    photohead = ctypes.c_char_p()
    photohead.value = hotohead

    sMsg="\0"*1024
    buf=ctypes.c_char_p()
    buf.value=sMsg

    errInfo="\0"*1024
    psErrInfo=ctypes.c_char_p()
    psErrInfo.value=errInfo

    iRet=dll.Get_Info(iPortNo,extendPort,iBaudRate, iTimeOut,photohead,buf, psErrInfo)
    if iRet==0:
        msgInfo.insert(INSERT,'读卡成功\n')
        msg=ctypes.string_at(sMsg)
        msg1 = msg.encode('utf-8')
        msgInfo.insert(INSERT,msg1+'\n')
        msgInfo.insert(END,'')
    else:
        msgInfo.insert(INSERT,'读卡失败\n')
        msgInfo.insert(END,'')
def cardMessagePhoto(port):
    iPortNo=int(port)

    sBuf='A'
    extendPort=ctypes.c_char_p()
    extendPort.value=sBuf

    iBaudRate=9600
    iTimeOut=20

    hotohead='C:\\TEMP\\head.jpg'
    photohead=ctypes.c_char_p()
    photohead.value=hotohead

    hotoface='C:\\TEMP\\face.jpg'
    photoface=ctypes.c_char_p()
    photoface.value=hotoface

    hotoback='C:\\TEMP\\back.jpg'
    photoback=ctypes.c_char_p()
    photoback.value=hotoback

    sMsg="\0"*1024
    buf=ctypes.c_char_p()
    buf.value=sMsg

    errInfo="\0"*1024
    psErrInfo=ctypes.c_char_p()
    psErrInfo.value=errInfo

    iRet=dll.Get_Info_WithPhoto(iPortNo,extendPort ,iBaudRate,iTimeOut,photohead,photoface,photoback,buf,psErrInfo)
    if iRet==0:
        msgInfo.insert(INSERT,'读卡成功\n')
        msg=ctypes.string_at(sMsg)
        msg1 = msg.encode('utf-8')
        msgInfo.insert(INSERT,msg1+'\n')
        msgInfo.insert(END,'')
    else:
        msgInfo.insert(INSERT,'读卡失败\n')
        msgInfo.insert(END,'')
def cleanText():
    msgInfo.delete(1.0,END)
#tkinter.messagebox.showinfo('二代证测试',str(port))
root=Tk()
root.title('二代证测试程序')
root['bg'] = '#bcbcbc'
root.attributes("-alpha", 0.9)
root.geometry('800x500')

#dhl:显示交互信息的文本框
msgInfo = Text(root,width=110,height=30)
msgInfo.place(x=10,y=90,anchor=NW)

#dhl:输入端口号
Label(root,text='端口号').place(x=10,y=10,anchor=NW)
port = tk.StringVar()
port.set(9)
portNum = ttk.Entry(root, width=6,textvariable=port)
portNum.place(x=55,y=10,anchor=NW)

#dhl:打开端口按钮
oPort=Button(root,text='打开端口',width=10,command=lambda:openPort(port.get()))
oPort.place(x=10,y=40,anchor=NW)

#dhl:卡认证
Aut=Button(root,text='认证卡片',width=10,command=lambda:Authenticate())
Aut.place(x=100,y=40,anchor=NW)

#dhl:读信息
Read=Button(root,text='读卡',width=10,command=lambda:ReadBaseMsg())
Read.place(x=190,y=40,anchor=NW)

#dhl:关闭端口按钮
cPort=Button(root,text='关闭端口',width=10,command=lambda:closePort())
cPort.place(x=280,y=40,anchor=NW)


#Label(root,text='XX农信接口',height=2).place(x=450,y=40,anchor=NW)

Label(root,bitmap='info',text='  XX农信-->>', height=22,compound=Tkinter.LEFT).place(x=410,y=40,anchor=NW)


#dhl:XX农信的接口1,获取文字信息
cardMsg=Button(root,text='读卡信息',width=10,command=lambda:cardMessage(port.get()))
cardMsg.place(x=530,y=40,anchor=NW)

#dhl:XX农信的接口1,获取文字及图片
cardMsgPhoto=Button(root,text='文字图片',width=10,command=lambda:cardMessagePhoto(port.get()))
cardMsgPhoto.place(x=615,y=40,anchor=NW)

#dhl:清除显示内容
clean=Button(root,text='清除',width=10,command=lambda:cleanText())
clean.place(x=700,y=40,anchor=NW)

root.mainloop()

 

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Python调用DLL的多个函数,可以按照以下步骤进行操作: 1. 首先,确保已经安装了Python解释器和相应的库,如pywin32和PyInstaller。你可以从Python官方网站下载最新的Python 2版本,并使用pip安装pywin32和PyInstaller。 2. 创建一个新的Python脚本,可以使用任何文本编辑器打开。在脚本的开头,导入必要的库,比如ctypes。ctypes库提供了与C DLL交互的功能。 3. 使用ctypes库的cdll或windll函数加载DLL文件。cdll用于加载C语言编写的DLL,而windll用于加载Windows API的DLL。你需要提供DLL文件的路径作为参数。 4. 使用加载的DLL对象调用函数。你可以使用DLL对象的属性或方法来访问和调用DLL函数。要调用函数,你需要提供函数名称和参数。 5. 在调用DLL函数之前,你可能需要定义函数的返回类型和参数类型。这是因为Python是动态类型语言,需要明确指定函数的类型信息才能正确地调用。 6. 最后,测试你的代码。你可以运行Python脚本,检查是否成功调用DLL的多个函数。确保调用函数和参数与DLL的定义相匹配。 综上所述,Python调用DLL的多个函数的步骤包括导入必要的库、加载DLL文件、调用函数并进行测试。通过这些步骤,你可以在Python成功调用DLL的多个函数。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Python调用DLL函数](https://blog.csdn.net/donghailin/article/details/82705784)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *3* [python使用ctypes库调用DLL动态链接库](https://download.csdn.net/download/weixin_38703823/13705547)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

绝知此事要躬行

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值