Python通过指针到Delphi函数 [英] Python pass Pointer to Delphi function

查看:361
本文介绍了Python通过指针到Delphi函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有dll,建立在Delphi中,我尝试从它调用函数。功能声明如下:

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

这相当于这个C函数签名:

  int GetUID(void * pointer); 

使用ctypes处理的库:

  from ctypes import * 

lib = cdll.LoadLibrary(mylib.dll)

但我卡在这里:

  res = lib.GetUid(?)

我需要传递这个功能?
指针是void *,但是如何在python中正确使用?

解决方案

Python是一种高级语言。您通常不会从C或Pascal本机库导入DLL,并调用它,并将变量从Python传递到C或Pascal函数,并使用 void * 类型的原始指针,然后以这种方式处理原始记忆。



简而言之,如果你知道你在做什么,你会比在这里尝试做你想做的更好。

我们假设你的实现是这样的:

  function GetUid(UID:Pointer ):整数; STDCALL; 
var
P2:^整数;
begin
P2:= UID;
P2 ^:= 0;
结束

然后,您想要做的是将地址传递给32位整数。当然,我上面的例子是荒谬的,因为上面的意义就是将参数声明为int * pointer(C语言),而不是void * pointer。



无论你在做什么,接下来可能发生的事情是你会腐蚀你的Python解释器的堆,并造成很多有趣的崩溃和错误。



一个更加明智的做法是阅读Python文档,编写可以操作本机Python类型(PyObject)的C扩展,并且如果您喜欢,则执行相同的操作,但是在pascal中。



p4d似乎是一种在delphi中编写扩展DLL的可行方式:

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


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

这篇关于Python通过指针到Delphi函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆