简体   繁体   English

如何使用ctypes从Python代码获取char指针的值

[英]How to get value of char pointer from Python code wtih ctypes

I want to use C library on Python. 我想在Python上使用C库。 Then, I want to get message ( char* ) from C library fanction. 然后,我想从C库功能获取消息(char *)。

I wrote these codes. 我写了这些代码。 I got result value(double* result_out) , but I didn't get message. 我得到了结果值(double * result_out),但没有收到消息。 This code shown "c_char_p(None)". 此代码显示为“ c_char_p(None)”。

Any ideas? 有任何想法吗?

I use Python 3.6, and Ubuntu Bash. 我使用Python 3.6和Ubuntu Bash。

C (libdiv.so): C(libdiv.so):

#define ERROR -1
#define OK     0

int div (double x, double y, char *msg, double *result_out) {
    static char *err_msg = "0 div error"; 
    if(y == 0) {
        msg = err_msg;
        return ERROR;
    }
    *result_out = x/y;
    return OK;
}

Python: 蟒蛇:

from ctypes import *

lib = cdll.Loadlibrary('libdiv.so')
errmsg = c_char_p()
result = c_double(0)
rtn = lib.div(10, 0, errmsg, byref(result))

if rtn < 0:
    print (errmsg)       # None    
else :
    print (result.value) # OK.

The primary problem here is that your C is busted. 这里的主要问题是您的C被破坏了。 Assigning a value to the msg argument doesn't do anything visible on the caller's end (just like if you'd tried to assign to an argument in a Python function). msg参数赋值并不会在调用者端产生任何可见的效果(就像您试图在Python函数中为参数赋值一样)。

If you want to actually make the error message string available to the caller of div , you need to take a char** , not a char* , and assign to *msg . 如果您想将错误消息字符串实际提供给div的调用者,则需要使用char**而不是char* ,并将其分配给*msg On the Python end, you'd pass something like byref(errmsg) . 在Python端,您将传递诸如byref(errmsg)

Aside from that, you need to set the argtypes and restype on lib.div , or Python won't know how to pass arguments correctly. 除此之外,您需要在lib.div上设置argtypesrestype ,否则Python将不知道如何正确传递参数。

To return a value as an output parameter, you need to pass a pointer to the value type returned. 要将值作为输出参数返回,您需要将指针传递给返回的值类型。 Just like you used double* to receive a double, you need a char** to receive a char* : 就像您使用double*来接收一个double一样,您需要一个char**来接收一个char*

#ifdef _WIN32
#   define API __declspec(dllexport)
#else
#   define API
#endif

#define OK     0
#define ERROR -1

API int div(double x, double y, char** ppMsg, double* pOut)
{
    static char* err_msg = "0 div error";
    if(y == 0)
    {
        *ppMsg = err_msg;
        return ERROR;
    }
    *pOut = x / y;
    return OK;
}

In Python, you'll also need to declare the argument types, or Python will marshal the values to C as c_int by default, which will break double and may break char* depending on the pointer implementation of your OS: 在Python中,您还需要声明参数类型,否则默认情况下Python会将值作为c_int送给C,这将导致double并且可能会破坏char*具体取决于操作系统的指针实现:

from ctypes import *

lib = CDLL('test')
lib.div.argtypes = c_double,c_double,POINTER(c_char_p),POINTER(c_double)
lib.div.restype  = c_int

errmsg = c_char_p()
result = c_double()
rtn = lib.div(10, 0, byref(errmsg), byref(result))

if rtn < 0:
    print(errmsg.value)
else:
    print(result.value)

Output: 输出:

b'0 div error'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM