marcocassisa
marcocassisa

Reputation: 49

Python ctypes - Accessing data string in Structure .value fails

I am able to get a Structure populated as a result of a dll-function (as it seems looking into it using x=buffer(MyData) and then repr(str(buffer(x))).

But an error is raised if I try to access the elements of the Structure using .value.

I have a VarDefs.h that requires a struct like this:

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

that should be passed to a function like this:

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

In Python I am now able to declare the structure this way (thanks to Mr. Martelli: see here Python ctypes - dll function accepting structures crashes ):

class TMyData( Structure ):

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

I call the function this way: result = Afunction( byref(OtherData) , byref(MyData ) )

As said, as I try to access MyData.Var1.value I get an error (sorry, can't be more specific now!), but repr(str(x)) where x is a copy of buffer(MyData) shows that there are data in it!

How should I do it instead? Thanks!

Upvotes: 4

Views: 5935

Answers (2)

Mark Tolonen
Mark Tolonen

Reputation: 177735

Just use print MyData.Var1. The character array is converted to a Python string type when accessed through a Structure instance, which doesn't have a .value method.

Contrived, working example:

DLL code (test.c, compiled with MSVC with "cl /LD test.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 code:

import ctypes as ct

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

lib = ct.CDLL('./test')
data = TMyData()
lib.AFunction(ct.byref(data))

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

Output:

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:\test.py", line 12, in <module>
    print data.Var1.value
AttributeError: 'str' object has no attribute 'value'

Upvotes: 3

Rakis
Rakis

Reputation: 7874

The structure you're trying to use ctypes to interface with contains a several "arrays of characters" not "pointers to arrays of characters". Rather than using create_string_buffer(9) you'll need to use ctypes.c_char * 9.

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

Upvotes: 3

Related Questions