Python ctypes: Python file object <-> C FILE *

前端 未结 6 767
深忆病人
深忆病人 2020-12-14 04:16

I am using ctypes to wrap a C-library (which I have control over) with Python. I want to wrap a C-function with declaration:

int fread_int( FILE * stream );
         


        
6条回答
  •  爱一瞬间的悲伤
    2020-12-14 05:07

    If you want to use stdout/stdin/stderr, you can import those variables from the standard C library.

    libc = ctypes.cdll.LoadLibrary('libc.so.6')
    cstdout = ctypes.c_void_p.in_dll(libc, 'stdout')
    

    Or, if you want to avoid using void* for some reason:

    class FILE(ctypes.Structure):
        pass
    
    FILE_p = ctypes.POINTER(FILE)
    
    libc = ctypes.cdll.LoadLibrary('libc.so.6')
    cstdout = FILE_p.in_dll(libc, 'stdout')
    

提交回复
热议问题