Re: [ctypes-users] Handling pointers returned from and later passed to C functions?
Brought to you by:
theller
From: Thomas H. <th...@ct...> - 2007-10-17 10:53:32
|
Jim Fulton schrieb: > Many C libraries follow a common pattern: > > - Call a constructor that allocates and returns a pointer to a C object. > > - Call functions with the pointer returned from the constructor. > > On my system (32-bit Ubuntu Linux), constructors return integers and > passing these integers to functions expecting pointers works fine. I > worry that this is non portable. I especially worry about systems on > which sizeof(int) != sizeof(void *). > > I suppose I should set restype on constructors to ctypes.c_void_p. > And that I should set the corresponding types in argtypes on called > functions to ctypes.c_void_p. Will this cause necessary conversions > to take place? I'd like some confirmation that this is the right way > to approach this situation. Yes, this is exactly the way it should be done, and it works on 64-bit systems then. An alternative to using argtypes would be to wrap the integer in a c_void_p(...) instance and pass that to the function, for a missing restype there is no such workaround. > Is this (very common) situation documented anywhere? I thought it was mentioned somewhere but I can't find it. Would you like to suggest a documentation patch? Thomas |