How to access the value of a ctypes.LP_c_char pointer?
<p>I have defined a struct : </p>

<pre><code>class FILE_HANDLE(Structure):
_fields_ = [
    ("handle_bytes", c_uint),
    ("handle_type", c_int),
    ("f_handle", POINTER(c_char))
]
</code></pre>

<p>The struct is initialised :</p>

<pre><code>buf = create_string_buffer(f_handle.handle_bytes)
fh = FILE_HANDLE(c_uint(8), c_int(0), buf)
</code></pre>

<p>I am passing it by reference to a function that populates it.</p>

<pre><code>ret = libc.name_to_handle_at(dirfd, pathname, byref(fh), byref(mount_id), flags)
</code></pre>

<p>I can check with strace that the call works, but I have not been able to figure out how to access the value of fh.f_handle</p>

<p>fh.f_handle type is <code>&lt;ctypes.LP_c_char object at 0x7f1a7ca17560&gt;</code><br>
fh.f_handle.contents type is <code>&lt;ctypes.LP_c_char object at 0x7f1a7ca17560&gt;</code> but I get a SIGSEGV if I try to access its value.<br>
How could I get 8 bytes from f_handle into a string or array ?</p>

<p>Everything actually looks right for what you've shown, but without seeing the explicit C definition of the structure and function you are calling it is difficult to see the problem.</p>

<p>Here's an example that works with what you have shown.  I inferred what the C definitions <em>should</em> be from what you have declared in Python, but most likely your definition is different if you get a segfault.</p>

<h3>C Code (Windows)</h3>

<pre><code>struct FILE_HANDLE
{
    unsigned int handle_bytes;
    int handle_type;
    char* f_handle;
};

__declspec(dllexport) int name_to_handle_at(int dirfd, char* pathname, struct FILE_HANDLE* fh, int* mount_id, int flags)
{
    unsigned int i;
    printf("dirfd=%d pathname=%s fh-&gt;handle_bytes=%u fh-&gt;handle_type=%d flags=%d\n", dirfd, pathname, fh-&gt;handle_bytes, fh-&gt;handle_type, flags);
    for(i = 0; i &lt; fh-&gt;handle_bytes; ++i)
        fh-&gt;f_handle[i] = 'A' + i;
    *mount_id = 123;
    return 1;
}
</code></pre>

<h3>Python code (Works in Python 2 and 3):</h3>

<pre><code>from __future__ import print_function
from ctypes import *

class FILE_HANDLE(Structure):
    _fields_ = [("handle_bytes", c_uint),
                ("handle_type", c_int),
                ("f_handle", POINTER(c_char))]

buf = create_string_buffer(8);
fh = FILE_HANDLE(8,0,buf)
libc = CDLL('test.dll')
mount_id = c_int(0)
ret = libc.name_to_handle_at(1,b'abc',byref(fh),byref(mount_id),7)
print('mount_id =',mount_id.value)
print('fh.f_handle =',fh.f_handle[:fh.handle_bytes])
</code></pre>

<h3>Output</h3>

<pre><code>dirfd=1 pathname=abc fh-&gt;handle_bytes=8 fh-&gt;handle_type=0 flags=7
mount_id = 123
fh.f_handle = b'ABCDEFGH'
</code></pre>

<p>Note that since the structure is declared as a pointer to a single character, printing <code>fh.f_handle.contents</code> would only print <code>b'A'</code>.  Using slicing, I've instructed Python to index the pointer up to the length allocated.</p>

<p>If this doesn't work for you, provide a <a href="http://stackoverflow.com/help/mcve">Minimal, Complete, and Verifiable example</a> (as I have) to reproduce your error exactly.</p>

python ctypes