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处理json数据中的中文
Mar 06 Python
跟老齐学Python之从格式化表达式到方法
Sep 28 Python
Python Requests 基础入门
Apr 07 Python
Python编程实现微信企业号文本消息推送功能示例
Aug 21 Python
python破解zip加密文件的方法
May 31 Python
numpy返回array中元素的index方法
Jun 27 Python
python数据批量写入ScrolledText的优化方法
Oct 11 Python
python实现爬山算法的思路详解
Apr 09 Python
记录Python脚本的运行日志的方法
Jun 05 Python
基于Django实现日志记录报错信息
Dec 17 Python
Ubuntu配置Pytorch on Graph (PoG)环境过程图解
Nov 19 Python
python 统计list中各个元素出现的次数的几种方法
Feb 20 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 取得瑞年与平年的天数的代码
2009/08/10 PHP
jQuery获取json后使用zy_tmpl生成下拉菜单
2015/03/27 PHP
php精确的统计在线人数的方法
2015/10/21 PHP
编写PHP脚本来实现WordPress中评论分页的功能
2015/12/10 PHP
PHP实现八皇后算法
2019/05/06 PHP
完美解决JS中汉字显示乱码问题(已解决)
2006/12/27 Javascript
jQuery中val()方法用法实例
2014/12/25 Javascript
jQuery实现的进度条效果
2015/07/15 Javascript
仅30行代码实现Javascript中的MVC
2016/02/15 Javascript
详细解读Jquery各Ajax函数($.get(),$.post(),$.ajax(),$.getJSON())
2016/08/15 Javascript
正则验证小数点后面只能有两位数的方法
2017/02/28 Javascript
微信小程序开发之animation循环动画实现的让云朵飘效果
2017/07/14 Javascript
js实现文件上传功能 后台使用MultipartFile
2018/09/08 Javascript
vue2.0自定义指令示例代码详解
2019/04/25 Javascript
javascript 关于赋值、浅拷贝、深拷贝的个人理解
2019/11/01 Javascript
Vue实例的对象参数options的几个常用选项详解
2019/11/08 Javascript
vue keep-alive的简单总结
2021/01/25 Vue.js
vue监听键盘事件的相关总结
2021/01/29 Vue.js
[06:25]第二届DOTA2亚洲邀请赛主赛事第二天比赛集锦.mp4
2017/04/03 DOTA
[52:31]VP vs Serenity 2018国际邀请赛小组赛BO2 第二场 8.16
2018/08/17 DOTA
在Django的session中使用User对象的方法
2015/07/23 Python
简单解决Python文件中文编码问题
2015/11/22 Python
python常用知识梳理(必看篇)
2017/03/23 Python
详解css3 Transition属性(平滑过渡菜单栏案例)
2017/09/05 HTML / CSS
连卡佛中国官网:Lane Crawford中文站
2018/01/27 全球购物
娇韵诗Clarins意大利官方网站:法国天然护肤品牌
2020/03/11 全球购物
Java面试题汇总
2015/12/06 面试题
最新英语专业学生求职信范文
2013/09/21 职场文书
室内设计实习自我鉴定
2013/09/25 职场文书
办公室内勤工作职责
2013/12/11 职场文书
房地产资料员岗位职责
2014/07/02 职场文书
优秀家长自荐材料
2014/08/26 职场文书
浅谈Python 中的复数问题
2021/05/19 Python
Mysql实现简易版搜索引擎的示例代码
2021/08/30 MySQL
MySQL数据库完全卸载的方法
2022/03/03 MySQL
电脑只能进入安全模式无法正常启动的解决办法
2022/04/08 数码科技