将第三方共享库与 Python ctypes 集成

Integrating thirt-party shared library with Python ctypes

我需要编写一个 Python 程序来调用一些函数并从第三方共享库中获取 JSON 响应(一个 unicode 字符串)。据推测,该库是用 C++ 编写的。该库有一个包含以下内容的头文件:

#include <string>
#include <ExportLib.h>
// some code ignored here
typedef std::string UString;
using namespace std;
namespace ns1{
class DLL_PUBLIC S_API {
public:
static UString function1();
static UString function2();
// some code ignored here
};
}

这是我在 Python 中写的(尝试了 2.7 和 3.x):

from ctypes import *
lib1 = CDLL('lib1.so')
func1 = lib1.function1
func1.restype = c_wchar_p
result = func1()
print(result)

但代码显示的不是 JSON,而是问号。我确实尝试了以下操作,但出现了段错误:

func1.restype = POINTER(c_wchar_p)
result = func1()
result1 = wstring_at(result)

请指出正确的方向,因为我不是一个很好的程序员。

UPD:忘了说,我正在 Linux 系统上工作。 UPD2:要解决这个需要使用 Cython,感谢 erykson

感谢 eryksun,我发现无法将 ctypes 与 C++ 数据类型一起使用。打算用Cython解决问题