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通过pil模块获得图片exif信息的方法
Mar 16 Python
Python中文分词实现方法(安装pymmseg)
Jun 14 Python
Python标准库06之子进程 (subprocess包) 详解
Dec 07 Python
详解python中字典的循环遍历的两种方式
Feb 07 Python
Python实现遍历目录的方法【测试可用】
Mar 22 Python
numpy添加新的维度:newaxis的方法
Aug 02 Python
Python定时任务工具之APScheduler使用方式
Jul 24 Python
django框架forms组件用法实例详解
Dec 10 Python
pytorch-神经网络拟合曲线实例
Jan 15 Python
简单了解如何封装自己的Python包
Jul 08 Python
python3访问字典里的值实例方法
Nov 18 Python
matplotlib事件处理基础(事件绑定、事件属性)
Feb 03 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 Hash函数,增强密码安全
2011/02/25 PHP
PHP使用strtotime计算两个给定日期之间天数的方法
2015/03/18 PHP
ThinkPHP自定义函数解决模板标签加减运算的方法
2015/07/03 PHP
PHP基于phpqrcode类生成二维码的方法详解
2018/03/14 PHP
js本身的局限性 别让javascript做太多事
2010/03/23 Javascript
JavaScript实现拼音排序的方法
2012/11/20 Javascript
Js 回车换行处理的办法及replace方法应用
2013/01/24 Javascript
setTimeout内不支持jquery的选择器的解决方案
2015/04/28 Javascript
简单理解JavaScript中的封装与继承特性
2016/03/19 Javascript
js注入 黑客之路必备!
2016/09/14 Javascript
JavaScript触发onScroll事件的函数节流详解
2016/12/14 Javascript
jQuery点击导航栏选中更换样式的实现代码
2017/01/23 Javascript
微信小程序switch组件使用详解
2018/01/31 Javascript
webpack 4.0.0-beta.0版本新特性介绍
2018/02/10 Javascript
vue组件开发之用户无限添加自定义填写表单的方法
2018/08/28 Javascript
angularJS1 url中携带参数的获取方法
2018/10/09 Javascript
jquery实现垂直无限轮播的方法分析
2019/07/16 jQuery
node.js使用yargs处理命令行参数操作示例
2020/02/11 Javascript
element-ui封装一个Table模板组件的示例
2021/01/04 Javascript
[02:52]2014DOTA2西雅图国际邀请赛 CIS战队巡礼
2014/07/07 DOTA
python打开网页和暂停实例
2014/09/30 Python
Python中的条件判断语句基础学习教程
2016/02/07 Python
Python WSGI的深入理解
2018/08/01 Python
基于Python3.6+splinter实现自动抢火车票
2018/09/25 Python
Python根据当前日期取去年同星期日期
2019/04/14 Python
Keras-多输入多输出实例(多任务)
2020/06/22 Python
python调用摄像头的示例代码
2020/09/28 Python
Django中ORM的基本使用教程
2020/12/22 Python
一站式跨境收款解决方案:Payoneer(派安盈)
2018/09/06 全球购物
Nº21官方在线商店:numeroventuno.com
2019/09/26 全球购物
会计学财务管理专业个人的自我评价
2013/10/19 职场文书
授权委托书样本
2014/09/25 职场文书
奖学金感谢信
2015/01/21 职场文书
会议简报格式范文
2015/07/20 职场文书
mysql中整数数据类型tinyint详解
2021/12/06 MySQL
JavaScript前端面试扁平数据转tree与tree数据扁平化
2022/06/14 Javascript