查看: 35|回复: 0

    Python入门:Python调用Delphi写的Dll

    [复制链接]

    244

    主题

    244

    帖子

    585

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    585
    发表于 2022-6-17 16:15:49 | 显示全部楼层 |阅读模式
    Python入门:Python调用Delphi写的Dll

    Delphi版本Delphi 10 Seattle

    Delphi代码


    1. unit Unit1;

    2. interface

    3. function testint(): integer; stdcall;
    4. function testpchar(): PChar; stdcall;

    5. implementation

    6. function testint(): integer; stdcall;
    7. begin
    8.   result := 666;
    9. end;

    10. function testpchar(): PChar; stdcall;
    11. begin
    12.   result := '中文';
    13. end;
    复制代码
    1. library Project1;

    2. { Important note about DLL memory management: ShareMem must be the
    3.   first unit in your library's USES clause AND your project's (select
    4.   Project-View Source) USES clause if your DLL exports any procedures or
    5.   functions that pass strings as parameters or function results. This
    6.   applies to all strings passed to and from your DLL--even those that
    7.   are nested in records and classes. ShareMem is the interface unit to
    8.   the BORLNDMM.DLL shared memory manager, which must be deployed along
    9.   with your DLL. To avoid using BORLNDMM.DLL, pass string information
    10.   using PChar or ShortString parameters. }

    11. uses
    12.   System.SysUtils,
    13.   System.Classes,
    14.   Unit1 in 'Unit1.pas';

    15. {$R *.res}

    16. exports
    17.   testint,
    18.   testpchar;

    19. begin

    20. end.
    复制代码
    Python调用
    1. import ctypes

    2. dll = ctypes.windll.LoadLibrary("Project1.dll")
    3. ri = dll.testint()
    4. print(ri)

    5. rc = dll.testpchar()
    6. rc = ctypes.c_wchar_p(rc)
    7. print(rc.value)
    复制代码

    备注:


    ctypes定义了一系列基本C数据类型:


    00000000.png

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|小黑屋|源码资源网 ( 鲁ICP备2021043233号-3 )

    GMT+8, 2022-7-2 23:15

    邮箱:312337667@qq.com 客服QQ:312337667(工作时间:7:00~23:00)

    © Powered by Net188.com X3.4

    快速回复 返回顶部 返回列表