title: python ctypes to pass a pointer to a pointer to a struct dynamically allocating
body: <p>I'm attempting to use python ctypes to pass the appropriate variables and pointers to a function in a DLL file.</p>
  <p>Immediately below is the function description provided in the DLL documentation:</p>
  <blockquote>
  <p>Prototype: Picam_GetAvailableDemoCameraModels( const PicamModel** model_array, piint*                      model_count )</p>
  <p>Description: Returns an allocated array of software-simulated camera models in model_array whose number of items is in model_count</p>
  <p>Notes: model_array is allocated by picam and must be freed with
  Picam_DestroyModels.</p>
  </blockquote>
  <p>Further note that piint is mapped to an integer and model_array is mapped to a struct.  My python syntax for doing this is as follows:</p>
  <pre><code>PicamStringSize_SensorName = PicamStringSize_SerialNumber = 64
  PicamModel = PicamComputerInterface = ctypes.c_int
  pichar = ctypes.c_char
  piint = ctypes.c_int
  
  class PicamCameraID(ctypes.Structure):
      _fields_ = [(&quot;model&quot;, PicamModel),
          (&quot;computer_interface&quot;, PicamComputerInterface),
          (&quot;sensor_name&quot;, pichar * PicamStringSize_SensorName),
          (&quot;serial_number&quot;, pichar * PicamStringSize_SerialNumber)]
  </code></pre>
  <p>I am then trying to call the function as follows:</p>
  <pre><code>&quot;&quot;&quot;Picam_GetAvailableDemoCameraModels( const PicamModel** model_array,
                                         piint* model_count )&quot;&quot;&quot;
  model_count = ctypes.c_int()
  
  x = PicamCameraID()
  
  model_array = pointer(pointer(x))
  
  err = picam.Picam_GetAvailableDemoCameraModels( model_array, pointer(model_count))
  </code></pre>
  <p>After running this, I am able to read the model_count variable (which tells me there are 133 items in it) and I am able to access the contents of model_array, but the contents are incorrect or garbage, e.g.</p>
  <pre><code>model_count
  c_long(133)
  model_array[5].contents.sensor_name
  '@~\xe7\x02'
  </code></pre>
  <p>I think I'm doing something wrong to the effect of handling the pointers (double-pointer?) incorrectly, but I can't determine where I am straying from the defined syntax.</p>
  <p>It seems to me that my syntax is passing in a pointer-to-a-pointer-to-a-struct, <strong>BUT</strong> I expect the result to be an <em>array</em> of the struct (PicamCameraID).  If it was just a struct, I could only have 1 model in the array.  However, I don't know how many models are in the array until after the function runs... and I'm not sure how to (or even if I must) deal with that.</p>
  <p>Any insights would be much appreciated and please let me know if I can elaborate anywhere.</p>
  <p>Regards</p>