ctypes pointer

Nitin Bhargava nitinbhargava74 at hotmail.com
Tue Feb 7 03:12:08 CET 2012


Hi,
     I wish to call a function in c++ dll which returns a unicode array from python.
 
 
 
extern "c"
{
__declspec(dllexport) int test(wchar_t** ppText)
{
      *ppText = new wchar_t[30];
       wcscpy(*ppText, L"this is a test");
       return 0;
}
 
 
__declspec(dllexport) int FreeString(wchar_t** ppText)
{
      delete [] *ppText;
       return 0;
}
 
}
 
 
 
In python I'm doing this
 
import ctypes
dll = ctypes.cdll.LoadLibrary(r"x")
func = dll.test
func.argtypes = [ ctypes.POINTER(ctypes.c_wchar_p)]
 
funcDel = dll.FreeString
funcDel.argtypes = [ctypes.POINTER(ctypes.c_wchar_p)]
 
a = ctypes.c_wchar_p('')
z = ctypes.pointer(a)
 
n= func(z)
 
print a  /* displays  "this is a test" */
 
/* is this correct way to return charater array */
/* will memory allocated in c++ function be freed by python */
/* or should I call */
 
n = funcDel(z)
 
 
thanks,
Nitin.
 
 
  		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120207/ae175477/attachment.html>


More information about the Python-list mailing list