How to wrap a C function whose parameter is wchar_

2019-08-02 19:49发布

问题:

I want to use cython to wrap a C library. One function in the library is like

int hid_get_manufacturer_string(hid_device *device, wchar_t *string, size_t maxlen);

There are two questions:

  1. What can I do with the wchar_t in cython;

  2. How to convert the string pointer in my .pyx file.

回答1:

Declare wchar_t:

cdef extern from "stddef.h":
    ctypedef void wchar_t

Or import from libc module:

from libc.stddef cimport wchar_t

A function to convert wchar_t to python string by using WideCharToMultiByte (see CefStringToPyString):

# Declare these in .pxd file:
#
# cdef extern from "Windows.h":
#     cdef int CP_UTF8
#     cdef int WideCharToMultiByte(int, int, wchar_t*, int, char*, int, char*, int*)

cdef object WideCharToPyString(wchar_t *wcharstr):
    cdef int charstr_bytes = WideCharToMultiByte(CP_UTF8, 0, wcharstr, -1, NULL, 0, NULL, NULL)
    # Do not use malloc, otherwise you get trash data when string is empty.
    cdef char* charstr = <char*>calloc(charstr_bytes, sizeof(char))
    cdef int copied_bytes = WideCharToMultiByte(CP_UTF8, 0, wcharstr, -1, charstr, charstr_bytes, NULL, NULL)
    if bytes == str:
        pystring = "" + charstr # Python 2.7
    else:
        pystring = (b"" + charstr).decode("utf-8", "ignore") # Python 3
    free(charstr)
    return pystring

Since Python 3.2 you can do this with the use of PyUnicode_FromWideChar(wcharstr, -1), see the comment by compostus.