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 //程序代码实现部分 

{$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代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

(0)

相关推荐

  • c++生成dll使用python调用dll的方法

    第一步,建立一个CPP的DLL工程,然后写如下代码,生成DLL 复制代码 代码如下: #include <stdio.h> #define DLLEXPORT extern "C" __declspec(dllexport) DLLEXPORT int __stdcall hello()     {         printf("Hello world!\n");         return 0;     } 第二步,编写一个 python 文件:

  • Python 调用VC++的动态链接库(DLL)

    1. 首先VC++的DLL的导出函数定义成标准C的导出函数: 复制代码 代码如下: #ifdef LRDLLTEST_EXPORTS #define LRDLLTEST_API __declspec(dllexport) #else #define LRDLLTEST_API __declspec(dllimport) #endif extern "C" LRDLLTEST_API int Sum(int a , int b); extern "C" LRDLLTE

  • python引用DLL文件的方法

    本文实例讲述了python引用DLL文件的方法.分享给大家供大家参考.具体分析如下: 在python中调用dll文件中的接口比较简单,如我们有一个test.dll文件,内部定义如下: extern "C" { int __stdcall test( void* p, int len) { return len; } } 在python中我们可以用以下两种方式载入 1. import ctypes dll = ctypes.windll.LoadLibrary( 'test.dll' )

  • Python获取DLL和EXE文件版本号的方法

    本文实例讲述了Python获取DLL和EXE文件版本号的方法.分享给大家供大家参考.具体实现方法如下: 复制代码 代码如下: import win32api def getFileVersion(file_name):     info = win32api.GetFileVersionInfo(file_name, os.sep)     ms = info['FileVersionMS']     ls = info['FileVersionLS']     version = '%d.%d

  • Python 调用DLL操作抄表机

    # -*- coding: GBK -*- from ctypes import * dll = windll.LoadLibrary('JBA188.dll') a = dll.test() print '测试设备连接状态%s' % a srcName = c_char_p("publish_pd.bin") decName = c_char_p('d:\\publish_pd.bin') b = dll.upfile(srcName,decName) print '将文件上传至计算

  • 用Python遍历C盘dll文件的方法

    python 的fnmatch还真是省心,相比于 java 中的FilenameFilter,真是好太多了,你完成不需要去实现什么接口. fnmatch 配合 os.walk() 或者 os.listdir() ,你能做的事太多了,而且用起来相当 easy. # coding: utf-8 """ 遍历C盘下的所有dll文件 """ import os import fnmatch def main(): f = open('dll_list.t

  • python调用Delphi写的Dll代码示例

    首先看下Delphi单元文件基本结构: unit Unit1; //单元文件名 interface //这是接口关键字,用它来标识文件所调用的单元文件 uses //程序用到的公共单元 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs; type //这里定义了程序所用的组件,一些类,以及组件所对应的过程.事件 TForm1 = class(TForm) private //定义私

  • Python文件的读写和异常代码示例

    一.从文件中读取数据 #!/usr/bin/env python with open('pi') as file_object: contents = file_object.read() print(contents) =================================== 3.1415926 5212533 2324255 1.逐行读取 #!/usr/bin/env python filename = 'pi' with open(filename) as file_obje

  • python shell根据ip获取主机名代码示例

    这篇文章里我们主要分享了python中shell 根据 ip 获取 hostname 或根据 hostname 获取 ip的代码,具体介绍如下. 笔者有时候需要根据hostname获取ip 比如根据machine.company.com 获得ip 10.173.14.117 方法1:利用 socket 模块 里的 gethostbyname 函数 代码如下,使用socket模块 >>> import socket >>> socket.gethostbyname(&qu

  • Python实现比较扑克牌大小程序代码示例

    是Udacity课程的第一个项目. 先从宏观把握一下思路,目的是做一个比较德州扑克大小的问题 首先,先抽象出一个处理的函数,它根据返回值的大小给出结果. 之后我们在定义如何比较两个或者多个手牌的大小,为方便比较大小,我们先对5张牌进行预处理,将其按照降序排序,如下: def card_ranks(hand): ranks = ['--23456789TJQKA'.INDEX(r) for r, s in hand] ranks.sort(reverse=True) return ranks 然后

  • Python调用飞书发送消息的示例

    一.创建飞书机器人 自定义飞书机器人操作步骤,具体详见飞书官方文档:<机器人 | 如何在群聊中使用机器人?> 二.调用飞书发送消息 自定义机器人添加完成后,就能向其 webhook 地址发送 POST 请求,从而在群聊中推送消息了.支持推送的消息格式有文本.富文本.图片消息,也可以分享群名片等. 参数msg_type代表消息类型,可传入:text(文本)/ post(富文本)/ image(图片)/ share_chat(分享群名片)/ interactive(消息卡片),可参照飞书接口文档:

  • pytorch教程实现mnist手写数字识别代码示例

    目录 1.构建网络 2.编写训练代码 3.编写测试代码 4.指导程序train和test 5.完整代码 1.构建网络 nn.Moudle是pytorch官方指定的编写Net模块,在init函数中添加需要使用的层,在foeword中定义网络流向. 下面详细解释各层: conv1层:输入channel = 1 ,输出chanael = 10,滤波器5*5 maxpooling = 2*2 conv2层:输入channel = 10 ,输出chanael = 20,滤波器5*5, dropout ma

  • 关于python调用c++动态库dll时的参数传递问题

    目录 string cv::Mat string C++生成dll代码: #include <iostream> extern "C" __declspec(dllexport) int get_str_length(char *str); int get_str_length(char *in_str) { std::string str(in_str); return str.length(); } 将VS_create_dll.dll放在与python相同文件夹下.p

  • Python爬取附近餐馆信息代码示例

    本代码主要实现抓取大众点评网中关村附近的餐馆有哪些,具体如下: import urllib.request import re def fetchFood(url): # 模拟使用浏览器浏览大众点评的方式浏览大众点评 headers = {'User-Agent', 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36'} ope

  • Python使用Turtle模块绘制五星红旗代码示例

    在Udacity上课时学到了python的turtle方法,这是一个很经典的用来教小孩儿编程的图形模块,最早起源于logo语言.python本身内置了这个模块,其可视化的方法可以帮助小孩儿对编程的一些基本理念有所理解. 在作业提交的论坛里看到很多turtle画出来的精美图形,想不出什么要画的东西,于是决定拿五星红旗来练练手. 前期准备 五星红旗绘制参数 Turtle官方文档 turtle的基本操作 # 初始化屏幕 window = turtle.Screen() # 新建turtle对象实例 i

  • Java调用JavaScript实现字符串计算器代码示例

    如果表达式是字符串的形式,那么一般我们求值都会遇到很大的问题. 这里有一种直接调用JavaScript的方法来返回数值,无疑神器. 代码如下: package scc; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; public class Counter { public static void main(String

随机推荐