Re: 2.6 - 3.x bytearray from c_char_p

"Jeff C. Britton" <[email protected]> Thu, 12 May 2011 13:40:24 -0700
Newsgroups gmane.comp.python.ctypes
Message-ID <[email protected]>
Note the following:
1) You omitted the return type when using CFUNCTYPE.
2) The 'C' code assigned to buffer[3] which overflows the array.

I tested the following and it worked.

from ctypes import *
import ctypes
import struct

# I created a .dll named callback.dll for testing
dll = cdll.callback

def Callback(pointer, length):
    s = pointer[:length]
    print map(ord, s)
    # or
    print struct.unpack('B'*length, s)
    return 0
    
# the first value indicates the return type
CALLBACK_TYPE = CFUNCTYPE(c_int, c_char_p, c_int)

UseCallback = dll.use_callback
UseCallback.restype = c_int
UseCallback.argtypes = [ CALLBACK_TYPE ]

callback_wrapper = CALLBACK_TYPE(Callback)
UseCallback(callback_wrapper)

-----Original Message-----
From: Thomas Stover [mailto:[email protected]] 
Sent: Thursday, May 12, 2011 9:30 AM
To: [email protected]
Subject: [ctypes-users] 2.6 - 3.x bytearray from c_char_p


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;  // <------ overflow

 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
_______________________________________________
ctypes-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ctypes-users

------------------------------------------------------------------------------
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