Search code examples
pythonctypesmediainfo

How to pass a 'file' to cpp function that expects 'Buffer' in Python ctypes?


I want to add 'Open_Buffer_Continue' function to the MediaInfoDLL.py ctypes wrapper, the bindings are here and MediaInfoDLL.cs C# binding already implements this function so it's possible.

How do i pass the following:

file = open('./file.avi', 'rb')

to the mediainfo cpp Open_Buffer_Continue which expects:

(   const ZenLib::int8u *   Buffer,
    size_t                  Buffer_Size 
) 

?

this is what i have so far:

MediaInfo_Open_Buffer_Init = MediaInfoDLL_Handler.MediaInfo_Open_Buffer_Init
MediaInfo_Open_Buffer_Init.argtype = [c_size_t, c_size_t]
MediaInfo_Open_Buffer_Init.restype = None

MediaInfo_Open_Buffer_Continue = MediaInfoDLL_Handler.MediaInfo_Open_Buffer_Continue
MediaInfo_Open_Buffer_Continue.argtype = [c_size_t, c_size_t]  # NOT SURE HERE var 1
MediaInfo_Open_Buffer_Continue.restype = c_size_t

MediaInfo_Open_Buffer_Finalize = MediaInfoDLL_Handler.MediaInfo_Open_Buffer_Finalize
MediaInfo_Open_Buffer_Finalize.argtype = [c_void_p]
MediaInfo_Open_Buffer_Finalize.restype = None

Solution

  • I know nothing about mediainfo, but it looks like ZenLib::int8u* is a pointer to bytes, so the best thing to use would be:

    MediaInfo_Open_Buffer_Continue.argtype = [c_char_p, c_size_t]
    

    Then read your file into a string and pass it:

    with open('./file.avi','rb') as f:
        data = f.read()
    MediaInfo_Open_Buffer_Continue(data,len(data))