将结构从c ++ dll返回到Python [英] Returning struct from c++ dll to Python
问题描述
我试图返回结构,所以我可以在Python中使用它。我是初学者程序员,所以请向我解释我做错了什么。我成功地返回简单的ctypes更早(bool,unsigned int),但结构对我来说太复杂了。这是我的:
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:
DLLAPI.h
#define DLLAPI extern "C" __declspec(dllexport)
...
DLLAPI myStruct* DLLApiGetStruct();
DLLAPI.cpp
DLLAPI.cpp
EDIT1:而不是TString,struct成员类型是wchar_t *现在,但是我得到的错误是相同的
...
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;
}
这里是我的Python代码:
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 result.id # line 152
这是我得到的:
result type: <class 'Foo.TestStruct'>
------------------------------
result: <Foo.TestStruct object at 0x027E1210>
------------------------------
Traceback (most recent call last):
....
....
....
line 152, in test
print result.id
ValueError: invalid string pointer 0x00000002
TString我使用的是std :: wstring
TString I've used is std::wstring
应该输入myStruct是指针还是东西,而不是TString?
Should type in myStruct be pointers or something instead TString? Please help me, I've spend 5 days trying to make this work.
推荐答案
正如其他人解释的那样,问题的版本1的问题是使用std :: string这不是一个有效的interop的类型。
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.
看看版本2的问题,你的C ++和Python声明不匹配。 C ++代码返回一个指向结构体的指针,但Python代码期望结构体通过值返回。
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.
你可以改变C ++或Python来匹配另一个。
You could change either C++ or Python to match the other.
C ++
DLLAPI myStruct DLLApiGetStruct()
{
myStruct result;
result.id = L"some id";
result.content = L"some content";
result.message = L"some message";
return result;
}
Python
myDLL.DLLApiGetStruct.restype = POINTER(TestStruct)
显然,你只能应用这些更改之一!
Obviously you must apply only one of these changes!
请注意,在C ++代码中,我选择使用带有L前缀的显式宽字符串,而不是_T()宏。前者匹配wchar_t *,后者是你使用TCHAR。我不会推荐TCHAR这些天,除非你需要支持Win98。
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 ++ dll返回到Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!