如何在使用 ctypes 和 wchar_t 时将字符串从 C++ 获取到 python?

How to get a string from C++ to python when using ctypes and wchar_t?

我可以:

  1. 从 C++ 获取一个整数并在 python
  2. 中使用它
  3. 将 python 字符串(作为 wchar_t)发送到 C++ 并用它做一些逻辑

我不能 反方向第 2 步。

这是我的 C++ 代码(使用 clion 和 cygwin 作为共享库使用 C++14 编译)。

#include <iostream>

wchar_t aa[2];
extern "C" {
    int DoA()
    {
        return 10;
    }
    int DoB(wchar_t  * in)
    {
        if (in[1] == 'a')
        {
            return 25;
        }
        return  30;
    }

    wchar_t * DoC()
    {
        aa[0] = 'a';
        aa[1] = 'b';
        return aa;
    }
}

这是我的 python 3.6.1 代码,显示了我能做什么和不能做什么。那么我应该如何获取我的字符串并在 python 中使用它呢?我希望使用带 wstring_at 的地址来获取值,但它不起作用。

from ctypes import *
import os.path
print('Hello')

itExist = os.path.exists('C:/Users/Daan/CLionProjects/stringproblem/cmake-build-release/cygstringproblem.dll')
print(itExist)
lib = cdll.LoadLibrary('C:/Users/Daan/CLionProjects/stringproblem/cmake-build-release/cygstringproblem.dll')
print('dll loaded')

A = lib.DoA()
print(A)
Bx = lib.DoB(c_wchar_p('aaa'))
print(Bx)
By = lib.DoB(c_wchar_p('bbb'))
print(By)
Ca = lib.DoC()
print(Ca)
print('Issue is coming')
Cb = wstring_at(Ca,2)
print(Cb)

这是错误的输出。

Hello

True

dll loaded

10

25

30

-1659080704

Issue is coming
Traceback (most recent call last):
  File "ShowProblem.py", line 19, in <module>
    Cb = wstring_at(Ca,2)
  File "C:\Users\Daan\AppData\Local\Programs\Python\Python36\lib\ctypes\__init__.py", line 504, in wstring_at
    return _wstring_at(ptr, size)
OSError: exception: access violation reading 0xFFFFFFFF9D1C7000

我在 Linux 上重现了你的问题,并通过从你的 DoC 函数中定义 return 类型来更正它:

from ctypes import *
print('Hello')

lib = cdll.LoadLibrary(PATH_TO_TOUR_LIB)
print('dll loaded')
# this line solved the issue for me
lib.DoC.restype = c_wchar_p

A = lib.DoA()
print(A)
Bx = lib.DoB(c_wchar_p('aaa'))
print(Bx)
By = lib.DoB(c_wchar_p('bbb'))
print(By)
Ca = lib.DoC()
print(Ca)
print('Issue is coming')
Cb = wstring_at(Ca,2)
print(Cb)

我也动态分配了内存(一些Python专家可能会评论这个,我猜这会导致内存泄漏):

extern "C" {
    int DoA()
    {
        return 10;
    }
    int DoB(wchar_t  * in)
    {
        if (in[1] == 'a')
        {
            return 25;
        }
        return  30;
    }

    wchar_t * DoC()
    {
        wchar_t* aa = new wchar_t[2];
        aa[0] = 'a';
        aa[1] = 'b';
        return aa;
    }
}

让我知道它是否适用于 Windows。

如果您设置包装函数的 .argtypes.restype,您可以更自然地调用它们。要处理输出字符串,如果您在 Python 中分配缓冲区而不是使用全局变量,或者只是 return 一个宽字符串常量,它将是线程安全的。这是为 Microsoft 编译器编码的示例:

test.c

#include <wchar.h>
#include <string.h>

__declspec(dllexport) int DoA(void) {
    return 10;
}

__declspec(dllexport) int DoB(const wchar_t* in) {
    if(wcslen(in) > 1 && in[1] == 'a') // Make sure not indexing past the end.
        return 25;
    return  30;
}

// This version good if variable data is returned.
// Need to pass a buffer of sufficient length.
__declspec(dllexport) int DoC(wchar_t* aa, size_t length) {
    if(length < 3)
        return 0;
    aa[0] = 'a';
    aa[1] = 'b';
    aa[2] = '[=10=]';
    return 1;
}

// Safe to return a constant.  No memory leak.
__declspec(dllexport) wchar_t* DoD(void) {
    return L"abcdefg";
}

test.py

from ctypes import *

# Set up the arguments and return type
lib = CDLL('test')
lib.DoA.argtypes = None
lib.DoA.restype = c_int  # default, but just to be thorough.
lib.DoB.argtypes = [c_wchar_p]
lib.DoB.restype = c_int
lib.DoC.argtypes = [c_wchar_p,c_size_t]
lib.DoC.restype = c_int
lib.DoD.argtypes = None
lib.DoD.restype = c_wchar_p

# Map to local namespace functions
DoA = lib.DoA
DoB = lib.DoB
DoD = lib.DoD

# Do some pre- and post-processing to hide the memory details.
def DoC():
    tmp = create_unicode_buffer(3)  # Writable array of wchar_t.
    lib.DoC(tmp,sizeof(tmp))
    return tmp.value  # return a Python string instead of the ctypes array.

print(DoA())
print(DoB('aaa'))
print(DoB('bbb'))
print(DoC())
print(DoD())

输出:

10
25
30
ab
abcdefg