2.6 - 3.x bytearray from c_char_ p
Thomas Stover <[email protected]> Thu, 12 May 2011 11:30:07 -0500
| Newsgroups | gmane.comp.python.ctypes |
|---|---|
| Message-ID | <[email protected]> |
Given a C callback that passes a variable length byte array, how can that
be turned into a bytearray() type?
=====
int use_callback( void (*callback) (unsigned char *, int) )
{
unsigned char *buffer;
if(callback == NULL)
return 1;
buffer = malloc(3);
if(buffer == NULL)
return 1;
buffer[0] = 1;
buffer[1] = 2;
buffer[3] = 3;
callback(buffer, 3);
free(buffer);
return 0;
}
=====
I'm trying to figure out what this would look like.
def Callback(pointer, length):
Array = bytearray(length)
Array = some how pull length bytes from pointer with no string
encoding?
CALLBACK_TYPE = CFUNCTYPE(c_char_p, c_int)
UseCallback = use_callback
UseCallback.restype = c_int
UseCallback.argtypes = [ CALLBACK_TYPE ]
callback_wrapper = CALLBACK_TYPE(Callback)
UseCallback(callback_wrapper)
I've seen examples of passing an already created bytearray as a ctypes
function argument, but not this. Maybe there is a way to do it with
strings, but the raw bytes of python strings in general is about ambiguous
as it gets in my mind.
--
www.thomasstover.com
FLYNN LIVES!
------------------------------------------------------------------------------
Achieve unprecedented app performance and reliability
What every C/C++ and Fortran developer should know.
Learn how Intel has extended the reach of its next-generation tools
to help boost performance applications - inlcuding clusters.
http://p.sf.net/sfu/intel-dev2devmay