views:

130

answers:

2

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 http://stackoverflow.com/questions/3488173/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!

A: 

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), ... ]
Rakis
That bit of his code is certainly unidiomatic and more convoluted than necessary, but Python reports `(ctypes.c_char * 9) is type(ctypes.create_string_buffer(9))` as `True`, so that's not the poster's (primary) problem.
llasram
It's not unidiomatic. That's the way to declare an array in ctypes.
Mark Tolonen
@Mark I was saying the original poster's code was unidiomatic, not Rakis's correction.
llasram
@llasram: my bad, I didn't read "that bit of _his_ code" close enough :)
Mark Tolonen
+2  A: 

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 (x.c, compiled with MSVC with "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 Code

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!

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:\x.py", line 12, in <module>
    print data.Var1.value
AttributeError: 'str' object has no attribute 'value'
Mark Tolonen
I'm not sure how I got muddled, but on attribute access `Structure` instances automatically convert values of any basic type into the corresponding Python type. Normal `ctypes` character arrays definitely do have `value` attributes, so you should probably update your answer to prevent confusion.
llasram
I see what you mean. I'll fix it.
Mark Tolonen