title: Passing a C structure to a function in C DLL
body: <p>I've a <code>.DLL</code> which creates "named" a shared memory using some utility functions inside it</p>
  
  <p>I need to write/read following C <code>struct</code> via python using functions having following prototype:</p>
  
  <ul>
  <li><code>int write_shmem(const char* shmem_name, char* shmem)</code> </li>
  <li><code>int read_shmem ( const char* shmem_name , char* shmem);</code></li>
  </ul>
  
  <p>in the <code>.DLL</code> file</p>
  
  <ul>
  <li><code>shmem_name</code> is shared memory name</li>
  <li><code>shmem</code> is the data to be written</li>
  </ul>
  
  <p>The C <code>struct</code> is similar to</p>
  
  <pre><code>typedef struct {
     unsigned char c;
     unsigned long l;
     float f;
     double d;
   } DataBlock ;
  </code></pre>
  
  <p>I'm using following in python code</p>
  
  <pre><code>from ctypes import *
  dll = cdll.LoadLibrary('shared_mem.dll')
  write_shmem = dll.write_shmem
  write_shmem.restype = ctypes.c_int
  
  read_shmem = dll.read_shmem
  read_shmem.restype = ctypes.c_int
  
  class DataBlock(Structure):
      _fields_ = [('c', c_ubyte), ('l', c_ulong),
                  ('f',c_float), ('d', c_double) ]
  
  data = DataBlock('A',123, 12.32, 1.89721)
  
  write_shmem(c_char_p(b'P0W') , ??? ) #  cast(data, POINTER(c_char) ?
  
  #...
  
  read_shmem(c_char_p(b'P0W'),  ??? )  #  cast(data, POINTER(c_char) ?
  </code></pre>
  
  <p>How to typecast to <code>char*</code> ? </p>
  
  <p>Will using casting correctly puts the data to shared memory ? I need to read the same shared memory from a C application</p>
  
  <p><strong>Edit</strong></p>
  
  <p>Using:</p>
  
  <p><code>int create_shmem(const char*, long long );</code> from <code>.DLL</code> I'm creating a "named" shared memory</p>
  
  <p>Python code :</p>
  
  <pre><code>create_shmem( c_char_p(b'P0W'),
                 ctypes.c_longlong(sizeof(DataBlock)) )
  </code></pre>
  
  <p><code>write_shmem</code> and  <code>read_shmem</code> will simply write/read data by using the size of created memory. This is done in <code>.DLL</code> functions itself. (<code>Boost</code> Interprocess Shared Memory Object is used here)</p>
  
  <p><sup><em><strong>Using Python Version : 3.3.0</strong></em></sup></p>