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

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

本文关键字:获取 C++ 字符串 python ctypes wchar 使用      更新时间:2023-10-16

我可以:

  1. 从C++中获取一个整数并在python中使用它
  2. 发送一个 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:UsersDaanAppDataLocalProgramsPythonPython36libctypes__init__.py", line 504, in wstring_at
return _wstring_at(ptr, size)
OSError: exception: access violation reading 0xFFFFFFFF9D1C7000

我在 Linux 上重现了您的问题,并通过定义DoC函数的返回类型来纠正它:

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 中分配缓冲区而不是使用全局变量,或者只返回宽字符串常量,这将是线程安全的。 下面是为 Microsoft 编译器编码的示例:

测试.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] = '';
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