[python-win32] How to create IStream from Python file to use in ctypes

Conrado P. L. Gouvêa conradoplg at gmail.com
Tue Jan 6 02:16:49 CET 2015


I want to call the function GdipLoadImageFromStream(IStream* stream,
GpImage **image) using ctypes, and I need to create the first
parameter from a Python file (e.g. created with open). Is there a way
to do this?

I'm aware of win32com.server.util.FileStream and
win32com.server.util.wrap which can be used to create a IStream from a
file, but the resulting object (PyIStream) is not accepted by ctypes
("ArgumentError: argument 1: : Don't know how to convert parameter 1")

Cheers
Conrado



import ctypes
import pythoncom
from win32com.server import util
from ctypes import wintypes

gdiplus = ctypes.windll.gdiplus

WNDPROC = wintypes.WINFUNCTYPE(ctypes.c_long, ctypes.c_int,
ctypes.c_uint, ctypes.c_int, ctypes.c_int)

class GdiplusStartupInput(wintypes.Structure):
    _fields_ = [
        ('GdiplusVersion', ctypes.c_uint),
        ('DebugEventCallback', ctypes.c_void_p),
        ('SuppressBackgroundThread', wintypes.BOOL),
        ('SuppressExternalCodecs', wintypes.BOOL),
    ]

    def __init__(self):
        wintypes.Structure.__init__(self)
        self.GdiplusVersion = 1
        self.DebugEventCallback = None
        self.SuppressBackgroundThread = 0
        self.SuppressExternalCodecs = 0

StartupInput = GdiplusStartupInput()
token = ctypes.c_ulong()
gdiplus.GdiplusStartup(ctypes.pointer(token),
ctypes.pointer(StartupInput), None)


f = open(r'test.jpg')
img = ctypes.c_void_p()
fs = util.FileStream(f)
istream = util.wrap(fs, pythoncom.IID_IStream)
r = gdiplus.GdipLoadImageFromStream(istream, ctypes.byref(img))


More information about the python-win32 mailing list