ctypes & strings

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Thu Dec 25 01:46:39 EST 2008


En Wed, 24 Dec 2008 23:07:27 -0200, Red Rackham <redrackem at yahoo.com>
escribió:

> I would like to pass a string into a dll function.  I notice that to  
> pass using ctypes, it has to be a ctypes type.  Looking at the ctypes  
> doc page I don't see a c_string class. 

Because the C language doesn't have a string type?
Python strings are automatically converted to c_char_p if you set the
argtypes attribute.

> I tried to pass in byref("name of string") and got back "TypeError:  
> byref() argument must be a ctypes instance, not 'str'"

byref? Please post the prototype of the function you want to call. If it
takes a const char*, or a char* but doesn't modify it, you don't have to
use byref.
If it takes a char* and modifies it, use create_string_buffer (also, the
function SHOULD take the buffer size as an additional parameter...)

> If I use astr = create_string_buffer( "name of string" ), and try to  
> pass that in, I get "ValueError: Procedure probably called with too many  
> arguments (4 bytes in excess)".

Maybe you didn't set correctly the calling convention (cdecl? stdcall?).
How did you load the dll and get the function? Libraries loaded using
windll use the stdcall convention; cdll uses cdecl.

-- 
Gabriel Genellina




More information about the Python-list mailing list