python調(diào)用Delphi寫的Dll代碼示例
首先看下Delphi單元文件基本結(jié)構(gòu):
unit Unit1; //單元文件名 interface //這是接口關(guān)鍵字,用它來(lái)標(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; 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中調(diào)用如下:
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()
運(yùn)行Python,輸出如下:
>>> 888 c_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代碼示例的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
相關(guān)文章
Python中多線程的創(chuàng)建及基本調(diào)用方法
由于注明的GIL的存在,Python盡管能創(chuàng)建多個(gè)線程,但是多線程卻不能同時(shí)工作...well,這里我們來(lái)看一下Python中多線程的創(chuàng)建及基本調(diào)用方法2016-07-07合并Excel工作薄中成績(jī)表的VBA代碼,非常適合教育一線的朋友
每次學(xué)生考試,評(píng)分完畢之后,把每個(gè)科的成績(jī)收集起來(lái),就得到了一個(gè)有若干工作表,每個(gè)表有學(xué)生學(xué)號(hào)、分?jǐn)?shù)等列的Excel工作薄。2009-04-04python處理json文件的四個(gè)常用函數(shù)
這篇文章主要介紹了python處理json文件的四個(gè)常用函數(shù),主要包括json.load()和json.dump()及json.loads()還有json.dumps(),需要的朋友可以參考一下2022-07-07python使用pynput庫(kù)操作、監(jiān)控你的鼠標(biāo)和鍵盤
這篇文章主要介紹了python使用pynput庫(kù)操作、監(jiān)控你的鼠標(biāo)和鍵盤,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-03-03Python 合并多個(gè)TXT文件并統(tǒng)計(jì)詞頻的實(shí)現(xiàn)
這篇文章主要介紹了Python 合并多個(gè)TXT文件并統(tǒng)計(jì)詞頻的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08信號(hào)生成及DFT的python實(shí)現(xiàn)方式
今天小編就為大家分享一篇信號(hào)生成及DFT的python實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-02-02Python光學(xué)仿真學(xué)習(xí)處理高斯光束分布圖像
這篇文章主要為大家介紹了Python光學(xué)仿真學(xué)習(xí)之如何處理高斯光束的分布圖像,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-10-10Python中實(shí)現(xiàn)常量(Const)功能
這篇文章主要介紹了Python中實(shí)現(xiàn)常量(Const)功能,python語(yǔ)言本身沒有提供const,本文使用一個(gè)類來(lái)實(shí)現(xiàn)常量定義功能,并介紹了使用方法,需要的朋友可以參考下2015-01-01