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模拟新浪微博登陆功能(新浪微博爬虫)
Dec 24 Python
初学Python函数的笔记整理
Apr 07 Python
Python素数检测实例分析
Jun 15 Python
Python实现批量检测HTTP服务的状态
Oct 27 Python
pygame加载中文名mp3文件出现error
Mar 31 Python
Python实现OpenCV的安装与使用示例
Mar 30 Python
Windows 64位下python3安装nltk模块
Sep 19 Python
Python 矩阵转置的几种方法小结
Dec 02 Python
Python openpyxl模块原理及用法解析
Jan 19 Python
Python龙贝格法求积分实例
Feb 29 Python
在Django中自定义filter并在template中的使用详解
May 19 Python
Python之Matplotlib绘制热力图和面积图
Apr 13 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
人大复印资料处理程序_输入篇
2006/10/09 PHP
php调用mysql存储过程
2007/02/14 PHP
php上传文件常见问题总结
2015/02/03 PHP
JS版网站风格切换实例代码
2008/10/06 Javascript
Domino中运用jQuery读取视图内容的方法
2009/10/21 Javascript
基于jquery的内容循环滚动小模块(仿新浪微博未登录首页滚动微博显示)
2011/03/28 Javascript
jQuery使用load()方法载入另外一个网页文件内的指定标签内容到div标签的方法
2015/03/25 Javascript
ES6中Proxy代理用法实例浅析
2017/04/06 Javascript
使用jQuery ajaxupload插件实现无刷新上传文件
2017/04/23 jQuery
nodejs multer实现文件上传与下载
2017/05/10 NodeJs
BootStrap 表单控件之单选按钮水平排列
2017/05/23 Javascript
js生成word中图片处理方法
2018/01/06 Javascript
JS实现简易留言板增删功能
2020/02/08 Javascript
[01:46]DOTA2上海特锦赛小组赛英文解说KotlGuy采访
2016/02/27 DOTA
[00:43]拉比克至宝魔导师密钥展示
2018/12/20 DOTA
[01:54]TI珍贵瞬间系列(三):翻盘
2020/08/28 DOTA
[54:26]完美世界DOTA2联赛PWL S3 Forest vs Rebirth 第一场 12.10
2020/12/12 DOTA
[08:08]DOTA2-DPC中国联赛2月28日Recap集锦
2021/03/11 DOTA
python使用json序列化datetime类型实例解析
2018/02/11 Python
用Python编写一个简单的CS架构后门的方法
2018/11/20 Python
Apache,wsgi,django 程序部署配置方法详解
2019/07/01 Python
python实现密码强度校验
2020/03/18 Python
Python中有几个关键字
2020/06/04 Python
Python通过zookeeper实现分布式服务代码解析
2020/07/22 Python
html5定制表单_动力节点Java学院整理
2017/07/11 HTML / CSS
JD Sports西班牙:英国领先的运动服装公司
2020/01/06 全球购物
企业给企业的表扬信
2014/01/13 职场文书
绩效管理实施方案
2014/03/19 职场文书
企业安全生产标语
2014/06/06 职场文书
投标人法定代表人授权委托书格式
2014/09/28 职场文书
2015年七一建党节活动方案
2015/05/05 职场文书
天那边观后感
2015/06/09 职场文书
2016年七夕情人节宣传语
2015/11/25 职场文书
Python爬虫进阶之Beautiful Soup库详解
2021/04/29 Python
MySql 缓存查询原理与缓存监控和索引监控介绍
2021/07/02 MySQL
python使用shell脚本创建kafka连接器
2022/04/29 Python