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 相关文章推荐
windows下wxPython开发环境安装与配置方法
Jun 28 Python
Python数据库的连接实现方法与注意事项
Feb 27 Python
Python数据操作方法封装类实例
Jun 23 Python
python skimage 连通性区域检测方法
Jun 21 Python
详解使用PyInstaller将Pygame库编写的小游戏程序打包为exe文件
Aug 23 Python
python线程的几种创建方式详解
Aug 29 Python
python序列化与数据持久化实例详解
Dec 20 Python
详解Django3中直接添加Websockets方式
Feb 12 Python
如何在Win10系统使用Python3连接Hive
Oct 15 Python
详解appium自动化测试工具(monitor、uiautomatorviewer)
Jan 27 Python
Python anaconda安装库命令详解
Oct 16 Python
Python Matplotlib库实现画局部图
Nov 17 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
一个ubbcode的函数,速度很快.
2006/10/09 PHP
mysql 性能的检查和优化方法
2009/06/21 PHP
分享下PHP register_globals 值为on与off的理解
2013/09/26 PHP
php通过分类列表产生分类树数组的方法
2015/04/20 PHP
[原创]静态页面也可以实现预览 列表不同的显示方式
2006/10/14 Javascript
jQuery EasyUI API 中文文档 - ComboGrid 组合表格
2011/10/13 Javascript
IE的fireEvent方法概述及应用
2013/02/22 Javascript
jQuery的bind()方法使用详解
2015/07/15 Javascript
Javascript 实现计算器时间功能详解及实例(二)
2017/01/08 Javascript
详解JavaScript数组过滤相同元素的5种方法
2017/05/23 Javascript
H5基于iScroll实现下拉刷新和上拉加载更多
2017/07/18 Javascript
Angular2环境搭建具体操作步骤(推荐)
2017/08/04 Javascript
浅谈Node异步编程的机制
2017/10/18 Javascript
对Vue2 自定义全局指令Vue.directive和指令的生命周期介绍
2018/08/30 Javascript
微信小程序的线程架构【推荐】
2019/05/14 Javascript
vue样式穿透 ::v-deep的具体使用
2020/06/04 Javascript
详解Vue中Axios封装API接口的思路及方法
2020/10/10 Javascript
vue中如何添加百度统计代码
2020/12/19 Vue.js
分析python服务器拒绝服务攻击代码
2014/01/16 Python
解决python大批量读写.doc文件的问题
2018/05/08 Python
python调用百度语音识别实现大音频文件语音识别功能
2018/08/30 Python
python实现五子棋游戏(pygame版)
2020/01/19 Python
纯css3实现图片翻牌特效
2015/03/10 HTML / CSS
Html5内唤醒百度、高德APP的实现示例
2019/05/20 HTML / CSS
Omio意大利:全欧洲低价大巴、火车和航班搜索和比价
2017/12/02 全球购物
加拿大领先的牛仔零售商:Bluenotes
2018/01/22 全球购物
Linux文件系统类型
2012/09/16 面试题
轻金属冶金专业毕业生自荐信
2013/11/02 职场文书
外贸业务员工作职责
2014/01/06 职场文书
高一英语教学反思
2014/01/22 职场文书
查摆问题整改措施
2014/10/24 职场文书
求职导师推荐信范文
2015/03/27 职场文书
运动会开幕式新闻稿
2015/07/17 职场文书
读鲁迅先生的经典名言
2019/08/20 职场文书
Apache Hudi的多版本清理服务彻底讲解
2022/03/31 Servers
Java 使用类型为Object的变量指向任意类型的对象
2022/04/13 Java/Android