wahab
wahab

Reputation: 835

Python passing char * as an argument to a function

I have created a python module from a C++ static library using the Cython add-on. The C++ library provides a function for listing the contents of a folder. It provides the name of the directory whose contents are to be displayed, as well as an output buffer where the contents of the directory will be stored, and of course the length of the output buffer. The C++ definition of the function is as follows:

int ListItems(char *dir, char *buf, int buflen)

The python module then in turn has a function PyListItems which simply calls this function:

def PyListItems(char *dir, char *buf, int buflen):
    return ListItems(dir,buf,buflen)`

In my script where I am using this function, I define a string buffer and a pointer to it of type c_char_p, and pass it to the function in place of the buf argument:

buf = c_types.create_string_buffer(100)
pBuf = c_char_p(addressof(buf))
PyListItems('someFolder',pBuf,100)

But I am getting the following error:

TypeError: expected string or Unicode object, c_char_p found

Any help would be greatly appreciated.

Upvotes: 2

Views: 4028

Answers (1)

kirbyfan64sos
kirbyfan64sos

Reputation: 10727

pBuf = c_char_p(addressof(buf))

You don't need this. create_string_buffer already returns a character array. Just do:

buf = c_types.create_string_buffer(100)
PyListItems('someFolder',buf,100)

Upvotes: 2

Related Questions