0
votes

I have a DLL with a function

EXPORT long Util_funct( char *intext, char *outtext, int *outlen )

Looks like it expects char *intext, char *outtext, int *outlen. I wass trying to define differnt data types in python so i can pass an argument, but no success so far.

from ctypes import *

string1 = "testrr"
#b_string1 = string1.encode('utf-8')

dll = WinDLL('util.dll')
funct = dll.Util_funct

funct.argtypes = [c_wchar_p,c_char_p, POINTER(c_int)]
funct.restype = c_char_p

p = c_int()
buf = create_string_buffer(1024)
retval = funct(string1, buf, byref(p))

print(retval)

The output is None, but I see some changes in p. Could you please help me to define proper data types for the function.

2
The return type (restype) is c_long, not c_char_p. - 101
You may also need to use string1 = create_string_buffer('testrr') instead of string1 = 'testrr' if your function is expecting a mutable buffer. - 101

2 Answers

0
votes

This should work:

from ctypes import *

string1 = b'testrr'     # byte string for char*

dll = CDLL('util.dll')  # CDLL unless function declared __stdcall
funct = dll.Util_funct

funct.argtypes = c_char_p,c_char_p,POINTER(c_int) # c_char_p for char*
funct.restype = c_long # return value is long

p = c_int()
buf = create_string_buffer(1024) # assume this is big enough???
retval = funct(string1, buf, byref(p))

print(retval)
-1
votes

Thanks for all your answers! I think i figured it out. Using not the smartest way but just trying/experimenting with different data types. As this is not a common library and i had no information for it, maybe the sulution wont be very useful for others , but anyway.

Looks like the function process only one character at a time, because if i pass a word it returns only one encoded character. So here it is :

from ctypes import *


buf = create_unicode_buffer(1024)
string1 = "a"
c_s = c_wchar_p(string1)

dll = CDLL('util.dll')
enc = dll.Util_funct

enc.argtypes = c_wchar_p, c_wchar_p, POINTER(c_int)

enc.restype = c_long # i don't think this type matters at all

p = c_int()


enc(c_s, buf, byref(p))


print(p.value)
print(buf.value)

the output is 1 and the simbol ^

Thanks again