delphi调用python文件,Python通过指针到Delphi函数

I have dll, that builded in Delphi, and I try to call function from it. Declaration of function looks like this:

function GetUid(UID:Pointer):Integer; stdcall;

This is equivalent to this C function signature:

int GetUID(void *pointer);

Library handled using ctypes:

from ctypes import *

lib = cdll.LoadLibrary("mylib.dll")

But i stuck here:

res = lib.GetUid(?)

What I need to pass in this function?

Pointer is void *, but how make this rightly in python?

解决方案

Python is a high level language. You do not typically import a DLL from a C or Pascal native library and invoke it and pass variables from Python into a C or Pascal function taking a void * type raw pointer and then manipulate raw memory this way.

In short if you knew what you were doing you would know better than to try to do what you're doing here.

Let's suppose that your implementation is like this:

function GetUid(UID:Pointer):Integer; stdcall;

var

P2:^Integer;

begin

P2 := UID;

P2^ := 0;

end;

Then, what you would want to do is pass in an address to a 32 bit integer. Of course my example above is absurd, because what would have made sense above is to just declare the parameter as an "int *pointer" (in C terms) rather than as a "void *pointer".

Whatever it is you're doing, the next thing that will likely happen is that you will corrupt your python interpreter's heap, and cause lots of fun crashes and errors.

A far more sensible approach is to read the Python documentation on writing C extensions that can manipulate native Python types (PyObject), and doing the same thing but in pascal, if you like.

p4d appears to be a workable way of writing extension DLLs in delphi:

https://code.google.com/p/python4delphi/source/list

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值