title: ctypes: Passing string by reference
body: <p>With regard to Python 2.5, I wish to implement the following C code in python:</p>
  
  <p>C code:</p>
  
  <pre><code>#include &lt;wtypes.h&gt;
  
  __declspec(dllexport) X_ERROR __stdcall GetAdmSize(INT* piAdmSize, CHAR** chBuf, INT iBufSize);
  
  int iProgSize = 0;
  char szProgSize[50];
  char* pszProgSize = szProgSize;
  error = GetAdmSize(&amp;iProgSize, &amp;pszProgSize, 49);
  </code></pre>
  
  <p>Python code:</p>
  
  <pre><code>from ctypes import *
  c_bool = c_int
  
  x = windll.LoadLibrary("x.dll")
  iProgSize = c_int()
  szProgSize = create_string_buffer(50)
  getAdmSize = x.AdkGetAdmSize
  getAdmSize.argtypes = [POINTER(c_int), POINTER(c_char_p), c_int]
  status = getAdmSize(byref(iProgSize), byref(szProgSize), 49)
  </code></pre>
  
  <p>But I'm getting the following exception:</p>
  
  <pre><code>Traceback (most recent call last):
      status = getAdmSize(byref(iProgSize), (szProgSize), 49)
  ArgumentError: argument 2: &lt;type 'exceptions.TypeError'&gt;: expected LP_c_char_p instance instead of c_char_Array_50
  </code></pre>
  
  <p>What am I doing wrong?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I tried: </p>
  
  <pre><code>pointerToStringBuffer = cast(szProgSize, c_char_p)
  status = getAdmSize(byref(iProgSize), byref(pointerToStringBuffer), 49)
  </code></pre>
  
  <p>But this gives me:</p>
  
  <pre><code>Traceback (most recent call last):
      status = getAdmSize(byref(iProgSize), byref(pointerToStringBuffer), 49)
  WindowsError: exception: access violation reading 0x00000031
  </code></pre>
  
  <p>As a matter of interest, I get the same error in C if I call this:</p>
  
  <pre><code>error = AdkGetAdmSize((int*)0, (char**)49, 0);
  </code></pre>
  
  <p>Seems like my arguments are not aligned correctly perhaps</p>
  
  <p>Any suggestions?</p>