I'm new to Ctypes and trying to make a wrapper to use some dll functions but i'm facing a problem first here is the code
C function structure
long func(char *pID);
Python code
lib = WinDLL("some.dll")
lib.func.restype = c_long
lib.func.argtypes = [c_char_p]
ID = c_char()
lib.func(byref(ID))
print(ID)
this outputs 8 which is correct but only the first character
the problem is I need the complete output not just the first char. I replaced it with c_char_p it give this c_char_p(925904440) which is a pointer but when I print its value (ID.value) it print an empty string while c_char was giving the correct value why?
also tried create_string_buffer(10) but gives an error
expected LP_c_char_p instance instead of pointer to c_char_Array_10
Notice: I'm writing a J2534 wrapper since all the libraries that I had found have some kind of error tried python libs and C# libs if you know or have a working J2534 library then send me its link
You could use create_string_buffer. Documentation says:
ctypes.create_string_buffer(init_or_size, size=None)
This function creates a mutable character buffer. The returned object is a ctypes array of c_char.
init_or_size must be an integer which specifies the size of the array, or a bytes object which will be used to initialize the array items.
see https://docs.python.org/3/library/ctypes.html#ctypes.create_string_buffer
An example could look like this:
from ctypes import *
lib = cdll.LoadLibrary("some.dll")
lib.func.restype = c_long
lib.func.argtype = c_char_p
buf = create_string_buffer(9)
lib.func(buf)
print(buf.value.decode("utf-8"))
A simple test function on the C-side could look like this:
#include <string.h>
static const char some_data[] = "8.07.696";
long func(char *p) {
strcpy(p, some_data);
return 1L;
}
This prints the expected result to the debug console:
8.07.696
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.