Вопрос

I am trying to pass a Python string to a c function (DLL) using ctypes. The function needs a const char*. I am using create_string_buffer, but it returns an error: raise TypeError(init) TypeError:

My python code is quite simple:

testUpdate = myDll['testUpdate']
def testUpdate(Path):
    p_path = ctypes.create_string_buffer(Path)
    print ctypes.sizeof(p_path), repr(p_path.raw)
    print repr(p_path.value)

    testUpdate(p_path)
    testUpdate.restype = ctypes.c_char

And my C code is even simpler:

char testUpdate(const char* softwareToolsPath){
     char p;
     p = *softwareToolsPath;
     return p;

}

I saw this post: create_string_buffer throwing error TypeError: str/bytes expected instead of str instance But it didn't help. Could anybody help?

I am using python 2.7.

To avoid any confusion, all I'm trying to do is give a Path (using Python) to a DLL. That DLL will open the file pointed by the path and do its magic. So I am simply trying to write a little wrapper for a existing C function.

Это было полезно?

Решение

I am confused. Is testUpdate(p_path) supposed to call a C function? It looks like it recursively calls the python code (with the wrong parameter type). Try to rename the Python function.

BTW, Your C code is faulty. I suppose what you want is

char * testUpdate(const char * softwareToolsPath) {
    char * p;
    p = softwareToolsPath;
    return p;
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top