Is it possible to pass a python string by reference through ctypes?

Assigning a new value to instances of the pointer types c_char_p, c_wchar_p, and c_void_p changes the memory location they point to, not the contents of the memory block (of course not, because Python strings are immutable): s = " World" >>> c_s = c_char_p(s) >>> print c_s c_char_p(' World') >>> c_s. Value = " there" >>> print c_s c_char_p(' there') >>> print s # first string is unchanged World You should be careful, however, not to pass them to functions expecting pointers to mutable memory. If you need mutable memory blocks, ctypes has a create_string_buffer function which creates these in various ways.

The current memory block contents can be accessed (or changed) with the raw property, if you want to access it as NUL terminated string, use the string property: Says the ctypes tutorial. What I gather from this is that only if the function would work with a const char would passing in the python string be valid. Keep in mind, it won't have a null termination I'd suggest using create_string_buffer anyhow.

Assigning a new value to instances of the pointer types c_char_p, c_wchar_p, and c_void_p changes the memory location they point to, not the contents of the memory block (of course not, because Python strings are immutable): >>> s = " World" >>> c_s = c_char_p(s) >>> print c_s c_char_p(' World') >>> c_s. Value = " there" >>> print c_s c_char_p(' there') >>> print s # first string is unchanged World >>> You should be careful, however, not to pass them to functions expecting pointers to mutable memory. If you need mutable memory blocks, ctypes has a create_string_buffer function which creates these in various ways.

The current memory block contents can be accessed (or changed) with the raw property, if you want to access it as NUL terminated string, use the string property: Says the ctypes tutorial. What I gather from this is that only if the function would work with a const char*, would passing in the python string be valid. Keep in mind, it won't have a null termination.

I'd suggest using create_string_buffer anyhow.

Thanks, you are entirely correct. I even vaguely remember reading this before. I despise the ctypes docs for putting key information in the tutorials.

– porgarmingduod Apr 7 at 9:07 I hear you on that. Python documents are great, except when you need to look something up. Then you'll need to read everything related to what you want to do.

I can't stress enough though, the string you pas in will not be null terminated. So do not use any std::string or string. H functions on them.) – nxzdr Apr 7 at 9:09 a Python string passed to ctypes will be nul-terminated.

It is valid to pass a Python string to a function that takes the c_char_p type. – Mark Tolonen Apr 12 at 4:42 it'll only be null terminated is you pass it through create_string_buffer. Python strings have no null terminator.

– nxzdr Apr 12 at 17:26.

The type ctypes. C_char_p represents a nul-terminated string. If a function takes a const char* you can pass a Python string to it and it will receive a nul-terminated version.

A Windows example DLL: #include __declspec(dllexport) char* func(char* a,size_t len,const char* b) { if(strlen(b) * 2 >= len) return NULL; strcpy_s(a,len,b); strcat_s(a,len,b); return a; } Python: Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) MSC v.1500 32 bit (Intel) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> from ctypes import * >>> x=CDLL('x') >>> x.func. Restype=c_char_p >>> x.func.

Argtypes=c_char_p,c_int,c_char_p >>> s=create_string_buffer(10) >>> x. Func(s,len(s),'abcd') 'abcdabcd.

Says the ctypes tutorial. What I gather from this is that only if the function would work with a const char*, would passing in the python string be valid. Keep in mind, it won't have a null termination.

I'd suggest using create_string_buffer anyhow.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions