Returning struct from c++ dll to Python

I'm trying to return structure so I can use it in Python. I am beginner programmer so please explain me what am I doing wrong. I've succeeded to return simple ctypes earlier (bool, unsigned int) but struct is too complicated for me. This is what I have:


#define DLLAPI extern "C" __declspec(dllexport)
DLLAPI myStruct* DLLApiGetStruct();


EDIT1: instead of TString, struct members type is wchar_t* now, but error I get is the same

typedef struct myStruct{
    wchar_t* id; 
    wchar_t* content; 
    wchar_t* message;
} myStruct;

DLLAPI myStruct* DLLApiGetStruct(){
    myStruct* test = new myStruct();
    test->id = _T("some id"); 
    test->content = _T("some content"); 
    test->message = _T("some message");
    return test;

here is my Python code:

class TestStruct(Structure):
    _fields_ = [
        ("id", c_wchar_p),
        ("content", c_wchar_p),
        ("message", c_wchar_p)
class SomeClass(object):
    def test(self):
        myDLL = cdll.LoadLibrary('myDLL.dll')
        myDLL.DLLApiGetStruct.restype = TestStruct
        result = myDLL.DLLApiGetStruct()
        print "result type: ", type(result)
        print "-"*30
        print "result: ",result
        print "-"*30
        print # line 152

this is what I get:

    result type:  <class 'Foo.TestStruct'>
    result:  <Foo.TestStruct object at 0x027E1210>
    Traceback (most recent call last):
    line 152, in test
    ValueError: invalid string pointer 0x00000002

TString I've used is std::wstring

Should type in myStruct be pointers or something instead TString? Please help me, I've spend 5 days trying to make this work.


As the others explained, the problem with version 1 of the question is the use of std::string which is not a valid type for interop.

Looking at version 2 of the question, your C++ and Python declarations do not match. The C++ code returns a pointer to the struct, but the Python code expects the struct to be returned by value.

You could change either C++ or Python to match the other.


DLLAPI myStruct DLLApiGetStruct()
    myStruct result; = L"some id";
    result.content = L"some content";
    result.message = L"some message";  
    return result;


myDLL.DLLApiGetStruct.restype = POINTER(TestStruct)

Obviously you must apply only one of these changes!

Note that in the C++ code I chose to use explicit wide strings with the L prefix rather than the _T() macro. The former matches wchar_t* and the latter is what you use with TCHAR. I would not recommend TCHAR these days, unless you need to support Win98.

c_wchar_p contains wchar_t *, not std::wstring

The problem is you are returning a structure containing std::string's but you are telling Python that the types are pointers to wchar_t. This has the same effect as doing the following in C++.

struct Foo
    std::string id; 
    std::string content; 
    std::string message;

struct Bar
    wchar_t* id; 
    wchar_t* content; 
    wchar_t* message;

Foo f;
Bar* = reinterpret_cast<Bar*>(&f);

Need Your Help

Change from paid to free play store

android google-play

I have a paid app on the store and I want to know if it is possible to change the price at a later date i.e increase or decrease the price as well as make it free.

Disable window scrolling leaving the scrollbar visible

javascript jquery html5 css3

How can I disable window scrolling but leave scrollbar visible?