python调用Delphi写的Dll代码示例


Posted in Python onDecember 05, 2017

首先看下Delphi单元文件基本结构:

unit Unit1;  //单元文件名 
interface   //这是接口关键字,用它来标识文件所调用的单元文件 
uses     //程序用到的公共单元 
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs; 

type     //这里定义了程序所用的组件,一些类,以及组件所对应的过程、事件 
TForm1 = class(TForm) 
private   //定义私有变量和私有过程 
  { Private declarations }
public   //定义公共变量和公共过程 
  { Public declarations }
end; 
  
var      //定义程序使用的公共变量 
Form1: TForm1; 

implementation //程序代码实现部分 

{$R *.dfm}
  
end.

Delphi单元如下(输出hello.dll):

unit hellofun;

interface

function getint():integer;stdcall;
function sayhello(var sname:PAnsiChar):PAnsiChar;stdcall;
implementation

function getint():integer;stdcall;
begin
 result:=888;
end;
function sayhello(var sname:PAnsiChar):PAnsiChar;stdcall;
begin
 sname:='ok!';
 result:='hello,garfield !';
end;

end.
library hello;

{ Important note about DLL memory management: ShareMem must be the
 first unit in your library's USES clause AND your project's (select
 Project-View Source) USES clause if your DLL exports any procedures or
 functions that pass strings as parameters or function results. This
 applies to all strings passed to and from your DLL--even those that
 are nested in records and classes. ShareMem is the interface unit to
 the BORLNDMM.DLL shared memory manager, which must be deployed along
 with your DLL. To avoid using BORLNDMM.DLL, pass string information
 using PChar or ShortString parameters. }

uses
 System.SysUtils,
 System.Classes,
 hellofun in 'hellofun.pas';

{$R *.res}

exports
 getint,
 sayhello;

begin
end.

python中调用如下:

import ctypes

def main():
  dll=ctypes.windll.LoadLibrary("hello.dll")
  ri=dll.getint()
  print(ri)

  s=ctypes.c_char_p()
  rs=ctypes.c_char_p()
  rs=dll.sayhello(ctypes.byref(s))
  print(s)
  print(ctypes.c_char_p(rs))

if __name__ == '__main__':
  main()

运行Python,输出如下:

>>> 
888
c_char_p(b'ok!')
c_char_p(b'hello,garfield !')
>>>

好了,我们可以让python完成部分功能在Delphi中调用,也可以用Delphi完成部分功能在Python中调用。

以上程序在DelphiXE2及Python3.2中调试通过。

总结

以上就是本文关于python调用Delphi写的Dll代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

Python 相关文章推荐
python中map()与zip()操作方法
Feb 27 Python
Python下的Softmax回归函数的实现方法(推荐)
Jan 26 Python
TensorFlow神经网络优化策略学习
Mar 09 Python
python3+PyQt5实现柱状图
Apr 24 Python
Python小游戏之300行代码实现俄罗斯方块
Jan 04 Python
python 实现矩阵上下/左右翻转,转置的示例
Jan 23 Python
python 读取dicom文件,生成info.txt和raw文件的方法
Jan 24 Python
python实现连连看辅助(图像识别)
Mar 25 Python
Django模板获取field的verbose_name实例
May 19 Python
基于python的opencv图像处理实现对斑马线的检测示例
Nov 29 Python
Pycharm创建python文件自动添加日期作者等信息(步骤详解)
Feb 03 Python
使用pandas或numpy处理数据中的空值(np.isnan()/pd.isnull())
May 14 Python
Python字典数据对象拆分的简单实现方法
Dec 05 #Python
python reduce 函数使用详解
Dec 05 #Python
有趣的python小程序分享
Dec 05 #Python
详细分析python3的reduce函数
Dec 05 #Python
Python数据可视化正态分布简单分析及实现代码
Dec 04 #Python
Python编程实现二分法和牛顿迭代法求平方根代码
Dec 04 #Python
Python编程给numpy矩阵添加一列方法示例
Dec 04 #Python
You might like
《PHP编程最快明白》第四讲:日期、表单接收、session、cookie
2010/11/01 PHP
PHP开发框架kohana3 自定义路由设置示例
2014/07/14 PHP
PHP的Laravel框架中使用消息队列queue及异步队列的方法
2016/03/21 PHP
js禁止document element对象选中文本实现代码
2013/03/21 Javascript
JS实现局部选择打印和局部不选择打印
2014/04/03 Javascript
深入分析Cookie的安全性问题
2015/03/01 Javascript
谈谈impress.js初步理解
2015/09/09 Javascript
NodeJS使用formidable实现文件上传
2016/10/27 NodeJs
详解vue 模版组件的三种用法
2017/07/21 Javascript
Vue.js中使用iView日期选择器并设置开始时间结束时间校验功能
2018/08/12 Javascript
IE8中jQuery.load()加载页面不显示的原因
2018/11/15 jQuery
vue前端框架—Mint UI详解(更适用于移动端)
2019/04/30 Javascript
D3.js的基础部分之数组的处理数组的排序和求值(v3版本)
2019/05/09 Javascript
小试小程序云开发(小结)
2019/06/06 Javascript
vue实现在线预览pdf文件和下载(pdf.js)
2019/11/26 Javascript
解决Vue大括号字符换行踩的坑
2020/11/09 Javascript
react中hook介绍以及使用教程
2020/12/11 Javascript
Python实现读取邮箱中的邮件功能示例【含文本及附件】
2017/08/05 Python
python实现求最长回文子串长度
2018/01/22 Python
pandas创建新Dataframe并添加多行的实例
2018/04/08 Python
python实现俄罗斯方块游戏
2020/03/25 Python
浅析python继承与多重继承
2018/09/13 Python
pandas计数 value_counts()的使用
2019/06/24 Python
Win10下Python3.7.3安装教程图解
2019/07/08 Python
python目标检测给图画框,bbox画到图上并保存案例
2020/03/10 Python
opencv之颜色过滤只留下图片中的红色区域操作
2020/06/05 Python
基于Python下载网络图片方法汇总代码实例
2020/06/24 Python
基于OpenCV的网络实时视频流传输的实现
2020/11/15 Python
h5移动端调用支付宝、微信支付的实现
2020/06/08 HTML / CSS
美国眼镜网:GlassesUSA
2017/09/07 全球购物
Skyscanner香港:机票比价, 平机票和廉价航空机票预订
2020/02/07 全球购物
Chi Chi London官网:购买连衣裙和礼服
2020/10/25 全球购物
2014组织生活会方案
2014/05/19 职场文书
医院护士党的群众路线教育实践活动对照检查材料思想汇报
2014/10/04 职场文书
毕业设计答辩开场白
2015/05/29 职场文书
Golang中channel的原理解读(推荐)
2021/10/16 Golang