【问题标题】:Python ctypes - Accessing data string in Structure .value failsPython ctypes - 访问结构.value 中的数据字符串失败
【发布时间】:2010-09-13 21:57:07
【问题描述】:

我能够通过 dll 函数获得一个填充的结构(因为它似乎使用x=buffer(MyData) 然后repr(str(buffer(x))) 来查看它。

但如果我尝试使用 .value 访问结构的元素,则会引发错误。

我有一个 VarDefs.h 需要这样的结构:

typedef struct
{
  char Var1[8+1];
  char Var2[11+1];
  char Var3[3+1];
...
}TMyData

应该传递给这样的函数:

__declspec(dllexport) int AFunction(TOtherData *OtherData, TMyData *MyData);

在 Python 中,我现在能够以这种方式声明结构(感谢 Martelli 先生:请参阅此处Python ctypes - dll function accepting structures crashes):

class TMyData( Structure ):

    _fields_ = [
        ("Var1" , type( create_string_buffer(9) ) ),
        ("Var2" , type( create_string_buffer(12)) ),
...

我这样调用函数:result = Afunction( byref(OtherData) , byref(MyData ) )

如前所述,当我尝试访问 MyData.Var1.value 时出现错误(抱歉,现在不能更具体了!),但 repr(str(x)) 其中 x 是 buffer(MyData) 的副本表明其中有数据它!

我应该怎么做呢?谢谢!

【问题讨论】:

  • 我在这里看到了一些可能有问题的地方,但是很难帮助您解决本质上“它不起作用”的问题。
  • 顺便说一句,我之前的评论并不是要作为被动攻击性的蛇,而是要求更准确的错误或堆栈跟踪:-)。

标签: python ctypes structure


【解决方案1】:

您尝试使用 ctypes 与之交互的结构包含几个“字符数组”而不是“指向字符数组的指针”。而不是使用create_string_buffer(9),您需要使用ctypes.c_char * 9

class TMyData( ctypes.Structure ):
   _fields_ = [ ("Var1", ctypes.c_char * 9),
                ("Var2", ctypes.c_char * 12), ... ]

【讨论】:

  • 他的那段代码肯定是单调的,而且比必要的更复杂,但 Python 将 (ctypes.c_char * 9) is type(ctypes.create_string_buffer(9)) 报告为 True,所以这不是发帖者的(主要)问题。
  • 这不是单调的。这就是在 ctypes 中声明数组的方式。
  • @Mark 我是说原始发布者的代码是单一的,而不是 Rakis 的更正。
  • @llasram:我的错,我没有读到“那段 他的 代码”足够近 :)
【解决方案2】:

只需使用print MyData.Var1。当通过Structure 实例访问时,字符数组被转换为 Python 字符串类型,该实例没有.value 方法。

人为的工作示例:

DLL 代码(x.c,用 MSVC 编译,带有“cl /LD x.c”)

#include <string.h>

typedef struct
{
    char Var1[5];
    char Var2[10];
    char Var3[15];
} TMyData;

__declspec(dllexport) int AFunction(TMyData *MyData)
{
    strcpy(MyData->Var1,"four");
    strcpy(MyData->Var2,"--nine---");
    strcpy(MyData->Var3,"---fourteen---");
    return 3;
}

Python 代码

import ctypes as c

class TMyData(c.Structure):
   _fields_ = [
        ("Var1", c.c_char * 5),
        ("Var2", c.c_char * 10),
        ("Var3", c.c_char * 15)]

lib = c.CDLL('x')
data = TMyData()
lib.AFunction(c.byref(data))

print data.Var1
print data.Var2
print data.Var3
print data.Var1.value # error!

输出

four
--nine---
---fourteen---
Traceback (most recent call last):
  File "C:\Python26\Lib\site-packages\Pythonwin\pywin\framework\scriptutils.py", line 436, in ImportFile
    my_reload(sys.modules[modName])
  File "C:\x.py", line 12, in <module>
    print data.Var1.value
AttributeError: 'str' object has no attribute 'value'

【讨论】:

  • 我不知道我是怎么弄糊涂的,但是在属性访问时Structure 实例会自动将任何基本类型的值转换为相应的 Python 类型。普通的ctypes 字符数组肯定有value 属性,所以你应该更新你的答案以防止混淆。
  • 我明白你的意思。我会解决的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-17
  • 1970-01-01
相关资源
最近更新 更多