国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python調(diào)用Delphi寫的Dll代碼示例

2020-02-16 10:56:49
字體:
供稿:網(wǎng)友

首先看下Delphi單元文件基本結(jié)構(gòu):

unit Unit1;  //單元文件名 interface   //這是接口關(guān)鍵字,用它來標(biāo)識(shí)文件所調(diào)用的單元文件 uses     //程序用到的公共單元 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs; type     //這里定義了程序所用的組件,一些類,以及組件所對(duì)應(yīng)的過程、事件 TForm1 = class(TForm) private   //定義私有變量和私有過程   { Private declarations }public   //定義公共變量和公共過程   { Public declarations }end;   var      //定義程序使用的公共變量 Form1: TForm1; implementation //程序代碼實(shí)現(xiàn)部分 {$R *.dfm}  end. 

Delphi單元如下(輸出hello.dll):

unit hellofun;interfacefunction getint():integer;stdcall;function sayhello(var sname:PAnsiChar):PAnsiChar;stdcall;implementationfunction 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;beginend.

python中調(diào)用如下:

import ctypesdef 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()

運(yùn)行Python,輸出如下:

>>> 888c_char_p(b'ok!')c_char_p(b'hello,garfield !')>>> 

好了,我們可以讓python完成部分功能在Delphi中調(diào)用,也可以用Delphi完成部分功能在Python中調(diào)用。

以上程序在DelphiXE2及Python3.2中調(diào)試通過。

總結(jié)

以上就是本文關(guān)于python調(diào)用Delphi寫的Dll代碼示例的全部內(nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 南澳县| 徐州市| 天峨县| 绥芬河市| 常德市| 清流县| 利辛县| 上思县| 锦屏县| 锡林郭勒盟| 曲水县| 安塞县| 丹棱县| 曲麻莱县| 江阴市| 四会市| 永昌县| 垦利县| 石家庄市| 韩城市| 灵川县| 五大连池市| 横山县| 芷江| 泰宁县| 安吉县| 老河口市| 西华县| 惠安县| 历史| 青龙| 高陵县| 博野县| 九龙城区| 海口市| 贺兰县| 曲沃县| 沙坪坝区| 东源县| 邵阳县| 阳新县|