Re: How to pass FILE *

xDog Walker <[email protected]> Sat, 29 Sep 2012 02:01:11 -0700
Newsgroups gmane.comp.python.ctypes
Message-ID <[email protected]>
On Saturday 2012 September 29 05:09, LukenShiro wrote:
> > dlg_progressbox(const char *title,
> >               const char *cprompt,
> >               int height,
> >               int width,
> >               int pauseopt,
> >               FILE *fp)
> >
> > I can open the file to be referenced:
> >
> > fp = os.fdopen(self.pipefds[0], 'r')
> >
> > Every thing I've tried ends with ctypes raising a TypeError.
>
> AFAIK you can use (FILE is defined in /usr/include/stdio.h):
> """
> class _IO_FILE(ctypes.Structure):
>     pass
> FILE = _IO_FILE
> """
>
> then you can use (as type for 'FILE *'):
> ctypes.POINTER(FILE)

Tried that but it doesn't work because the output of os.fdopen is a python 
file object which ctypes rejects.

I got it to work by using the libc's fdopen and defining the argtype in the 
function as a c_void_p:

-------------------------------------
from ctypes import *

libc = CDLL(find_library("c"))
fdopen = libc.fdopen
fdopen.argtypes = (c_int, c_char_p,)
fdopen.restype = c_void_p

libdialog = CDLL(find_library("dialog"))
progbox = libdialog.dlg_progressbox
progbox.restype = c_int
progbox.argtypes = (c_char_p, c_char_p, c_int, c_int, c_int, c_void_p)

FILEp = fdopen(self.pipefds[0], 'r')
-------------------------------------
FILEp can then be used as the 6th parameter of libdialog.dlg_progressbox.

-- 
Yonder nor sorghum stenches shut ladle gulls stopper torque wet 
strainers.



------------------------------------------------------------------------------
How fast is your code?
3 out of 4 devs don\\\'t know how their code performs in production.
Find out how slow your code is with AppDynamics Lite.
http://ad.doubleclick.net/clk;262219672;13503038;z?
http://info.appdynamics.com/FreeJavaPerformanceDownload.html