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网络编程学习笔记(三):socket网络服务器
Jun 09 Python
python 生成器生成杨辉三角的方法(必看)
Apr 10 Python
TensorFlow 实战之实现卷积神经网络的实例讲解
Feb 26 Python
Python解析Excle文件中的数据方法
Oct 23 Python
python 用opencv调用训练好的模型进行识别的方法
Dec 07 Python
python快排算法详解
Mar 04 Python
Python 中PyQt5 点击主窗口弹出另一个窗口的实现方法
Jul 04 Python
如何基于Python批量下载音乐
Nov 11 Python
python将邻接矩阵输出成图的实现
Nov 21 Python
python openpyxl模块的使用详解
Feb 25 Python
python字符串拼接.join()和拆分.split()详解
Nov 23 Python
Python经常使用的一些内置函数
Apr 11 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 Smarty初体验二 获取配置信息
2011/08/08 PHP
thinkphp实现数组分页示例
2014/04/13 PHP
ThinkPHP CURD方法之page方法详解
2014/06/18 PHP
laravel学习教程之关联模型
2016/07/30 PHP
PHP 枚举类型的管理与设计知识点总结
2020/02/13 PHP
基于JavaScript 声明全局变量的三种方式详解
2013/05/07 Javascript
js动画效果制件让图片组成动画代码分享
2014/01/14 Javascript
JS对象与json字符串格式转换实例
2014/10/28 Javascript
浅析$.getJSON异步请求和同步请求
2016/06/06 Javascript
客户端验证用户名和密码的方法详解
2016/06/16 Javascript
js模块加载方式浅析
2017/08/12 Javascript
Promise扫盲贴
2019/06/24 Javascript
基于ajax实现上传图片代码示例解析
2020/12/03 Javascript
python 实现堆排序算法代码
2012/06/05 Python
Python中实现从目录中过滤出指定文件类型的文件
2015/02/02 Python
在Django框架中伪造捕捉到的URLconf值的方法
2015/07/18 Python
python ddt实现数据驱动
2018/03/14 Python
Windows下将Python文件打包成.EXE可执行文件的方法
2018/08/03 Python
python针对mysql数据库的连接、查询、更新、删除操作示例
2019/09/11 Python
python超时重新请求解决方案
2019/10/21 Python
python SVD压缩图像的实现代码
2019/11/05 Python
Python pymysql模块安装并操作过程解析
2020/10/13 Python
css3背景_动力节点Java学院整理
2017/07/11 HTML / CSS
AmazeUI底部导航栏与分享按钮的示例代码
2020/08/18 HTML / CSS
Anthropologie英国:美国家喻户晓的休闲服装和家居产品品牌
2018/12/05 全球购物
几个MySql的面试题
2013/04/22 面试题
公司出纳岗位职责
2013/12/07 职场文书
幼儿园大班新学期寄语
2014/01/18 职场文书
保险专业自荐信范文
2014/02/20 职场文书
计算机专业求职信
2014/06/02 职场文书
2014年学校团委工作总结
2014/12/20 职场文书
团结主题班会
2015/08/13 职场文书
小数乘法教学反思
2016/02/22 职场文书
Canvas跟随鼠标炫彩小球的实现
2021/04/11 Javascript
Oracle使用别名的好处
2022/04/19 Oracle
Golang日志包的使用
2022/04/20 Golang