0
votes

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.

1
I should have mentioned it, the argument is a valid str. So, that is not the problem :) - BaldDude
I'm not sure to see what information this method can give, other than what I already know. The error that I got pointed me to that file. I looked before, but I can't figure out the problem. As for comparing the output to the original. the output gives me : <class 'ctypes.c_char_Array_27'>. Am I missing something? (by the way thanks for your help) - BaldDude

1 Answers

1
votes

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;
}