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转换摩斯密码示例
Feb 16 Python
Python实现豆瓣图片下载的方法
May 25 Python
Python的Flask框架中使用Flask-Migrate扩展迁移数据库的教程
Jun 14 Python
python绘制地震散点图
Jun 18 Python
Pytorch Tensor基本数学运算详解
Dec 30 Python
wxpython自定义下拉列表框过程图解
Feb 14 Python
使用IPython或Spyder将省略号表示的内容完整输出
Apr 20 Python
Django实现后台上传并显示图片功能
May 29 Python
python中if及if-else如何使用
Jun 02 Python
浅谈Python中的继承
Jun 19 Python
python3通过subprocess模块调用脚本并和脚本交互的操作
Dec 05 Python
pandas DataFrame.shift()函数的具体使用
May 24 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
人族 TERRAN 概述
2020/03/14 星际争霸
php simplexmlElement操作xml的命名空间实现代码
2011/01/04 PHP
php多功能图片处理类分享(php图片缩放类)
2014/03/14 PHP
PHP的Laravel框架结合MySQL与Redis数据库的使用部署
2016/03/21 PHP
PDO实现学生管理系统
2020/03/21 PHP
jquery中this的使用说明
2010/09/06 Javascript
Bootstrap树形组件jqTree的简单封装
2016/01/25 Javascript
jQuery基础知识点总结(必看)
2016/05/31 Javascript
让浏览器崩溃的12行JS代码(DoS攻击分析及防御)
2016/10/10 Javascript
vue组件生命周期详解
2017/11/07 Javascript
基于vue 动态加载图片src的解决方法
2018/02/05 Javascript
vue中动态绑定表单元素的属性方法
2018/02/23 Javascript
Vue2 模板template的四种写法总结
2018/02/23 Javascript
利用nodeJs anywhere搭建本地服务器环境的方法
2018/05/12 NodeJs
jQuery 实现倒计时天,时,分,秒功能
2018/07/31 jQuery
layui radio性别单选框赋值方法
2018/08/15 Javascript
基于Koa2写个脚手架模拟接口服务的方法
2018/11/27 Javascript
解决Layui中templet中a的onclick参数传递的问题
2019/09/20 Javascript
Vue之封装公用变量以及实现方式
2020/07/31 Javascript
Javascript call及apply应用场景及实例
2020/08/26 Javascript
[01:24:34]2014 DOTA2华西杯精英邀请赛5 24 DK VS LGD
2014/05/25 DOTA
[45:16]完美世界DOTA2联赛PWL S3 Magma vs Phoenix 第一场 12.12
2020/12/16 DOTA
Python中Class类用法实例分析
2015/11/12 Python
利用python代码写的12306订票代码
2015/12/20 Python
详解python eval函数的妙用
2017/11/16 Python
PythonPC客户端自动化实现原理(pywinauto)
2020/05/28 Python
Python3基于plotly模块保存图片表格
2020/08/03 Python
Bluebella法国官网:英国性感内衣品牌
2019/05/03 全球购物
Sandro法国官网:法国成衣品牌
2019/08/28 全球购物
台湾7-ELEVEN线上购物中心:7-11
2021/01/21 全球购物
实习生自荐信范文分享
2013/11/27 职场文书
企划专员岗位职责
2013/12/09 职场文书
会计系中文个人求职信
2013/12/24 职场文书
2014国庆黄金周超市促销活动方案
2014/09/21 职场文书
科技馆观后感
2015/06/08 职场文书
房屋所有权证明
2015/06/19 职场文书