title: How to access with ctypes to functions returning custom types coded in a Delphi dll?
body: <p>I have a Delhi DLL that is exposing a function with the following signature:</p>
  
  <pre class="lang-pascal prettyprint-override"><code>Function MyFunc(ObjID : Cardinal) : TMyRec; stdcall;
  </code></pre>
  
  <p>where the records are defined so:</p>
  
  <pre class="lang-pascal prettyprint-override"><code>type TMyRec = record
    Count : Cardinal;
    Items : array of TMyItemRec;
  end;
  
  type TMyItemRec = record
    ID : Cardinal;
    Params : array of String;
  end;
  </code></pre>
  
  <p>Now my question is: how can I acces results of MyFunc calling the dll with Python ctypes? I coded two classes that mimic the types</p>
  
  <pre class="lang-py prettyprint-override"><code>from ctypes import *
  class TMyItemRec(Structure):
      _fields_ = [("ID", c_int), ("Params", POINTER(c_wchar_p))]
  
  class TMyRec(Structure):
      _fields_ = [("Count", c_int), ("Params", POINTER(TMyItemRec))]
  </code></pre>
  
  <p>but when I try to read data like this:</p>
  
  <pre class="lang-py prettyprint-override"><code>my_dll = windll.Script
  
  def GetMyRec(ID):
      my_dll.MyFunc.argtypes = [c_uint]
      my_dll.MyFunc.restype = TClilocRec
  
      return my_dll.Script_GetClilocRec(ID)
  </code></pre>
  
  <p>I get access violation error.</p>