How to return a IStream COM object?

Discussion in 'Python' started by =?ISO-8859-1?Q?Juan_Carlos_CORU=D1A?=, Aug 7, 2003.

  1. Hello all,

    I have developed an automation server using Mark's win32 extensions
    and I must return a IStream COM object from one method of the
    automation server.

    Here is an extract of my code:

    class Stream:
    _public_methods_ = [ 'Read', 'Write', 'read', 'write',
    'Seek', 'SetSize', 'CopyTo',
    'Commit', 'Revert', 'LockRegion', 'UnlockRegion',
    'Stat', 'Clone']
    _com_interfaces_ = [ pythoncom.IID_IStream ]

    def __init__(self, mode, data):
    print "init stream"
    self._data = data
    self._ptr = 0
    self._size = len(data)
    self._creationTime = Time(time())
    self._accessTime = Time(time())
    self._modificationTime = Time(time())
    self._mode = mode

    def Read(self, numBytes):
    """Read the specified number of bytes from the string"""
    self._accessTime = Time(time())
    data = self._data[self._ptr:self._ptr + numBytes]
    self._ptr = self._ptr + numBytes
    if self._ptr > len(self._data): self._ptr = len(self._data) - 1
    print "Read data", data
    return data # string
    read = Read

    def Write(self, data):
    """Write data to stream"""
    self._modificationTime = Time(time())
    print "Write data", data
    self._data = self._data + data
    write = Write

    def Seek(self, offset, origin):
    """Changes the seek pointer to a new location"""
    self._ptr = offset + origin
    return self._ptr # ULARGE_INTEGER

    def SetSize(self, newSize):
    """Changes the size of the stream object."""
    self._size = newSize

    def CopyTo(self, stream, cb):
    """Copies a specified number of bytes from the
    current seek pointer in the stream to the current
    seek pointer in another stream"""
    data = self._data[self._ptr:self._ptr+cb]
    return len(data) # ULARGE_INTEGER

    def Commit(self, flags):
    """Ensures that any changes made to a stream object
    open in transacted mode are reflected in the parent storage"""

    def Revert(self):
    """Discards all changes that have been made to a
    transacted stream since the last PyIStream::Commit call"""

    def LockRegion(self, offset, cb, lockType):
    """Restricts access to a specified range of bytes
    in the stream"""

    def UnlockRegion(self, offset, cb, lockType):
    """Removes the access restriction on a range of bytes
    previously restricted with PyIStream::LockRegion"""

    def Clone(self):
    """Creates a new stream object with its own seek pointer
    that references the same bytes as the original stream"""
    # not yet implemented
    return # PyIStream

    def Stat(self, grfStatFlag):
    """Returns information about the stream"""
    st = ('stream', STGTY_STREAM, self._size, self._modificationTime,
    self._creationTime, self._accessTime, self._mode, LOCK_EXCLUSIVE,
    return st # STATSTG

    class AutomationServer:
    def GetStream(self):
    from win32com import storagecon
    _grfMode_READ = storagecon.STGM_SHARE_EXCLUSIVE | \
    storagecon.STGM_DIRECT | \
    _grfMode_WRITE = storagecon.STGM_SHARE_EXCLUSIVE | \
    s = XMLRPCLib2.Stream(_grfMode_WRITE, '')
    return wrap(s, pythoncom.IID_IStream, useDispatcher=useDispatcher)

    I'm trying to use this automation server in the Navision ERP. Navision
    accepts the return value VT_UNKNOWN and maps it to an Navision
    internal type InStream or OutStream. These types must be standard
    IStream COM objects.
    Since I'm using a correct typelib calling GetStream does not generate
    a exception, the problem appears when I call a method of the Navision
    type InStream/OutStream accessing the Stream methods. The error
    message says something like "The is an problem communicating with the
    stream". The python trace collector says nothing.

    Since there are working automation servers that returns a IStream COM
    object for Navision, I think there must be an error in my Stream
    class. I suppose that python maps the native IStream methods to the
    PyIStream methods in order to avoid using the pointers of the native

    I made some test using pythoncom._univgw in order to create a vtable
    for the IStream COM object, but the function CreateTearOff crashes
    python. So, running after changing the line "import
    univgw" to "from pythoncom import _univgw as univgw" crashes python. A
    bug? Can anybody confirm this?

    Can anybody help me? Maybe a working example?
    =?ISO-8859-1?Q?Juan_Carlos_CORU=D1A?=, Aug 7, 2003
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Todd Beauchemin

    overloaded >> and istream delimiters

    Todd Beauchemin, Jul 31, 2003, in forum: C++
    Victor Bazarov
    Jul 31, 2003
  2. Greenhorn
    Keith Thompson
    Mar 6, 2005
  3. Randy
    Jan 17, 2006
  4. Replies:
    Stuart Redmann
    May 10, 2006
  5. xmllmx
    Jorgen Grahn
    Jun 15, 2010

Share This Page