Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python ctypes处理wchar\u t**?_Python_Ctypes_Wchar T - Fatal编程技术网

如何使用python ctypes处理wchar\u t**?

如何使用python ctypes处理wchar\u t**?,python,ctypes,wchar-t,Python,Ctypes,Wchar T,我有一个动态C库(比如foo.so),其中有一个函数具有以下原型 wchar_t **foo(const char *); /* The structure of the return value is a NULL terminated (wchar_t **), each of which is also NULL terminated (wchar_t *) strings */ 现在我想使用ctypes模块从python通过这个API调用该函数 以下是我尝试过的片段: fro

我有一个动态C库(比如foo.so),其中有一个函数具有以下原型

wchar_t **foo(const char *);

/*
  The structure of the return value is a NULL terminated (wchar_t **),
  each of which is also NULL terminated (wchar_t *) strings
*/
现在我想使用ctypes模块从python通过这个API调用该函数

以下是我尝试过的片段:

from ctypes import *

lib = CDLL("foo.so")

text = c_char_p("a.bcd.ef")
ret = POINTER(c_wchar_p)
ret = lib.foo(text)
print ret[0]
但它显示了以下错误:

回溯(最近一次呼叫最后一次):

文件“/src/test.py”,第8行,在

打印ret[0]

TypeError:'int'对象没有属性'\uu\getitem\uu'

任何有助于在python中运行的东西都是非常值得赞赏的


注:我已经在示例C代码中交叉检查了foo(“a.bcd.ef”)的功能,返回指针是什么样子的缺少的步骤是定义
foo
的and:

from ctypes import *
from itertools import takewhile

lib = CDLL("foo")
lib.foo.restype = POINTER(c_wchar_p)
lib.foo.argtypes = [c_char_p]

ret = lib.foo('a.bcd.ef')

# Iterate until None is found (equivalent to C NULL)
for s in takewhile(lambda x: x is not None,ret):
    print s
简单(Windows)测试DLL:

#include <stdlib.h>

__declspec(dllexport) wchar_t** foo(const char *x)
{
    static wchar_t* y[] = {L"ABC",L"DEF",L"GHI",NULL};
    return &y[0];
}

缺少的步骤是定义
foo
的和:

from ctypes import *
from itertools import takewhile

lib = CDLL("foo")
lib.foo.restype = POINTER(c_wchar_p)
lib.foo.argtypes = [c_char_p]

ret = lib.foo('a.bcd.ef')

# Iterate until None is found (equivalent to C NULL)
for s in takewhile(lambda x: x is not None,ret):
    print s
简单(Windows)测试DLL:

#include <stdlib.h>

__declspec(dllexport) wchar_t** foo(const char *x)
{
    static wchar_t* y[] = {L"ABC",L"DEF",L"GHI",NULL};
    return &y[0];
}