简体   繁体   English

传递Python的对象作为函数的参数

[英]Python-passing object as an argument of function

  1. There is a function from a DLL (C language) link("parameters", &connection); 有一个来自DLL(C语言) link("parameters", &connection);的函数link("parameters", &connection); which takes a string parameter and initializes a connection. 它采用字符串参数并初始化连接。

  2. There is a function connect(connection) , where the connection - the object, initialized with a call to link() . 有一个函数connect(connection) ,其中连接-对象,通过调用link()初始化。

Question: how to pass Python connection object function Connect () as an argument? 问题:如何通过Python连接对象函数Connect()作为参数?

from ctypes import *
mydll = CDLL (link.dll)
# How define the connection object?
top = link ("localhost: 5412", connection)
top = connect (connection)

I believe that winapi handle is 32 bits long, but you might need to ensure that it be that the true length. 我相信winapi句柄是32位长,但是您可能需要确保它是真实的长度。 If you are not using connection in any other way, maybe you can do this: 如果您没有以其他任何方式使用连接 ,则可以执行以下操作:

connection_t = ctypes.c_uint32

link = mydll.link
link.argtypes=(ctypes.c_char_p, ctypes.POINTER(connection_t) )
connect = mydll.connect
connect.argtypes=(connection_t,)
...
connection = connection_t()
link ("localhost: 5412", ctypes.byref(connection))
...

Note that: 注意:

  • You might use another type as the opaque equivalent to HANDLE, as long as it has the same byte-length. 您可以使用另一种类型作为与HANDLE等效的不透明对象,只要它具有相同的字节长度即可。 Even better, you can "protect" your created connection _t type by inheriting instead of just assigning. 更好的是,您可以通过继承而不是仅仅通过分配来“保护”您创建的连接 _t类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM