python调用Delphi写的Dll代码示例

作者:garfieldtom 时间:2023-07-25 09:33:53 

首先看下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中调试通过。

来源:http://www.cnblogs.com/GarfieldTom/archive/2013/03/15/2961994.html

标签:delphi,python,dll
0
投稿

猜你喜欢

  • 19个ASP编程基础典型代码

    2008-10-23 15:46:00
  • prototype.js常用函数及其用法

    2007-11-21 20:47:00
  • Javascript操作表单之间的数据传递

    2007-10-25 12:58:00
  • 使用 Osql 工具管理 SQL Server 桌面引擎 (MSDE 2000)应用介绍

    2020-07-01 22:12:59
  • 从SQL Server2000升级到2005的过程解析

    2009-01-13 14:07:00
  • 网页HTTP header头信息详解

    2010-03-31 14:42:00
  • 基于php权限分配的实现代码

    2023-11-14 11:10:49
  • 关于设计的一些个人看法

    2008-06-12 12:44:00
  • python复制文件到指定目录的实例

    2021-03-17 17:10:26
  • 关于基于字体大小(em)的设计

    2008-06-17 15:01:00
  • YUI3.3.0 中 transition 事件的变化

    2011-06-16 20:51:45
  • Python如何使用神经网络进行简单文本分类

    2022-05-10 00:45:26
  • SQL Server 数据库操作实用技巧锦集

    2009-01-20 13:20:00
  • 基于Python实现二维图像双线性插值

    2023-08-13 07:46:45
  • python 对任意数据和曲线进行拟合并求出函数表达式的三种解决方案

    2021-10-02 12:40:16
  • python导入坐标点的具体操作

    2023-02-24 19:59:46
  • 怎样使用 SQL Server 数据库嵌套子查询

    2009-02-05 15:26:00
  • 各个版本IE合集下载,共存无冲突

    2007-11-29 13:12:00
  • PyCharm安装库numpy失败问题的详细解决方法

    2023-05-18 22:45:16
  • python集合比较(交集,并集,差集)方法详解

    2023-09-30 22:11:13
  • asp之家 网络编程 m.aspxhome.com