[Tutor] pass a memory pointer to a c function
Nathan Smith
nathan-tech at hotmail.com
Sat Jan 29 21:35:35 EST 2022
Hello!
I actually managed to figure this out myself, but do have a related
question (I hope you all don't mind!).
My solution was:
import ctypes
data=b/"some text"
x=bytearray(data)
Buffer = ctypes.c_char * len(x)
buf = Buffer.from_buffer(x)
addr=ctypes.addressof(buf)
Then pass addr to the c function.
My question is, do I need to free that buffer? Will python do it when
the object is deleted?
Thanks
On 30/01/2022 01:34, Nathan Smith wrote:
> Hi list,
>
>
> Put simply, I have a c function which takes a memory pointer as one of
> the arguments, "An unmanaged pointer to the memory location as an
> IntPtr." which I am calling from a python program.
>
>
> I've done some digging and it looks like tempfile can write to the
> memory, but can I use that to then get the pointer and pass it over?
>
> More digging seems to indicate id(obj) gets the memory location for
> that object, but as it is a python object I'm guessing that is not
> much use?
>
> What I want the function to receive is the memory pointer for the data
> in the tempfile, rather than the python tempfile object.
>
>
> I've also figured out that ctypes.pointer(i) is probably what I want,
> where i is a ctypes.c_int(python_int) of some kind, but getting py_int
> to be the correct memory locations is my stumbling block
>
>
> Sorry in advance if my terms are off here, I'm only a passing c user and.
>
> I hope someone can point me in the right direction!
>
> Nathan
>
--
Best Wishes,
Nathan Smith, BSC
My Website: https://nathantech.net
More information about the Tutor
mailing list