这里是我的C代码签名
typedef struct ApiReturn
{
int error;
char* errorMessage;
} ApiReturn;
// if this call fails, we'll declare instantiate the errorMessage pointer and
// set the appropriate value for error.
ApiReturn DoSomething();
// this frees the memory in clear.
void Api_Clear(char* clear);
而这里的ctypes的代码:
class ApiReturn(Structure):
_fields_ = [('error', c_int),
('errorMessage', c_char_p)]
def check_api_return(api_return):
# 0 means api call succeeded.
if api_return.error != 0:
get_global_lib().Api_Clear(api_return.errorMessage)
raise Exception('API call failed with' + api_return.errorMessage)
do_something = get_global_lib().DoSomething
do_something.restype = ApiReturn
这Python代码是错误的,因为api_return.errorMessage已实例化一个新的Python字符串,但我无法直接通过字段成员访问指针errorMessage。
库的其余部分按预期工作。我很感谢在这个问题上的任何帮助。