Win32 COM and Collections
Alex Martelli
alex at magenta.com
Sun Aug 27 16:04:34 EDT 2000
"Dale Strickland-Clark" <dale at out-think.NOSPAMco.uk> wrote in message
news:yPaq5.24731$Xg.672226 at news-east.usenetserver.com...
[snip]
I've already answered Dave by e-mail, but, for the record:
with this version of CollectionTest, in his otherwise unchanged
code:
def CollectionTest(self):
data = ['a', 'b', 'c']
cln = win32com.server.util.NewCollection(data)
return cln
the following VBScript code:
Set Testing = CreateObject("Testing.Collection")
WScript.echo(Testing.Test())
Set collect = Testing.CollectionTest()
WScript.echo(CStr(collect.Count()) & " items in collection")
For Each element In collect
WScript.echo(element)
Next
will happily run under, e.g., CScript, and output:
The Testing Collection COM server is running.
3 items in collection
a
b
c
To return a collection of _objects_, they need a modicum
of COM support, e.g.:
class ClnTest:
_public_methods_ = ['Helo']
def __init__(self, data):
self.val = data
def Helo(self):
return 'Here is ('+self.val+')'
and, with the line, at the start of the Python script:
from win32com.server.util import NewCollection, wrap
changing CollectionTest to:
def CollectionTest(self):
data = [wrap(ClnTest('a')), wrap(ClnTest('b')), wrap(ClnTest('c'))]
# data = ['a', 'b', 'c']
cln = NewCollection(data)
return cln
and the VBS line in the foreach loop to:
WScript.echo(element.Helo())
we similarly get:
The Testing Collection COM server is running.
3 items in collection
Here is (a)
Here is (b)
Here is (c)
Note: the _public_methods_ member IS needed in the class;
without it, you'll get something such as:
The Testing Collection COM server is running.
C:\My Documents\testob.vbs(4, 1) Python COM Server Internal Error:
Unexpected Py
thon Error: win32com.server.policy error: Object does not support
DesignatedWrap
Policy
The 'wrap' (win32com.server.util.wrap) is also necessary for the
objects placed in the list passed to the NewCollection function;
without it, you'll get something such as:
The Testing Collection COM server is running.
3 items in collection
C:\My Documents\testob.vbs(7, 1) Unspecified error
Alex
More information about the Python-list
mailing list