visual basic -> win32com server problem
Giles Brown
giles_brown at hotmail.com
Thu Feb 6 11:06:00 EST 2003
Hi Mark,
Thanks for looking at this problem.
I tried what you wrote posting on this and it didn't fix it
unfortunately.
I still think the reason has to do with how I've defined the Stations
property
in my Application class and not how the Stations class is defined.
Anyway I've created some running code and included the debug trace
for it to show you what I mean. As it happens I want the result of
the Item method to be read only anyway, so I don't want a Set_value_
really.
This was run using VB6.0 and win32all build 150 running against Python
2.2.1.
Thanks v. much for your help.
Cheers,
Giles
Python COM Server (DefaultTest.py)
==============================================
"""COM server exposing collection property with default "Item"
method."""
import winerror
from win32com.server.exception import COMException
from win32com.server import util
from win32com.server import dispatcher
def Wrap(object):
return util.wrap(object,
useDispatcher=dispatcher.DefaultDebugDispatcher)
class Station:
_public_methods_ = []
_public_attrs_ = ['Name']
def __init__(self, name):
self.Name = name
class Stations:
_public_methods_ = ['Item']
def __init__(self):
self.stations = {
'StationX' : Wrap(Station('StationX')),
'StationY' : Wrap(Station('StationX'))
}
def Item(self, *args):
if len(args) != 1:
raise COMException(scode=winerror.DISP_E_BADPARAMCOUNT)
try:
return self.stations[args[0]]
except IndexError, desc:
raise COMException(scode=winerror.DISP_E_BADINDEX,
desc=str(desc))
_value_ = Item
#
# Don't actually want the item settable, but put it in just to
# show that it doesn't cause make the VB code work :-)
# (read only method?)
def SetItem(self, *args):
pass
Set_value_ = SetItem
class Application:
_reg_clsid_ = '{59F3FBE3-38E5-11D7-869E-0002A5DB8B18}'
_reg_progid_ = "DefaultTest.Application"
_public_attrs_ = ['Stations']
_readonly_attrs_ = ['Stations']
# If we have empty _public_methods_ list here we get error about
# not supporting DesignatedWrapPolicy.
_public_methods_ = ['Dummy']
def Dummy(self):
pass
def __init__(self):
self.Stations = Wrap(Stations())
if __name__=='__main__':
import win32com.server.register
win32com.server.register.UseCommandLine(Application)
Visual Basic Code with two Buttons (Form1.frm)
==============================================
Option Explicit
Dim App As Object
Private Sub Command1_Click()
' Sanity check - test calling item explicitly
MsgBox App.Stations.Item("StationX").Name
End Sub
Private Sub Command2_Click()
' Test calling item implicitly
MsgBox App.Stations("StationX").Name
End Sub
Private Sub Form_Load()
Set App = CreateObject("DefaultTest.Application")
End Sub
Trace Output
==============================================
This output is from starting the form. Clicking on Command1 then
Command2.
This looks to me like in the first case it gets and invokes "Stations"
from the App object and
then gets and invokes "Item" from the Stations object (with an
argument of "StationX").
Whereas in the second case it only gets and invokes "Stations" on the
App
object (with an argument of "StationX"). Presumably this is to do
with how the App
Stations property is defined?
Object with win32trace dispatcher created (object=None)
Object with win32trace dispatcher created (object=<DefaultTest.Station
instance at 0x028A81A0>)
Object with win32trace dispatcher created (object=<DefaultTest.Station
instance at 0x028A9CF0>)
Object with win32trace dispatcher created
(object=<DefaultTest.Stations instance at 0x028A8F20>)
in <DefaultTest.Application instance at 0x028A8BE0>._QueryInterface_
with unsupported IID IPersistStreamInit
({7FD52380-4E07-101B-AE2D-08002B2EC713})
in <DefaultTest.Application instance at 0x028A8BE0>._QueryInterface_
with unsupported IID IPersistPropertyBag
({37D84F60-42CB-11CE-8135-00AA004BB851})
in _GetIDsOfNames_ with '('Stations',)' and '1033'
in _Invoke_ with 1000 1033 3 ()
in _GetIDsOfNames_ with '('Item',)' and '1033'
in _Invoke_ with 1000 1033 3 (u'StationX',)
in _GetIDsOfNames_ with '('Name',)' and '1033'
in _Invoke_ with 1000 1033 3 ()
in _GetIDsOfNames_ with '('Stations',)' and '1033'
in _Invoke_ with 1000 1033 3 (u'StationX',)
in _GetIDsOfNames_ with '('Name',)' and '1033'
More information about the Python-list
mailing list