Index: Modules/_ctypes/cfield.c =================================================================== --- Modules/_ctypes/cfield.c (Revision 53537) +++ Modules/_ctypes/cfield.c (Arbeitskopie) @@ -1424,10 +1424,19 @@ #endif #ifdef MS_WIN32 +/* We cannot use SysFreeString as the PyCObject_FromVoidPtr + because of different calling convention +*/ +static void _my_SysFreeString(void *p) +{ + SysFreeString((BSTR)p); +} + static PyObject * BSTR_set(void *ptr, PyObject *value, unsigned size) { BSTR bstr; + PyObject *result; /* convert value into a PyUnicodeObject or NULL */ if (Py_None == value) { @@ -1455,15 +1464,19 @@ } else bstr = NULL; - /* free the previous contents, if any */ - if (*(BSTR *)ptr) - SysFreeString(*(BSTR *)ptr); - - /* and store it */ + if (bstr) { + result = PyCObject_FromVoidPtr((void *)bstr, _my_SysFreeString); + if (result == NULL) { + SysFreeString(bstr); + return NULL; + } + } else { + result = Py_None; + Py_INCREF(result); + } + *(BSTR *)ptr = bstr; - - /* We don't need to keep any other object */ - _RET(value); + return result; }