pass Python string to C char *

View: New views
4 Messages — Rating Filter:   Alert me  

pass Python string to C char *

by Adam Walley :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi, All.

This question has probably been resolved many times over on various forums, but I was wondering what the best way is to pass a Python string (in my case a file path) to a DLL written in C that expects a char* input?

Any suggestions?

Adam



_______________________________________________
PythonCE mailing list
PythonCE@...
http://mail.python.org/mailman/listinfo/pythonce

Parent Message unknown Re: pass Python string to C char *

by Adam Walley :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.
Hi, Jared.
 
Thanks for the tip. I came across c_char_p("mystring"), but have not been able to get it working. I also attempted a create_string_buffer("mystring"), but also without success. I have not checked this fully yet, so may still get it working...
 
Adam.

 
On 19/08/2008, Jared Forsyth <jabapyth@...> wrote:
i believe its ctypes.c_str("mystring"). or maybe its c_char

On Tue, Aug 19, 2008 at 12:25 PM, Adam Walley <adam.walley@...> wrote:
Hi, All.

This question has probably been resolved many times over on various forums, but I was wondering what the best way is to pass a Python string (in my case a file path) to a DLL written in C that expects a char* input?

Any suggestions?

Adam


 

_______________________________________________
PythonCE mailing list
PythonCE@...
http://mail.python.org/mailman/listinfo/pythonce


 


_______________________________________________
PythonCE mailing list
PythonCE@...
http://mail.python.org/mailman/listinfo/pythonce

Re: pass Python string to C char *

by Adam Walley :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Well, it works! I realised I had not updated the DLL on my device (that's what comes of working late). Just for the record, I have tested two methods, and both work fine to pass a string to a C dll requiring a char * input:

1. use mystr1=c_char_p("mystring")
2. use mystr2=create_string_buffer("mystring")

Both methods require the ctypes module and work equally well with the DLL, but there are some differences (all of which I am not aware at the moment). Method 1 creates a ctypes Array object, whereas method 2 appears to create something which behaves more like a standard Python string.

Adam

2008/8/20 Adam Walley <adam.walley@...>
Hi, Jared.
 
Thanks for the tip. I came across c_char_p("mystring"), but have not been able to get it working. I also attempted a create_string_buffer("mystring"), but also without success. I have not checked this fully yet, so may still get it working...
 
Adam.

 
On 19/08/2008, Jared Forsyth <jabapyth@...> wrote:
i believe its ctypes.c_str("mystring"). or maybe its c_char

On Tue, Aug 19, 2008 at 12:25 PM, Adam Walley <adam.walley@...> wrote:
Hi, All.

This question has probably been resolved many times over on various forums, but I was wondering what the best way is to pass a Python string (in my case a file path) to a DLL written in C that expects a char* input?

Any suggestions?

Adam


 

_______________________________________________
PythonCE mailing list
PythonCE@...
http://mail.python.org/mailman/listinfo/pythonce


 



_______________________________________________
PythonCE mailing list
PythonCE@...
http://mail.python.org/mailman/listinfo/pythonce

Re: pass Python string to C char *

by Alexandre Delattre :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

 > Well, it works! I realised I had not updated the DLL on my device (that's
 > what comes of working late). Just for the record, I have tested two
methods,
 > and both work fine to pass a string to a C dll requiring a char * input:

1. use mystr1=c_char_p("mystring")
2. use mystr2=create_string_buffer("mystring")

 > Both methods require the ctypes module and work equally well with the
DLL,
 > but there are some differences (all of which I am not aware at the
moment).
 > Method 1 creates a ctypes Array object, whereas method 2 appears to
create
 > something which behaves more like a standard Python string.

 From my experience, it sounds to me that c_char_p creates a wrapper
over an existing python string that is suitable for being passed as
argument in a ctypes dll function.

create_string_buffer will duplicate the content of the string in an
internal buffer and the resulting object will provide a fixed-length
array interface to modify this buffer from python.

This one should be used when the c function will modify the char*
argument in place (i.e. write directly in this buffer). Since python
strings are supposed to be immutable (with the benefit of being hashable
and so insertable as keys in a dict) first method should not be used here.

cheers,
Alexandre

_______________________________________________
PythonCE mailing list
PythonCE@...
http://mail.python.org/mailman/listinfo/pythonce