Я надеюсь на некоторую помощь с структурами и указателями ctypes.Python | ctypes struct return pointer
Вот мои 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
Этот питон код неправильно, потому что api_return.errorMessage имеет экземпляр новой строки Python, но я возникли проблемы с доступом к указателю errorMessage непосредственно через поле участник.
Остальная часть библиотеки работает должным образом. Я был бы признателен за любую помощь по этой проблеме.
Смотрите также [ctypes.create_string_buffer] (https://docs.python.org/2/library/ctypes.html#ctypes.create_string_buffer) и [ByRef()] (HTTPS: //docs.python .org/2/библиотека/ctypes.html # ctypes.byref) – theB