python調用Delphi寫的Dll代碼示例
更新時間:2017年12月05日 09:05:01 作者:garfieldtom
這篇文章主要介紹了python調用Delphi寫的Dll代碼示例,具有一定參考價值,需要的朋友可以了解下。
首先看下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 //程序代碼實現(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中調用如下:
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代碼示例的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
相關文章
合并Excel工作薄中成績表的VBA代碼,非常適合教育一線的朋友
每次學生考試,評分完畢之后,把每個科的成績收集起來,就得到了一個有若干工作表,每個表有學生學號、分數(shù)等列的Excel工作薄。2009-04-04python使用pynput庫操作、監(jiān)控你的鼠標和鍵盤
這篇文章主要介紹了python使用pynput庫操作、監(jiān)控你的鼠標和鍵盤,幫助大家更好的理解和學習使用python,感興趣的朋友可以了解下2021-03-03Python 合并多個TXT文件并統(tǒng)計詞頻的實現(xiàn)
這篇文章主要介紹了Python 合并多個TXT文件并統(tǒng)計詞頻的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-08-08